2013-02-11 72 views
0

Im新增核心動畫。每次我調用動畫時,圖像都會從其位置移動到屏幕的左上角,然後移動,然後返回到原始位置。如何讓圖像移動50個單位,停止,然後重複,如果再次按下按鈕。 代碼:動畫調用後UIImage視圖重置

-(IBAction)preform:(id)sender{ 

CGPoint point = CGPointMake(50, 50); 

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"]; 
anim.fromValue = [NSValue valueWithCGPoint:point]; 
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(point.x + 50, point.y)]; 
anim.duration = 1.5f; 
anim.repeatCount =1; 
anim.removedOnCompletion = YES; 
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 
[imView.layer addAnimation:anim forKey:@"position"]; 

} 

回答

1

對於你的情況,你可以跳過重CA機器和使用塊動畫

// Your image is at starting position 
[UIView animateWithDuration:1.5 animations:^{ 
     CGRect newFrame = CGRectOffset(imView.frame, 50, 0); 
     imView.frame = newFrame; 
    }]; 

但是,如果你想知道如何處理這個「彈回」正常,檢查this url