0

我有一个名为“nameLabel”的 UILabel,我将它放在一个动画块中,这样就会发生这种情况:

_nameLabel.alpha = 1;
CGAffineTransform translate = CGAffineTransformMakeTranslation(50, 50);
_nameLabel.transform = translate;

我认为将 UILabel 动画到我指定的位置,但它只是从上面的位置动画到我在 Interface Builder 中拥有它的位置。这里有什么帮助吗?

4

2 回答 2

1

您可以发布动画块和其他相关代码吗?我不确定CGAffineTransformMakeTranslation标签有什么作用,但如果你想为框架位置设置动画,你可以使用这个:

CGRect frame = _nameLabel.frame;
frame.origin.y = 100; //Desired height, origin.x can be modified as well. 
//You can also change the frame size here but it wont work on UILabels, you will need `CGAffineTransformScale` for that.

[UIView animateWithDuration: 1.5
                      delay:0.0
                    options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState
                 animations:^ {

                     [_nameLabel setFrame:frame];

                 }
                 completion:^ (BOOL finished) {

                 }];

编辑:另一种方式:

CGRect frame = _nameLabel.frame;
frame.origin.y = 100;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];

[_nameLabel setFrame: newFrame];

[UIView commitAnimations];

当前状态可能是标签的当前位置,请先尝试,但如果没有任何反应,UIViewAnimationOptionBeginFromCurrentState请在动画之前将标签的框架移除或设置到另一个位置,并将其移动到其初始(xib 文件中的那个)位置动画块,由您决定。

于 2012-11-16T08:31:31.360 回答
1

Watch out because Autolayout cause a lot of problems.

Try to deselect 'Use Autolayout'

It solves to me all the problems trying to translate objects.

于 2012-12-12T17:21:47.707 回答