当前位置:编程学习 > 网站相关 >>

使用CGPathAddArc使UIView的Layer绕中心点旋转

  float angle = 360 / 6;
        for (int i = 0; i < 6; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            float offsetAngle = angle * i;
            button.bounds = CGRectMake(0, 0, 32, 32);
            button.center = CGPointMake(cos(offsetAngle * RADIANS) * 160, sin(offsetAngle * RADIANS) * 160);
            button.tag = i;
            CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            CGMutablePathRef path = CGPathCreateMutable();
            pathAnimation.calculationMode = kCAAnimationPaced;
            pathAnimation.fillMode = kCAFillModeForwards;
            pathAnimation.removedOnCompletion = NO;
            pathAnimation.duration = 2;
            CGPathMoveToPoint(path, NULL, button.center.x, button.center.y);
            CGPathAddArc(path, NULL, 0, 0, 160, offsetAngle * M_PI/180, offsetAngle * M_PI/180 + M_PI, YES);
            pathAnimation.path = path;
            CGPathRelease(path);
            [button.layer addAnimation:pathAnimation forKey:@"curve"];
            [tabBarView addSubview:button];
        }
因为只是Layer的内容发生的位置变化,其实UIView还是在原来的位置上,如果需要使用应该在Animation结束后重新设定UIView的位置.


顺便翻译一下原文:

CGPathAddArc (

   CGMutablePathRef path,

   const CGAffineTransform *m,

   CGFloat x,

   CGFloat y,

   CGFloat radius,

   CGFloat startAngle,

   CGFloat endAngle,

   bool clockwise

);

path:动画的路径;

m:layer的transform;

x:其实x和y是什么我还没弄明白,根据原文应该是弧的圆心点,但我设置过,这更像是偏移量;

y:同上;

startAngle:起始的角度点,零度是与x轴相交点,度数为顺时针;
endAngle:结束的角度点;

clockwis:是否顺时针.

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,