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