欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

iOS Core Animation详解(五)CATransition_html/css_WEB-ITnose

程序员文章站 2022-04-23 12:24:08
...
欢迎关注我的iOS SDK详解专栏

不知不觉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;}

效果二

动图

代码

    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;//pushNSString * const kCATransitionReveal;//退出

次要种类也有四种

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