Ios 是CABASICanization';相对于初始位置的tos值?
我有一个Ios 是CABASICanization';相对于初始位置的tos值?,ios,objective-c,core-animation,cabasicanimation,Ios,Objective C,Core Animation,Cabasicanimation,我有一个CALayer,我正在显式地将其设置为新位置的动画。我的toValue属性似乎仅与层的初始位置相关 CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"]; move.fromValue = [NSValue valueWithCGPoint:CGPointMake(blueDot.position.x, blueDot.position.y)]; move.toVa
CALayer
,我正在显式地将其设置为新位置的动画。我的toValue
属性似乎仅与层的初始位置相关
CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"];
move.fromValue = [NSValue valueWithCGPoint:CGPointMake(blueDot.position.x, blueDot.position.y)];
move.toValue = [NSValue valueWithCGPoint:CGPointMake(self.view.center.x, self.view.center.y)];
move.duration = 1.0;
[blueDot addAnimation:move forKey:@"myMoveAnimation"];
blueDot.position = CGPointMake(self.view.center.x, self.view.center.y);
因此,此代码不会将图层重绘到视图的中心。似乎它真的在将层重新绘制为CGPointMake(self.view.center.x+blueDot.position.x,self.view.center.y+blueDot.position.y)
,就像是将self.view.center
的值添加到层的起始位置
toValue
属性的行为应该是这样的吗?将我的蓝点移动到视图中心最干净的方法是什么
编辑:
-(void)viewDidLoad {
[super viewDidLoad];
blueDot = [CAShapeLayer layer];
[blueDot setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(self.view.center.x - 60 - 5, 163.5, 10, 10)] CGPath]];
[blueDot setStrokeColor: [UIColor blueColor]CGColor];
[blueDot setFillColor:[UIColor blueColor]CGColor];
[[self.view layer] addSublayer:blueDot];
}
// 163.5 is a placeholder for another view's position value so it's more readable for you guys.
您可以在
视图
的superview的坐标系中将设置为value
(因为使用了中心
属性)。看起来这样应该行得通:
CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"];
move.fromValue = [NSValue valueWithCGPoint:CGPointMake(blueDot.position.x, blueDot.position.y)];
move.toValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds))];
move.duration = 1.0;
[blueDot addAnimation:move forKey:@"myMoveAnimation"];
blueDot.position = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));
您可以在
视图
的superview的坐标系中将设置为value
(因为使用了中心
属性)。看起来这样应该行得通:
CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"];
move.fromValue = [NSValue valueWithCGPoint:CGPointMake(blueDot.position.x, blueDot.position.y)];
move.toValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds))];
move.duration = 1.0;
[blueDot addAnimation:move forKey:@"myMoveAnimation"];
blueDot.position = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));
问题是您正在移动层的
帧
,但是该层的蓝点路径
已在该层的帧
内被self.view.center.x-60-5163.5
偏移,为该CAShapeLayer
创建路径时指定的坐标。因此,当您设置层的位置
的动画时,蓝点仍将从新的位置
偏移该量
修复方法是将CAShapeLayer
的路径定义在该层的左上角,例如
CAShapeLayer *blueDot = [CAShapeLayer layer];
blueDot.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(5, 5) radius:5 startAngle:0 endAngle:M_PI * 2 clockwise:true].CGPath; // note that using arc often generates a better looking circle, probably not noticeable at this size, just probably good practice
blueDot.fillColor = [UIColor blueColor].CGColor;
现在,当您移动位置时,蓝点的原点将移动到该位置
另外,我通常会创建一个单独的UIView
,并将这个CAShapeLayer
添加到该视图中。这样,您可以享受各种不同的功能:
- 您可以欣赏
UIView
基于块的动画李>
- 您可以使用
center
移动它,这样当您将其center
移动到其superview的center
时,它将真正居中(现在,如果您将图层的位置移动到self.view.center
,它实际上并不完全居中,因为它将是蓝点图层的左上角,位于中间)
- 您可以使用UIKit Dynamics将其捕捉到位置或定义其他行为
- 如果需要,可以使用自动布局约束指定放置
- 您可以定义一个
UIView
子类,该子类是IBDesignable
,使用此点作为视图,以便您可以将其添加到故事板上并查看其外观;等等
问题是您正在移动层的帧
,但此现金层
的蓝点路径
已在该层的帧
内偏移了self.view.center.x-60-5163.5
,即为该现金层创建路径
时指定的坐标ode>。因此,当您设置层的位置
动画时,蓝点仍将从新的位置
偏移该量
修复方法是将CAShapeLayer
的路径定义在该层的左上角,例如
CAShapeLayer *blueDot = [CAShapeLayer layer];
blueDot.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(5, 5) radius:5 startAngle:0 endAngle:M_PI * 2 clockwise:true].CGPath; // note that using arc often generates a better looking circle, probably not noticeable at this size, just probably good practice
blueDot.fillColor = [UIColor blueColor].CGColor;
现在,当您移动位置时,蓝点的原点将移动到该位置
另一方面,我通常会创建一个单独的UIView
并将此CAShapeLayer
添加到该视图中。这样,您可以享受各种不同的功能:
- 您可以欣赏
UIView
基于块的动画
- 您可以使用
center
移动它,这样当您将其center
移动到其superview的center
时,它将真正居中(现在,如果您将图层的位置移动到self.view.center
,它实际上并不完全居中,因为它将是蓝点图层的左上角,位于中间)
- 您可以使用UIKit Dynamics将其捕捉到位置或定义其他行为
- 如果需要,可以使用自动布局约束指定放置
- 您可以定义一个
UIView
子类,该子类是IBDesignable
,使用此点作为视图,以便您可以将其添加到故事板上并查看其外观;等等
此外,如果您只想更改位置,似乎可以使用隐式动画来完成所有操作。@dperk层隐式动画不是这样做的–这是用于UIView
动画。您想在CATransaction
块内执行隐式动画。即[CATransaction begin];[CATTransaction设置动画持续时间:1.0];[CATTransaction提交];
@originaluser2,对于支持隐式动画的层属性,您根本不需要CATTransaction开始/结束。您只需更改属性,并使用默认设置对更改进行动画设置。只有在需要更改默认动画设置(缓和、持续时间等)时,才需要CATTransaction调用隐式动画仅适用于层属性,而不适用于视图属性。这是关键。@Duncan确定您不需要CATTransaction
如果您对默认设置满意,我只是将OP的显式动画转换为隐式动画:)此外,如果您只想更改位置,似乎可以使用隐式动画来完成所有操作。@dperk层隐式动画不是这样做的–这是用于ui查看动画。您想在CATransaction
块内进行隐式动画。即[CATransaction begin];[CATTransaction设置动画持续时间:1.0];[CATTransaction提交];
@originalu