使用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:是否顺时针.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值