iOS Core Animation详解(五)CATransition

欢迎关注我的iOS SDK详解专栏
http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html
不知不觉iOS的动画系列文章已经写到第五篇了,在这里,能方便的找到我之前写的四篇关于动画的文章。

前言:CATransition 是CAAnimation的子类,最主要的用途是用来定义view reload的转场动画。使用这个集成好的类,可以几行代码就创建出不错的效果。

效果一

动图

核心代码,

    CATransition * transition = [CATransition animation];
    transition.type = kCATransitionFade;
    transition.duration = 1.0;
    [self.imageview.layer addAnimation:transition forKey:@"ToNext"];
    self.imageview.image = nextImage;

完整的代码
要在先在Storyboard上拖拽一个imageview,并拖拽outlet

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageview;
@property (assign,nonatomic)NSInteger currentIndex;
@end

@implementation ViewController

-(NSArray *)imagesNamesArray{
    return @[@"meidui1.jpg",
             @"meidui2.jpg"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeToNextImage)];
    self.imageview.userInteractionEnabled = true;
    [self.imageview addGestureRecognizer:tap];
    self.currentIndex = 0;
}
-(void)changeToNextImage{
    self.currentIndex = (self.currentIndex + 1) % 2;
    NSString * imageName = [[self imagesNamesArray] objectAtIndex:self.currentIndex];
    UIImage * nextImage = [UIImage imageNamed:imageName];
    CATransition * transition = [CATransition animation];
    transition.type = kCATransitionFade;
    transition.duration = 1.0;
    [self.imageview.layer addAnimation:transition forKey:@"ToNext"];
    self.imageview.image = nextImage;
}

效果二

动图

iOS Core Animation详解(五)CATransition_第1张图片

代码


    CATransition * transition = [CATransition animation];
    transition.duration = 1.0;//动画间隔
    transition.type = kCATransitionMoveIn;//主要种类,决定动画效果
    transition.startProgress = 0.0;//开始
    transition.endProgress = 1.0;//结束
    transition.subtype = kCATransitionFromRight;//次要种类,决定动画方向
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];//时间函数
    [self.imageview.layer addAnimation:transition forKey:@"ToNext"];
    self.imageview.image = nextImage;

主要种类有四种

NSString * const kCATransitionFade; //消退
NSString * const kCATransitionMoveIn;//移入
NSString * const kCATransitionPush;//push
NSString * const kCATransitionReveal;//退出

次要种类也有四种

NSString * const kCATransitionFromRight;
NSString * const kCATransitionFromLeft;
NSString * const kCATransitionFromTop;
NSString * const kCATransitionFromBottom;

你可能感兴趣的:(ios,动画,transition)