花了很多时间试图弄清楚如何将UILabel的运动从屏幕的一个边缘动画到另一个边缘。 基本上我想要一个从用户那里得到的文本从右到左显示在屏幕上然后消失,它会继续循环并再次出现(你可以在时代广场上看到的内容: - )
答案 0 :(得分:2)
在一般意义上,标签可以通过首先指定其框架,设置其属性(文本,背景颜色),将其添加到您的框架来设置动画。
查看子视图,然后使用[UIView animateWithDuration:delay:options:animations:completion]
指定最终帧。这个
方法需要更多的代码“仅用于标签”,但它最终是值得的。
以下代码将在左上角(0,0)中放置一个标签,并将其设置为右下角的动画 (确切的坐标取决于您的iOS设备)。通用步骤如下:
applicationFrame
而不是bounds
的原因。initWithFrame
animations:
的块中完成的,我们正在指定
帧的最终位置(请注意,我们没有在动画期间更改帧的大小)我们之前计算的位于右下方。这是一个非常通用的例子,这里可以做很多事情,
包括同时动画几个标签以获得选框效果等
添加额外的动画要注意视图控制器可以被解雇
动画仍然在运行。通常使用[self.view.layer removeAllAnimations]
停止动画以及删除可能仍然存在的子视图(我通常会保留我创建的所有标签的NSMutableArray以便我可以在{{{ 1}})。
viewWillDisappear
答案 1 :(得分:1)
如果您希望标签保留在固定位置,您可以在标签内移动文本,例如使用NSTimer触发并调用方法以常规(大约两个)间隔更新您的UiLabel:
NSString * longText = [NSString stringWithString:@"this is a long text"];
// begin loop here
NSString * firstLetter = [longText substringToIndex:1]);
NSString * otherLetters = [longText substringFromIndex:1]);
longText = [otherLetters stringByAppendingString:firstLetter];
UILable.text = longText;
// wait here a second or so
// end loop
答案 2 :(得分:1)
- (void)viewDidLoad
{
[self animateLoop];
[super viewDidLoad];
}
- (void)animateLoop
{
mylab.text=@"SAAAAdiiiii";
mylab.frame = CGRectMake(-mylab.bounds.size.width, 100, mylab.bounds.size.width, mylab.bounds.size.height);
[UIView beginAnimations:@"timesquare" context:nil];
[UIView setAnimationDuration:5];
[UIView setAnimationRepeatAutoreverses:(YES)];
[UIView setAnimationRepeatCount:10];
mylab.frame = CGRectMake(480, 100, mylab.bounds.size.width, mylab.bounds.size.height);
[UIView commitAnimations];
}
答案 3 :(得分:0)
您可以使用计时器和所有套件使用核心动画,或者您可以使用UIView上的methof来启动并提交动画和动画事件以重新开始。
尝试类似:
-(void)animateLoop {
myLabel.frame = CGRectMake(-myLabel.bounds.size.width, 100, myLabel.bounds.size.width, myLabel.bounds.size.height);
[UIView beginAnimations:@"timesquare" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDidStopSelector:@selector(animateLoop)];
myLabel.frame = CGRectMake(480, 100, myLabel.bounds.size.width, myLabel.bounds.size.height);
[UIView commitAnimations];
}