iOS - layer 类似时钟指针转动

作者: 古流风 | 来源:发表于2016-03-10 16:21 被阅读839次

    第一篇博客,不知从何处下手。

    说下layer,先看下效果吧。

    layer-try2.gif

    CALayer就是UIView的视图层,你所看到的UIView,其实是UIView的layer,创建layer之后要添加到UIView上

    [self.view.layer addSublayer:self.layer1];

    下面是动画的的代码,关于北背景图片什么到底demo中会有。

    //动画载体   
    self.layer1 = [CALayer layer];  
    self.layer1.frame = CGRectMake(0, 0, 207, 207);
    self.layer1.position = CGPointMake(self.view.bounds.size.width/2+1,self.view.bounds.size.height/4+1);  
    [self.view.layer addSublayer:self.layer1];
    //设置动画代理   
     self.layer1.delegate = self;  
    //设置动画  
    [self.layer1 setNeedsDisplay];  
    //动画  作用在layer某个属性上,动画效果不会影响layer原有的属性值
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    //动画时长
    animation.duration = 3.0;
    //动画的初始值
    animation.fromValue = [NSNumber numberWithFloat:0];
     //动画的结束时的值
    animation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    //动画结束时的状态
    /*
      kCAFillModeForwards  保持结束时的状态
      kCAFillModeBackwards    回到开始时的状态
      kCAFillModeBoth        兼顾以上的两种效果
      kCAFillModeRemoved      结束时删除效果
    */
    animation.fillMode = kCAFillModeForwards;
    //动画的重复次数
    animation.repeatCount = HUGE_VALF;
    //开始动画  
    [self.layer1 addAnimation:animation forKey:@"rotation"];
    

    layer时CABasicAnimation还有一些其他的属性比如说:

    //延时执行
    animation1.beginTime = CACurrentMediaTime()+1;
    //设置动画速度
    animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    //动画翻转
    animation. autoreverses = NO;
    //动画完成时是否移除
    animation.removedOnCompletion = NO;
    

    layer还有一个属性要说一下:

    //动画运动时的锚点
     self.layertime.anchorPoint = CGPointMake(0.5, 0);
    

    anchorPoint 锚点
    称为“定位点”、“锚点”
    决定着CALayer身上的哪个点会在position属性所指的位置
    以自己的左上角为原点(0, 0)
    它的x、y取值范围都是0~1,默认值为(0.5, 0.5)
    当图形转动时,他会根据锚点转动

    我是一个分享者,详情请参考:http://www.cnblogs.com/wendingding/p/3800736.html 我是一个分享者,详情请参考:http://www.cnblogs.com/wendingding/p/3800736.html

    github:https://github.com/wolf3357/try-layer
    如有错误请指出,同时希望对您会有为帮助。

    相关文章

      网友评论

        本文标题:iOS - layer 类似时钟指针转动

        本文链接:https://www.haomeiwen.com/subject/dkutlttx.html