iOS - 标签或图像的动画移动

时间:2010-09-08 01:21:53

标签: animation ios4 ios

如何设置标签或图像的移动动画?我只是想从屏幕上的一个位置慢慢过渡到另一个位置(没什么特别的)。

3 个答案:

答案 0 :(得分:13)

对于ios4及更高版本,您不应使用beginAnimations:contextcommitAnimations,因为documentation不鼓励使用[UIView animateWithDuration:0.3 animations:^{ // animate the following: myLabel.frame = newRect; // move to new location }];

相反,您应该使用其中一种基于块的方法。

上面的例子如下所示:

{{1}}

答案 1 :(得分:12)

您正在寻找UIView上的-beginAnimations:context:-commitAnimations方法。

简而言之,您可以这样做:

[UIView beginAnimations:nil context:NULL]; // animate the following:
myLabel.frame = newRect; // move to new location
[UIView setAnimationDuration:0.3];
[UIView commitAnimations];

答案 2 :(得分:3)

以下是UILabel的示例 - 动画在0.3秒内从左侧滑动标签。

// Save the original configuration. 
CGRect initialFrame = label.frame;

// Displace the label so it's hidden outside of the screen before animation starts.
CGRect displacedFrame = initialFrame;
displacedFrame.origin.x = -100;
label.frame = displacedFrame;

// Restore label's initial position during animation. 
[UIView animateWithDuration:0.3 animations:^{
    label.frame = initialFrame;
}];
相关问题