移动CALayers时禁用动画

时间:2010-05-28 15:10:45

标签: iphone objective-c core-animation

以下代码为动作设置动画,即使我没有使用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 个答案:

答案 0 :(得分:14)

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

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

答案 1 :(得分:4)

更简单的方式:

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