Simple page turning

Source: Internet
Author: User

Http://marshal.easymorse.com/archives/3760

The page flip effect is similar to the following:

It is common in e-book applications. There are two key points:

    • Flip Animation
    • Processing of swipe

 

Let's talk about the implementation of swipe. You can refer to the simple gesture example: tap to understand the gesture types.

In the viewdidload method, you have registered four easy handling methods in the upper, lower, left, and right directions:

-(Void) viewdidload {

Uiswipegesturerecognizer * recognizer;

Recognizer = [[uiswipegesturerecognizer alloc] initwithtarget: Self action: @ selector (handleswipefrom :)];
[Recognizer setdirection :( uiswipegesturerecognizerdirectionright)];
[[Self view] addgesturerecognizer: recognizer];
[Recognizer release];

Recognizer = [[uiswipegesturerecognizer alloc] initwithtarget: Self action: @ selector (handleswipefrom :)];
[Recognizer setdirection :( uiswipegesturerecognizerdirectionup)];
[[Self view] addgesturerecognizer: recognizer];
[Recognizer release];

Recognizer = [[uiswipegesturerecognizer alloc] initwithtarget: Self action: @ selector (handleswipefrom :)];
[Recognizer setdirection :( uiswipegesturerecognizerdirectiondown)];
[[Self view] addgesturerecognizer: recognizer];
[Recognizer release];

Recognizer = [[uiswipegesturerecognizer alloc] initwithtarget: Self action: @ selector (handleswipefrom :)];
[Recognizer setdirection :( uiswipegesturerecognizerdireleft left)];
[[Self view] addgesturerecognizer: recognizer];
[Recognizer release];


[Super viewdidload];

We can see that handleswipefrom is the same method.

In this method, we can identify the specific direction of the light scanning gesture, for example, to determine the downward light scanning:

-(Void) handleswipefrom :( uiswipegesturerecognizer *) recognizer {
Nslog (@ "swipe received .");

If (recognizer. Direction = uiswipegesturerecognizerdirectiondown ){
Nslog (@ "swipe down ");

Check whether the scan is upward:

If (recognizer. Direction = uiswipegesturerecognizerdirectionup ){
Nslog (@ "swipe up ");

 The processing of animations, such as turning pages down (back), is similar to the following:

[Uiview beginanimations: @ "animationid" context: Nil];
[Uiview setanimationduration: 0.7f];
[Uiview setanimationcurve: uiviewanimationcurveeaseinout];
[Uiview setanimationrepeatautoreverses: No];
[Uiview setanimationtransition: uiviewanimationtransitioncurldown forview: Self. View cache: Yes];

[Currentview removefromsuperview];
[Self. View addsubview: contentview];

[Uiview commitanimations];

To flip the page up (forward), you only need to change:

[Uiview beginanimations: @ "animationid" context: Nil];
[Uiview setanimationduration: 0.7f];
[Uiview setanimationcurve: uiviewanimationcurveeaseinout];
[Uiview setanimationrepeatautoreverses: No];
[Uiview setanimationtransition:UiviewanimationtransitioncurlupForview: Self. View cache: Yes];

[Currentview removefromsuperview];
[Self. View addsubview: contentview];

[Uiview commitanimations];

 For an e-book, you also need to consider the problem of having multiple pages (images), such as 50 pages. A Data Structure is required to store the image paths of these pages:

    • Objc data structure, such as array
    • SQLite database table

In this way, write a set of flip pagesCodeIt can be decoupled from the loaded image.

In this example, an array is used, similar to the following:

Pages = [[nsarray alloc] initwithobjects: @ "1.jpg", @" 2.jpg", @ "3.jpg", @" 4.jpg", @ "5.jpg", @" 6.jpg ",
Nil];

Save the image to resources.

The following mechanism is used to locate the associated page when turning pages on the previous page and the next page:

    • Encapsulate the image as uiimageview display
    • You can set a tag value for uiimageview. The value is array subscript + 1.
    • In this way, the higher-level view can query uiimageview by TAG, for example, uiview * currentview = [self. View viewwithtag: currenttag];
    • Sets a member variable currenttag to save the current tag value.

For example, when an application is loaded, the first page is displayed:

currenttag = 1;
nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "pageflip1" oftype: @ "MP3"];
player = [[avaudioplayer alloc] initwithcontentsofurl: [nsurl fileurlwithpath: path] error: NULL];
// [[uiapplication sharedapplication] setstatushidbarden: yes animated: No];
[[uiapplication sharedapplication] setstatusbarhidden: Yes withanimation: uistatusbaranimationslide];
uiimageview * contentview = [uiimageview alloc] initwithframe: [[uiscreen mainscreen] applicationframe];
[contentview setimage: [uiimage imagenamed: [Pages objectatindex :( currentTag-1)];
[contentview setuserinteractionenabled: Yes];
contentview. tag = currenttag;

Processing during page turning:

If (currenttag <[Pages count]) {
Uiview * currentview = [self. View viewwithtag: currenttag];
Currenttag ++;

Uiimageview * contentview = [[uiimageview alloc] initwithframe: [uiscreen mainscreen] applicationframe];
[Contentview setimage: [uiimage imagenamed: [Pages objectatindex :( currentTag-1)];
[Contentview setuserinteractionenabled: Yes];
Contentview. Tag = currenttag;

[Uiview beginanimations: @ "animationid" context: Nil];
[Uiview setanimationduration: 0.7f];
[Uiview setanimationcurve: uiviewanimationcurveeaseinout];
[Uiview setanimationrepeatautoreverses: No];
[Uiview setanimationtransition: uiviewanimationtransitioncurlup forview: Self. View cache: Yes];

[Currentview removefromsuperview];
[Self. View addsubview: contentview];

[Uiview commitanimations];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.