如何在UILabel中使用动画师

时间:2010-07-27 11:56:10

标签: iphone core-animation

我已经读过我可以在任何UIView中使用动画对象制作动画,这包含在Core Animation中,所以我写了

[[label animator] setFrame:someRect];

但它发出警告称UILabel可能不会回应-animator

我也可以找到方法[label setWantsLayer:YES];

有人会帮我吗?

2 个答案:

答案 0 :(得分:5)

-animator-setWantsLayer:方法来自cocoa(OS X),而不是cocoa-touch(iOS)。默认情况下,UIKit对象支持图层。

答案 1 :(得分:2)

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.containerView addSubview:myLabel];

CGRect destination = CGRectMake(5, 5, 100, 100); //for instance

[UIView beginAnimations:@"animationIdentifierString" context:nil];
myLabel.frame = destination;
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //that's the default, actually, but set whatever you want there.
[UIView commitAnimations];

我只是在这里输入了这个,所以要小心打字错误。但这就是主意。在调用[UIView beginAnimations: context:][UIView commitAnimations]时包装对UIView子类的更改,在那里的动画上设置一些配置,然后繁荣,你就是动画。

还有其他方法可以做到这一点,但对我来说这是最简单的。

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIView_Class/UIView/UIView.html

的UIView类引用中提供更多详细信息