2010-05-28 44 views
7

以下代碼爲動作提供了動畫,即使我沒有使用beginAnimations:context。我如何在沒有動畫的情況下移動它?這是一個新的iPhone視圖項目,這是它的唯一更新。在移動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]; 
} 

回答

14

做到這一點最簡單的方法是通過將位置代碼放到一個事務覆蓋動畫時間:

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

或'setDisableActions:YES' – Sulthan 2012-10-03 21:26:13

4

更簡單的方法:

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