Animation preliminary study five -Core Animation: Animation _ transitions from one scene to another scene in the form of transition animation

Transition animation is the transition from one scene to another scene in the form of animation. Use transition animations are generally divided into the following steps:

1. Create animated transitions

2. Set the transition type, sub-type (optional), and other properties

3. Set the new view of the transitions and add animation to the layer

The following table lists the common transition type (note that Apple's proprietary API official did not disclose the type of animation, but there can still be used by):

Animation type Explanation The corresponding constants Support direction
Open API      
fade Fade-out effect kCATransitionFade Yes
movein Move to a new view on the old view kCATransitionMoveIn Yes
push The introduction of a new view of the old view kCATransitionPush Yes
reveal Remove the old view shows a new view kCATransitionReveal Yes
Private API   Private API access only through a string  
cube Cube rollover effect no Yes
oglFlip Rollover effect no Yes
suckEffect Shrinkage effect no no
rippleEffect Drop ripple effect no no
pageCurl Up flip effect no Yes
pageUnCurl Down Effect no Yes
cameralIrisHollowOpen Open the camera effect no no
cameraIrisHollowClose Camera Effect Off no no

In addition to support the direction of the type of animation also contain sub-types:

Animation subtype Explanation
kCATransitionFromRight Transition from the right
kCATransitionFromLeft Transition from the left
kCATransitionFromTop From the top of the transition
kCATransitionFromBottom Transition from the bottom

In the previous article " IOS development series - infinite loop image viewer " in order to use UIScrollView do an infinite loop picture viewer spent a lot of time in the above performance optimization, where the use of transition animations use a UIImageView achieve a beautiful infinity circulation picture viewer.

//
//  KCMainViewController.m
//  TransitionAnimation
//
//  Created by Kenshin Cui on 14-3-12.
//  Copyright (c) 2014年 Kenshin Cui. All rights reserved.
//

#import "KCMainViewController.h"
#define IMAGE_COUNT 5

@interface KCMainViewController (){
    UIImageView *_imageView;
    int _currentIndex;
}

@end

@implementation KCMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //定义图片控件
    _imageView=[[UIImageView alloc]init];
    _imageView.frame=[UIScreen mainScreen].applicationFrame;
    _imageView.contentMode=UIViewContentModeScaleAspectFit;
    = _imageView.image [UIImage imageNamed: @ "0.jpg" ]; // default picture
     [self.view addSubview: _imageView];
     // add gesture
     UISwipeGestureRecognizer * leftSwipeGesture = [[UISwipeGestureRecognizer alloc ] initWithTarget: self action: @selector ( :) leftSwipe]; 
    leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft; 
    [self.view addGestureRecognizer: leftSwipeGesture]; 
    
    UISwipeGestureRecognizer rightSwipeGesture * = [[UISwipeGestureRecognizer the alloc] initWithTarget: Self Action: @selector (rightSwipe :)]; 
    rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight; 
    [self.view addGestureRecognizer:rightSwipeGesture];
} 

# pragma Mark swipe to the next image to the left 
- ( void) leftSwipe: (UISwipeGestureRecognizer *) Gesture { 
    [Self transitionAnimation: YES]; 
} 

#pragma Mark right swipe on an image 
- ( void ) rightSwipe: (UISwipeGestureRecognizer *) Gesture { 
    [Self transitionAnimation: NO]; 
} 


#pragma mark the transition animation 
- ( void ) transitionAnimation: (BOOL) isNext {
     // create transitions 1 animated objects.
     CATransition * Transition = [[CATransition alloc] the init]; 
    
    // set animation type 2, Apple officials did not pay attention to the public. animation only string types, and the corresponding constants do not define
     transition.type = @ "Cube" ; 
    
    // set subtype
     IF  (isNext) {
        transition.subtype = kCATransitionFromRight; 
    } the else {
        = kCATransitionFromLeft transition.subtype; 
    } 
    // set the animation often
     transition.duration = 1.0f; 
    
    // set the transition. 3 after adding the new view animated transitions.
     _imageView.image = [Self the getImage: isNext]; 
    [_imageView.layer the addAnimation : Transition forKey: @ "KCTransitionAnimation" ]; 
} 

#pragma Mark obtain the current picture 
- (the UIImage *) the getImage: (BOOL) isNext { 
    IF (isNext) { 
        _currentIndex = (_ the currentIndex +. 1)% IMAGE_COUNT; 
    } the else { 
        _currentIndex = ( + IMAGE_COUNT. 1-_currentIndex)% IMAGE_COUNT;  
    }
    NSString imageName * = [NSString stringWithFormat: @ "% I.jpg" , _currentIndex];
     return[UIImage imageNamed:imageName];
}
@end

running result:

TransitionAnimationEffect

The code is very simple, but the effect is very striking, and performance. Of course, limited demo code, we can own other types of animation to achieve, the effect is very brilliant.


Published 40 original articles · won praise 10 · views 30000 +

Guess you like

Origin blog.csdn.net/ai_pple/article/details/50979148