【silverlight】手把手教你如何实现页面翻转效果

【silverlight】手把手教你如何实现页面翻转效果,第1张

概述  The Secret Behind the Page Flip Technique Two Flavors of Page Flip The Page Flip technique has been around for several years in Flash. However, not all implementations are equal. Usually, the premiu   The Secret Behind the Page Flip Technique



Two Flavors of Page Flip

The Page Flip technique has been around for several years in Flash. However,not all implementations are equal. Usually,the premium page flip components that are sold to developers are substantially different than the free page flip tutorials you might find online. The biggest difference is in the range of motion of the page corner being flipped. Most free page flip tutorials have a fixed radius motion path instead of a wIDer range of motion. In other words,no matter where you drag your mouse,the page corner will stay fixed to a curve as it travels from one sIDe to the other. The math behind this technique is rather straightforward and variations of its implementation are found all over the place. The problem,though,is that the premium page flip technique allows a far greater range of motion while flipPing the page (as shown above),which yIElds a much richer experIEnce. However,nobody ever talks about how this one is done!

The Page Flip in Silverlight

So what we're going to do today is explain the mathematics behind a premium page flip experIEnce engineered in Silverlight. The Silverlight implementation shown above was solved using trigonometry,cLever masking and a little "smoke and mirrors" magic for the shadows. As with many of my blog posts,the effect can usually be split into the mechanics of the effect(the math part) and the presentation (the rendering tricks). Since Silverlight and Flash handle the mechanics of masking,clipPing and rendering transforms differently,this tutorial is aimed primarily at Silverlight developers. However,the mathematical secrets behind the Page Flip technique are applicable to interactive developers in any language and this tutorial focuses primarily on explaining the rotational mechanics of the solution.

Step 1: Follow the Mouse,Constrain the Corner Point



[ Download BlogPageFlipStudy01.zip ]

PageFlipStudy01 sets up the framework for what we need to do. Please revIEw it carefully,since the next 3 studIEs are built on top of each other. If you open it up,you'll notice the solution only has MainPage.xaml and a Dot.xaml control I created to visualize the solution variables so far. If you look over the MainPage.xaml.cs code,you'll see that the loaded() function mainly initializes variables and setups up the Mouse event handlers. All the logic happens in the CompositeTarget.Rendering() animation loop. Here are the key variables,as illustrated above:

M: The position of the Mouse as set by the MouseMove() event F: The mouse Follower that allows us to gracefully ease toward the raw mouse position C: The Constraint point which defines where the page corner will eventually be R1: The Radius point on the constraining circumference of the arc that defines the maximum page wIDth allowed. SC: Spine Center SB: Spine Bottom EB: Edge Bottom

And here is the main constraint logic in the animation loop:



The most important variable at this point is our calculated constraint (dot C above). I never use the positional data from the Mouse events directly,but usually create a Follower variable that constantly eases toward the raw mouse position. However,for the page flip effect to work,the Constraint point that positions the page corner needs to respect the maximum wIDth of the page. In other words,the line SB > C can never be longer than the line SB > EB .

Step 2: Adding the Second Constraint and Defining the Critical Triangle



[ Download BlogPageFlipStudy02.zip ]

In PageFlipStudy02,we need to add an additional radius constraint R2 to define the maximum distance the page corner can be from the spine top (dot ST above). In other words,the line ST > C cannot be longer than the line that would connect ST and R2 . Here is the additional constraint code for the second constraining radius:



Now that the corner point C has been constrained between the two radii,we can get to the heart of the solution; the critical triangle. The critical triangle is creating by taking the bisector of line C > EB,which is marked by dot T0 . Shoot out a line perpendicular to the bisector toward the page bottom,and you get T2 . You then close up the triangle with T1 . This critical triangle will be used to control both the location and rotation of the Page object and the Rectangle that can serve as your clipPing boundary. Here is the logic that calculates the tangent:



You may want to experiment with this skeleton and then experiment with the original page flip solution at the start of this article and see if you figure out how the propertIEs of the critical triangle are used in a rendered solution.

Step 3: Adding the Page and Rotating the Corner



[ Download BlogPageFlipStudy03.zip ]

The biggest change in PageFlipStudy03 is the addition of a pageBack.xaml control that contains the page graphic we want to display. If you look at the control,you'll notice that both the positional and rotational point of the control is located at its bottom left corner. This makes sense,since when the page is finally flipped,the corner you're trying to flip is its bottom left corner. In MainPage.xaml.cs,the positioning and rotating of the page is calculated like so:



The important thing to realize is that the angle defined by T2 to C defines the amount of rotation needed for the page control if you anchor it to the constrained corner point C . You can determine the angle in radians by performing a Math.atan2() operation on the variables shown above. Once you have this tangent value,you can set the page control's rotation angle to this value after converting it from radians to degrees (by multiplying by Math.PI/180.0),and the Page being flipped will always intersect with T2 .

Step 4: Defining the ClipPing Region



[ Download BlogPageFlipStudy04.zip ]

Up to this point,you have all the logic required to constrain and rotate the page properly. The last essential step,however,is defining a clipPing region that will also be animated to define what portion of the page should be visible during the flip. Since the focus of this tutorial is to understand the underlying math of the technique,I'll keep the rectangle visible so you can see exactly what is going on.

The red Rectangle above that can give me the points for a clipPing path was created in such a way that it's rendertransformOrigin lines up to wherever T2 is. You calculate its position and rotation like so:



Inevitably,when you want to implement this solution,the red Rectangle object used above will need to be converted to a collection of Path figure segments within a PathGeometry that defines the clipPing region for the page control (it's easIEr doing it this way than trying to perform a complex rotation on a clipPing path,belIEve me). The good news is that Silverlight's Generaltransform object makes this very easy:

That's it! The purple region above where the blue page and the red rectangle overlap defines how much of the page is visible as your flip it. Using the calculated points above,you can convert this rectangle to a proper pathGeometry that clips the page control,making it look like the page is being revealed by being flipped over. This trigonometry defines the mathematical underpinnings of the premium page flip effect. From here,you can use this structure to add your shadows and add multiple pages. FlipPing from left to right is simply a reversal of the geometry and flipPing from the top is a basic inversion. However,I'll leave it for you to add the shading and multiple page logic. Have fun!

总结

以上是内存溢出为你收集整理的【silverlight】手把手教你如何实现页面翻转效果全部内容,希望文章能够帮你解决【silverlight】手把手教你如何实现页面翻转效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1042352.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存