目录:UIView动画 子视图翻转动画

UIImageView动画
CATransition动画
Core Graphics绘图:(线、矩形、曲

线、文字、图片)

CALayer

       核心动画:(关键帧动画、
            单一动画、

组动画)


1.UIView动画

(1)设置代理(viewController)

动画开始: beginAnimations

设置动画加减速方式: setAnimationCurve

设置持续时长: setAnimationDuration

设置动画重复次数: setAnimationRepeatCount

设置动画是否做反向操作: setAnimationRepeatAutoreverses:YES

设置视图的变化,提交动画 :[UIView commitAnimations];


(2)Block动画

eg:

[UIView animateWithDuration:2

                     animations:^{

                        baseView.alpha = 0;

                     } completion:^(BOOL finished) {

                         NSLog(@"end");

                     }];


2.UIImage动画

①创建 UIImageView对象

②创建一个数组,存放多个UIImage对象

③为UIImageView对象设置动画数组:imgView.animationImagesmArr;

④设置时长,开始动画:startAnimating


3.CATransition动画

①创建 CATransition对象

②设置对象属性(时长Duration、速率TimingFunction、类型type、方向subType)

类型有私有API:

//    animation.type = @"cube"             //立方体效果

//    animation.type = @"suckEffect"       //收缩效果,如一块布被抽走
//    animation.type = @"oglFlip"          //翻转效果
//    animation.type = @"rippleEffect"     //滴水效果
//    animation.type = @"pageCurl"         //向上翻一页

//    animation.type = @"pageUnCurl"       //向下翻一页


③为UIView对象添加动画,添加到它的Layer上

为导航栏添加动画(立方体旋转效果)

[self.navigationController.view.layer addAnimation:transition forKey:@"animation"];


4.绘图

调用父类方法,程序运行时自动调用

- (void)drawRect:(CGRect)rect{   

    [super drawRect:rect];

绘画不同图形,都需要先获取画布

//图像上下文,获取画布,他是一个结构体指针

    CGContextRef context = UIGraphicsGetCurrentContext();

都可以设置画笔属性:

//设置虚线
   
CGFloat arr[] = {10,5};
   
CGContextSetLineDash(context, 0, arr, 2);
   
   
//设置画笔的宽度
   
CGContextSetLineWidth(context, 2);
   
   
//设置画笔颜色
   
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
   
   
//设置填充色

    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);

最后都要保存,添加到画布

    //保存路径,添加绘图
   
CGContextDrawPath(context, kCGPathEOFillStroke);

(1)画直线

//获取画笔,设置起点
   
CGContextMoveToPoint(context, 20, 20);
   
   
//移动画笔(可多次移动,连接成不同图形)

    CGContextAddLineToPoint(context, 220, 20);

(2)画矩形

//绘制矩形

    CGContextAddRect(context, CGRectMake(20, 20, 100, 100));

(3)画曲线

//    //绘制贝塞尔曲线1
//    CGContextMoveToPoint(context, 10, 100);

//    CGContextAddCurveToPoint(context, 55, 0, 145, 200, 200, 100);

//    //绘制贝塞尔曲线2
//    CGContextMoveToPoint(context, 10, 200);
//    CGContextAddQuadCurveToPoint(context, 105, 0,200, 200)  ;

   

//绘制扇形

    CGContextAddArc(context, 100, 100, 100, arc(90), arc(0), 1);


(4)画图像(Image)

可使用PrintCode软件画,自动生成代码,粘贴过去


5.绘制阴影

创建UIView对象

设置圆角(必须是layer层)

    view.layer.cornerRadius = 10;

    view.layer.masksToBounds = YES;

***设置圆角时不会显示阴影

可以设置边框,阴影颜色( view.layer.shadowColor)、阴影透明度( view.layer.shadowOpacity)、图片( view.layer.contents

6.关键帧动画

创建路径,在路径上添加运动轨迹

//路径
   
CGMutablePathRef path = CGPathCreateMutable();
   
   
//为路径添加一个运动轨迹

    CGPathAddRect(path, nil, CGRectMake(10, 10, 200, 200));

创建关键帧动画

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

设置动画的加减速方式、时长、重复次数、路径( animation.path = path;

创建单一动画 CABasicAnimation对象,旋转

basicAnimation.fromValue = @0;

    basicAnimation.toValue = @(2 * M_PI);



设置组动画,可以将上面创建的关键帧动画和单一动画添加进去,如下,实现两种动画同时进行

//组动画
   
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
   
   
//往组里添加两个动画
    animationGroup.
animations = @[animation,basicAnimation];
   
    animationGroup.
duration = 2;
    animationGroup.
repeatCount = 1000;
   
    [
lView.layer addAnimation:animationGroup forKey:@"animationGroup"];