CALayer 坐标转换的问题

rollrock1987 2014-08-01 11:17:36

如果所示
我需要实现的功能是 上面的圆圈的中心点 ,以中间的 (240,160)为中心点,围绕着半径为100的虚线圆圈做圆周运动。
数据如下:

上面ROUND在屏幕上的坐标:(220,40,40,40)
圆周运动R=100
圆周运动中心点 (240,160)

UIButton * _btn;
在viewDidLoad里面



在viewDidLoad里面
CGRect rect = CGRectMake(220,40,40,40);
_btn = [[UIButton alloc]initWithFrame:rect];
_btn.layer.cornerRadius = 40/2;

_btn.backgroundColor = [UIColor redColor];

[self.view addSubview:_btn];
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

-(void)startAnim
{
//
CGRect rect = CGRectMake(x, y, 100*2, 100*2);

CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation];
orbit.keyPath = @"position";
orbit.path = CFAutorelease(CGPathCreateWithEllipseInRect(rect, NULL));
orbit.duration = 4;
orbit.additive = YES;
orbit.repeatCount = HUGE_VALF;
orbit.calculationMode = kCAAnimationPaced;
orbit.rotationMode = kCAAnimationRotateAuto;

[_btn.layer addAnimation:orbit forKey:@"orbit"];
}



现在的疑问是 CGRect rect = CGRectMake(x, y, 100*2, 100*2); 里面的 x,y 应该怎么确定,不知道怎么算出来。
关于 layer的原点,锚点什么的 我都看过文档了,但是就是不对。
这里的x,y是相对于谁的桌标呢?是在self.view的坐标呢 还是 _btn的坐标?


...全文
206 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_18801591 2014-08-02
  • 打赏
  • 举报
回复
nmi hjsdasud [huydboashd;laskhdv;ksaldsda
tabsong 2014-08-01
  • 打赏
  • 举报
回复
创建CAKeyframeAnimation, 用CGPath生成一个圆的path, 就完事
Bannings 2014-08-01
  • 打赏
  • 举报
回复
x,y是相对于_btn的,你用x、y = -半径试试
zdm123892917 2014-08-01
  • 打赏
  • 举报
回复
x=-80 y=20 即是x=所求的240-(btn的位置设置220+100运动轨迹的半径值) 即是y=所求的160-(btn的位置设置40+100运动轨迹的半径值)
rollrock1987 2014-08-01
  • 打赏
  • 举报
回复
http://www.mibugs.com/2014/05/18/%E5%8A%A8%E7%94%BB%E8%A7%A3%E9%87%8A/
ReyZhang 2014-08-01
  • 打赏
  • 举报
回复
引用 5 楼 rollrock1987 的回复:
[quote=引用 3 楼 zhanglei5415 的回复:] 建议使用UIBezierPath来绘制动画路径
CGPoint arcCenter=CGPointMake(240,160);
int radius = 100;
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:arcCenter   radius:radius                                              startAngle:DEGREES_TO_RADIANS(270)
                                                      endAngle:DEGREES_TO_RADIANS(-90)
                                                     clockwise:NO];

CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation];
orbit.keyPath = @"position";
orbit.path =circlePath.CGPath;
orbit.duration = 4;
orbit.repeatCount = HUGE_VALF;
orbit.calculationMode = kCAAnimationPaced;
 orbit.rotationMode = kCAAnimationRotateAuto;
 
 [_btn.layer addAnimation:orbit forKey:@"orbit"];
谢谢 我还是想知道 我的方法里面 的 x y 应该怎么计算出来。[/quote] x= 240 - 100 y = 160 - 100
rollrock1987 2014-08-01
  • 打赏
  • 举报
回复
引用 3 楼 zhanglei5415 的回复:
建议使用UIBezierPath来绘制动画路径
CGPoint arcCenter=CGPointMake(240,160);
int radius = 100;
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:arcCenter   radius:radius                                              startAngle:DEGREES_TO_RADIANS(270)
                                                      endAngle:DEGREES_TO_RADIANS(-90)
                                                     clockwise:NO];

CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation];
orbit.keyPath = @"position";
orbit.path =circlePath.CGPath;
orbit.duration = 4;
orbit.repeatCount = HUGE_VALF;
orbit.calculationMode = kCAAnimationPaced;
 orbit.rotationMode = kCAAnimationRotateAuto;
 
 [_btn.layer addAnimation:orbit forKey:@"orbit"];
谢谢 我还是想知道 我的方法里面 的 x y 应该怎么计算出来。
scunet 2014-08-01
  • 打赏
  • 举报
回复
参考这个,绘制 http://nachbaur.com/blog/core-animation-part-4
ReyZhang 2014-08-01
  • 打赏
  • 举报
回复
建议使用UIBezierPath来绘制动画路径
CGPoint arcCenter=CGPointMake(240,160);
int radius = 100;
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:arcCenter   radius:radius                                              startAngle:DEGREES_TO_RADIANS(270)
                                                      endAngle:DEGREES_TO_RADIANS(-90)
                                                     clockwise:NO];

CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation];
orbit.keyPath = @"position";
orbit.path =circlePath.CGPath;
orbit.duration = 4;
orbit.repeatCount = HUGE_VALF;
orbit.calculationMode = kCAAnimationPaced;
 orbit.rotationMode = kCAAnimationRotateAuto;
 
 [_btn.layer addAnimation:orbit forKey:@"orbit"];
rollrock1987 2014-08-01
  • 打赏
  • 举报
回复
引用 1 楼 zhangao0086 的回复:
x,y是相对于_btn的,你用x、y = -半径试试
哥能说详细点么 或者有木有文档之类的

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧