【发布时间】:2023-04-08 17:58:01
【问题描述】:
我有一个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 的float 值添加到图层的起始位置一样。
toValue 属性应该是这样的吗?将我的 blueDot 移动到视图中心的最干净的方法是什么?
编辑:
-(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.
【问题讨论】:
-
另外,如果您只想改变位置,似乎这一切都可以使用隐式动画来完成。
-
@dperk 这不是你做层隐式动画的方式——那是
UIView动画。你想在CATransaction块内做你的隐式动画。即[CATransaction begin]; [CATransaction setAnimationDuration:1.0]; <your changes to the layer> [CATransaction commit]; -
@originaluser2,对于支持隐式动画的图层属性,您根本不需要 CATransaction 开始/结束。您只需更改属性,更改就会使用默认设置进行动画处理。仅当您需要更改默认动画设置(缓动、持续时间等)时才需要 CATransaction 调用
-
隐式动画仅适用于 LAYER 属性,不适用于视图属性。这是关键。
-
@DuncanC 当然,如果您对默认设置感到满意,您肯定不需要
CATransaction,我只是将 OP 的显式动画转换为隐式动画 :)
标签: ios objective-c core-animation cabasicanimation