IOS开发动画总结

iOS开发中常用的动画方式一共有两种,UIView动画效果,还有核心动画。当然,还有另外一种叫做隐式动画,后续随手会介绍隐式动画。这里主要介绍UIView动画效果和简单的核心动画的使用。

1. 直接使用UIView的动画

    [UIView beginAnimations:nil context:nil];

    /*

     使用set方法设置动画的属性

     */

    //设置动画的时间

    [UIView setAnimationDuration:2.0];

    //动画的代理方法中,还可以监听动画的完成

    [UIView setAnimationDelegate:self];

    [UIView setAnimationDidStopSelector:@selector(stop)];

    [UIView setAnimationRepeatCount:5];

    //动画效果

    self.imageView.transform = CGAffineTransformMakeTranslation(100, 100);

    //完成编辑动画

    [UIView commitAnimations];

2. UIView动画的代码块方式

[UIView animateWithDuration:2.0 animations:^{

        self.imageView.center = CGPointMake(200, 200);

    } completion:^(BOOL finished) {

        NSLog(@"动画完成");

    }];

3.UIView自带专场动画

[UIView transitionWithView:self.imageView duration:2.0 options:UIViewAnimationOptionTransitionCurlUp animations:^{

        self.imageView.image = [UIImage imageNamed:@"5"];

    } completion:^(BOOL finished) {

        NSLog(@"动画完成3");

    }];

4.核心动画(核心动画是添加到图层上面的动画效果)

//这是一个组动画的应用效果

//核心动画只是一个假象,动画执行完之后相应的属性还是没变,要想保持动画之后的属性,应该在动画结束后的代码上设置,有两种方式,一种是直接在动画代码结束后的位置设置,一种是在代理方法中,有一个动画结束之后的方法

    CAAnimationGroup *group = [CAAnimationGroup animation];

        //设置代理

//    group.delegate = self;

    

    //1.帧动画

    CAKeyframeAnimation *key = [CAKeyframeAnimation animation];

    key.keyPath = @"position";

    

    //创建path

    CGMutablePathRef path = CGPathCreateMutable();

    

    CGPathAddEllipseInRect(path, NULL, CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.width));

    /*

    //如果要现实的话需要将path添加到图形上下文,和渲染,这里只需要一个路径,所以不用渲染

    CGContextAddPath(<#CGContextRef context#>, <#CGPathRef path#>);

    CGContextStrokePath(<#CGContextRef c#>);

     */

    //使用path的缺点:不能指定起始路径和结束路径,(2016.1.5)

    key.path = path;

    

    //旋转动画

    CABasicAnimation *basic = [CABasicAnimation animation];

    basic.keyPath = @"transform.rotation";

    

    basic.byValue = @(M_PI_4);

    

    self.imageView.image = [UIImage imageNamed:@"5"];

    //转场动画

    CATransition *cirani = [CATransition animation];

    

    //属性

    cirani.type = kCATransitionMoveIn;

    cirani.subtype = kCATransitionFromRight;

    

    //添加到组动画

    group.animations = @[key,basic,cirani];

    group.duration = 3;

    

    //添加到view

    [self.imageView.layer addAnimation:group forKey:nil];

    

}

// CAAnimation的代理方法,CAAnimation动画执行完成之后的方法

- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

{

    self.imageView.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);

    self.imageView.layer.position = CGPointMake(self.view.bounds.size.width, self.view.bounds.size.width * 0.5 + 100);

    NSLog(@"动画完成4");

}

原文地址:https://www.cnblogs.com/xiaofei993/p/5314685.html