您的位置:首页 > 博客中心 > 互联网 >

CGPathAddArc

时间:2022-04-21 00:45

 

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

2012-12-04 15:38 775人阅读 评论(0) 收藏 举报 animationAnimationiosiOSIOSlayerpathPATHPathuiviewUIViewUIVIew [plain] view plaincopy
  1. float angle = 360 / 6;  
  2. for (int i = 0; i < 6; i++) {  
  3.     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  4.     float offsetAngle = angle * i;  
  5.     button.bounds = CGRectMake(0, 0, 32, 32);  
  6.     button.center = CGPointMake(cos(offsetAngle * RADIANS) * 160, sin(offsetAngle * RADIANS) * 160);  
  7.     button.tag = i;  
  8.     CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];  
  9.     CGMutablePathRef path = CGPathCreateMutable();  
  10.     pathAnimation.calculationMode = kCAAnimationPaced;  
  11.     pathAnimation.fillMode = kCAFillModeForwards;  
  12.     pathAnimation.removedOnCompletion = NO;  
  13.     pathAnimation.duration = 2;  
  14.     CGPathMoveToPoint(path, NULL, button.center.x, button.center.y);  
  15.     CGPathAddArc(path, NULL, 0, 0, 160, offsetAngle * M_PI/180, offsetAngle * M_PI/180 + M_PI, YES);  
  16.     pathAnimation.path = path;  
  17.     CGPathRelease(path);  
  18.     [button.layer addAnimation:pathAnimation forKey:@"curve"];  
  19.     [tabBarView addSubview:button];  
  20. }  

因为只是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:是否顺时针.

CGPathAddArc,布布扣,bubuko.com

本类排行

今日推荐

热门手游