2012-06-25 49 views
0

如何動畫動畫的UIView或UILable像鞦韆如何動畫標籤或類似的UIView擺動

CGPoint newLeftCenter = CGPointMake(00.0f + label.frame.size.width/2.0f, label.center.y); 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0f]; 
label.center = newLeftCenter; 
[UIView commitAnimations]; 

這將動畫從左至右!

我需要從右向左動畫!

所以當我的動作觸發它時,UIVIew或標籤需要從左到右和從右到左移動。

@預先感謝

回答

1

如果你的意思是你想使用setAnimationDidStopSelector

- (void) animationDidStop:(NSString *)animationID 
       finished:(NSNumber *)finished 
        context:(void *)context 
{ 
    CGPoint newRightCenter = CGPointMake(00.0f + label.frame.size.width * 2, label.center.y); 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:1.0f]; 
    label.center = newRightCenter; 
    [UIView commitAnimations]; 
} 

- (void) doAnimation 
{ 
    CGPoint newLeftCenter = CGPointMake(00.0f + label.frame.size.width/2.0f, label.center.y); 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:1.0f]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

    label.center = newLeftCenter; 
    [UIView commitAnimations]; 
} 
+0

@ OmarAbdelhafith由於其工作對我來說將兩個連續的動畫,你可以做到以下幾點但動畫正在快速發展!當它從左到右動畫,然後從右向左移動它的動畫快! [或快速移動] – kiran

0

您可以通過將動畫自動反向設置爲YES來完成此操作。

[UIView setAnimationRepeatAutoreverses:YES]; 

從蘋果的文件:

If you enable autoreversing, a single animation cycle changes the properties being animated to their new values and then back to their original values. At the end of the animations, the affected views are then updated immediately to reflect the new values. This method does nothing if called from outside of an animation block. By default, autoreversing is disabled. 

If you combine autoreversing with a repeat count (settable using the setAnimationRepeatCount: method), you can create animations that shift back and forth between the old and new values the specified number of times. However, remember that the repeat count indicates the number of complete cycles. If you specify an integral value such as 2.0, the animation ends on the old value, which is followed by the view immediately updating itself to show the new value, which might be jarring. If you want the animation to end on the new value (instead of the old value), add 0.5 to the repeat count value. This adds an extra half cycle to the animation. 

Use of this method is discouraged in iOS 4.0 and later. Instead, you should use theanimateWithDuration:delay:options:animations:completion: method to specify your animations and the animation options.