2010-05-28 14 views
7

El siguiente código anima el movimiento, aunque no usé beginAnimations:context. ¿Cómo hago para que se mueva sin animar? Este es un nuevo proyecto de vista de iphone, y estas son las únicas actualizaciones.Deshabilitar animación al mover CALayers

// 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]; 
} 

Respuesta

14

La forma más sencilla de hacerlo es para anular la duración de la animación, poniendo el código de posición en una transacción:

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

o 'setDisableActions: SÍ' – Sulthan

4

Más manera más simple:

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

Cuestiones relacionadas