Ios:uiview block function for transition animation---dual view

Source: Internet
Author: User

animating transitions using the UIView animation function--dual View+ (void) Transitionfromview: (UIView *) Fromview Toview: (UIView *) Toview Duration: (nstimeinterval) Duration options :(uiviewanimationoptions) options completion: (void (^) (BOOL finished)) completion; parameter Description:–duration: Duration of animation–options: Types of transition animations–animations: Place code that changes the properties of the view in this block–completion: This block is automatically called after the animation is finished 

After the method call is complete, the following two lines of code are executed:

Add Toview to Parent view

[Fromview.superview Addsubview:toview];

Remove the Fromview from the parent view

[Fromview Removefromsuperview];

The concrete examples are as follows:

Implementation features: Create two sub-views View1 and View2, set different background colors, add them to the parent view, and create touch-click gestures,

Each time you touch the screen, two views alternately toggle the display, that is, to achieve the transition animation.

The code is as follows:

declaring properties

#import " ViewController.h " @interface  **view2; @end

Initialize, create two sub-views, create touch tap gestures and add gesture events

- (void) viewdidload {[Super viewdidload]; //InitializeSelf.view1 =[[UIView alloc]initwithframe:self.view.frame]; Self.view1.backgroundColor=[Uicolor Redcolor];    //view1 background color is red        [Self.view AddSubview:self.view1]; Self.view2=[[UIView alloc]initwithframe:self.view.frame]; Self.view2.backgroundColor=[Uicolor Greencolor];    //view2 background color is green            [Self.view AddSubview:self.view2]; //Add tap gesturesUITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (tap:)]; Tap.numberoftapsrequired=1; Tap.numberoftouchesrequired=1; [Self.view Addgesturerecognizer:tap];}

Handle touch-click Gesture events and implement two-view transition animations in the block function

#pragma mark-tap gesture Events-(voidTap: (UITapGestureRecognizer *) sender{//by judging whether the view's parent view is empty, you can tell if the current view is showing    if(Self.view2.superview = =Nil) {        //through the block functionToggle Sub-view (set animation transition type to book effect)[UIView transitionFromView:self.view1 toView:self.view2 Duration:1.0fOptions:uiviewanimationoptiontransitioncurlup Completion:nil];; }    Else    {        //through the block functionToggle Sub-view (set animation transition type to book effect)[UIView transitionFromView:self.view2 toView:self.view1 Duration:1.0fOptions:uiviewanimationoptiontransitioncurlup Completion:nil]; }}

The demo results are as follows:

At the beginning:

After clicking:

After the animation ends:

You can then click on it and it will turn into a green view, not a demonstration.

Ios:uiview block function for transition animation---dual view

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.