Example explains the catransition transitions animation using _ios in IOS

Source: Internet
Author: User


First, Introduction
Catransition is a subclass of caanimation that is used to animate transitions
The ability to provide a layer with an animated effect of moving out of the screen and moving it into the screen. iOS has less effect than Mac OS x transitions
such as: Uinavigationcontroller navigation controller is through the catransition transitions animation to achieve the controller's view into the screen animation effect






Catransition header File
Animation properties:
Type: Animation transition types
Subtype: Animation transition Direction
Startprogress: Animation starting point (percentage of overall animation)
Endprogress: Animation Endpoint (percentage of overall animation)
......


 code as follows:

#import <QuatzCore/CAAnimation.h>
Catransition *mytransition=[catransition animation];//Create catransition
mytransition.duration=0.3;//lasts 0.3 seconds.
mytransition.timingfunction=uiviewanimationcurveeaseinout;//timing function, fluency from beginning to end
mytransition.type=kcatransionpush;//Animation type
mytransition.subtype=kcatransitionfromleft;//Subtype
To make a transition effective, the group adds the animation to the layer that will become attached to the animated view. For example, for transitions between two view controllers, add animations to the layers of the window:
[[Self.view.superview layer]addanimation:mytransition Forkey:nil];
If you move a child view within a controller to another child view, the animation is added to the view controller's layer. There is also a choice to use the view within the view controller as an alternative to your child view as a child layer of the main views:
[Self.view.layer addanimation:mytransition Forkey:nil];
[Self.view Addsubview:newview];
[Oldview Removefromsuperview];
If you are using a navigation controller, you can add the animation to the visual map layer of the navigation controller.
[NavigationController.view.layer addanimation:mytransition Forkey:nil];

Transitions Animation Transition Effect








Two, the view class with the Transitions animation function
1, single view
+ (void) Transitionwithview: (uiview*) View Duration: (nstimeinterval) Duration options:
(uiviewanimationoptions) options
Animations: (void (^) (void)) animations
Completion: (void (^) (boolfinished)) completion;
Parameter description:
Duration: Duration of animation
View: Views that need to be animated for transitions
Options: type of transitions animation
Animations: Put the code that changes the View property in this block
Completion: This block is automatically invoked when the animation is finished
2, Dual view
+ (void) Transitionfromview: (uiview*) Fromview
Toview: (uiview*) Toviewduration: (Nstimeinterval) Durationoptions: (uiviewanimationoptions) options
Completion: (void (^) (boolfinished)) completion;
Parameter description:
Duration: Duration of animation
Options: type of transitions animation
Animations: Put the code that changes the View property in this block
Completion: This block is automatically invoked when the animation is finished
third, the application
Note:
Transitions Animation Use note point: The transition code must be written with the Transition animation code, otherwise invalid
1. Picture browsing
Example:






Code implementation


 code as follows:

#import "ViewController.h"





@interface Viewcontroller ()
@property (Weak, nonatomic) Iboutlet Uiimageview *imagev;



@end



@implementation Viewcontroller



Note: Transition animations use attention points: The transition code must be written with the Transition animation code, otherwise invalid
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
Implementation: Picture browsing



/** Transitions Code * *
static int index = 2;
NSString *imagename = [NSString stringwithformat:@ "%d", index];
_imagev.image = [UIImage imagenamed:imagename];
index++;



if (index = = 4) {
index = 1;
}



/** Transitions Animation Code * *
Create a Transitions Animation object
Catransition *anim = [catransition animation];



Set the type of transitions
Anim.type = @ "Pagecurl";



Set the direction of the animation
Anim.subtype = Kcatransitionfromleft;



Anim.duration = 3;



[_imagev.layer Addanimation:anim Forkey:nil];
}
@end



2, Icon 3D Flip:Implementation of the Transition animation function using UIView's own single view








Code implementation


 code as follows:

#import "ViewController.h"





@interface Viewcontroller ()



@property (Weak, nonatomic) Uiimageview *iconview;



@end



@implementation Viewcontroller



-(void) viewdidload{



[Super Viewdidload];




Uiimageview *iconview = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@ "1"]];
[Self.view Addsubview:iconview];
Iconview.center = Self.view.center;
Iconview.backgroundcolor = [Uicolor Greencolor];
Self.iconview = IconView;



Set to Fillet picture
Self.iconView.layer.cornerRadius = self.iconView.frame.size.width * 0.5;
Self.iconView.clipsToBounds = YES;
}



-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{



UIView animation of transitions from the belt
[UIView TransitionWithView:self.iconView duration:1.0 Options:uiviewanimationoptiontransitionflipfromleft animations:^{/** Perform left rollover animation, * *
Flip from left, before setting display picture 1, flip show Figure 2-"Picture 1 left flip to last show Picture 2"




Self.iconView.image = [UIImage imagenamed:@ "2"];



} completion:^ (BOOL finished) {



NSLog (@ "completion");



/** left rollover animation after the end, stop for 1 seconds, and then perform the right flip animation * *



Dispatch_after (Dispatch_time (Dispatch_time_now, int64_t) (1.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{//stop 1 seconds later , and then perform a right flip animation



[UIView TransitionWithView:self.iconView duration:1.0 Options:uiviewanimationoptiontransitionflipfromright animations:^{//Then, flip from right, flip show picture 1-"Picture 2 right Flip last show picture 1



Self.iconView.image = [UIImage imagenamed:@ "1"];



} Completion:nil];



});
}];
}
@end



3, view transitions between the animation:Implementation of transition animation function with UIView self-with two views







 code as follows:

#import "ViewController.h"





@interface Viewcontroller ()



@property (Weak, nonatomic) Iboutlet Uiscrollview *scrollview;



/**< ImageView1 * *
@property (nonatomic, strong) UIView *view1;



/**< IMAGEVIEW2 * *
@property (nonatomic, strong) UIView *view2;



@end



@implementation Viewcontroller



-(void) viewdidload{



[Super Viewdidload];



Self.view.backgroundColor = [Uicolor blackcolor];



1. ScrollView Add View2 child controls
UIView *view2 = [[UIView alloc] init];
View2.backgroundcolor = [Uicolor Greencolor];



[Self.scrollview Addsubview:view2];
Self.view2 = View2;



2. ScrollView Add View1 child controls
UIView *view1 = [[UIView alloc] init];
View1.backgroundcolor = [Uicolor Redcolor];



[Self.scrollview Addsubview:view1];
Self.view1 = View1;



3. Frame
CGFloat scrollvieww = self.scrollView.frame.size.width;
CGFloat SCROLLVIEWH = self.scrollView.frame.size.height;



View1.frame = CGRectMake (0, 0, SCROLLVIEWW, SCROLLVIEWH);
View2.frame = CGRectMake (0, 0, SCROLLVIEWW, SCROLLVIEWH); CGRectMake (scrollvieww, 0, SCROLLVIEWW, SCROLLVIEWH);



4. ScrollView
Self.scrollView.contentSize = Cgsizemake (Scrollvieww, SCROLLVIEWH);
Add gestures
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Tapclick:)];
[Self.scrollview Addgesturerecognizer:tap];
}



int i = 1;



Monitor to hear click ScrollView, flip Animation
-(void) Tapclick: (UITapGestureRecognizer *) tap{



if (i% 2!= 0) {



[UIView transitionFromView:self.view1 toView:self.view2 duration:1.0 options: Uiviewanimationoptiontransitionflipfromtop Completion:nil];



}else{
[UIView transitionFromView:self.view2 toView:self.view1 duration:1.0 options: Uiviewanimationoptiontransitionflipfrombottom Completion:nil];
}



i++;
}






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.