禁用CALayer移动时的动画效果

7
以下代码实现了移动效果,但我没有使用beginAnimations:context。如何使其在不进行动画的情况下移动?这是一个新的iPhone视图项目,这些是对它唯一的更新。
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    sublayer = [CALayer new];
    sublayer.backgroundColor = [[UIColor redColor] CGColor];
    sublayer.frame = CGRectMake(0, 0, 100, 100);

    [self.view.layer addSublayer:sublayer];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    sublayer.position = [[touches anyObject] locationInView:self.view];
}
2个回答

14

最简单的方法是通过将位置代码放入事务中来覆盖动画持续时间:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [CATransaction begin];
    [CATransaction setAnimationDuration:0];
    sublayer.position = [[touches anyObject] locationInView:self.view];
    [CATransaction commit];
}

4
更简单的方式:

sublayer.position = [[touches anyObject] locationInView:self.view];
[sublayer removeAnimationForKey:@"position"];

注:以上代码涉及it技术,无法进行解释。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接