Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable animation when moving CALayers

The following code animates the movement, even though I didn't use beginAnimations:context. How do I get it to move without animating? This is a new iphone view project, and these are the only updates to it.

// 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];
}
like image 442
Sean Clark Hess Avatar asked May 28 '10 15:05

Sean Clark Hess


People also ask

How do I stop background animation?

To remove all animation effects from text or an object, click the object that you want to stop animating. Then, on the Animations tab, in the gallery of animation effects, click None.


1 Answers

The simplest way to do it is to override the animation duration by putting the position code into a transaction:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [CATransaction begin];
    [CATransaction setAnimationDuration:0];
    sublayer.position = [[touches anyObject] locationInView:self.view];
    [CATransaction commit];
}
like image 174
Laurent Etiemble Avatar answered Oct 01 '22 06:10

Laurent Etiemble