0

如何像摇摆一样为 UIView 或 UILable 设置动画

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 或标签需要从左到右和从右到左移动。

@提前致谢

4

2 回答 2

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];  
}
于 2012-06-25T09:28:38.647 回答
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.
于 2012-06-25T09:28:25.490 回答