【发布时间】:2010-12-29 16:12:58
【问题描述】:
我花了很多时间试图弄清楚如何将 UILabel 从屏幕的一侧边缘移动到另一侧。 基本上我希望我从用户那里得到的文本从右到左出现在屏幕上然后消失,并且它继续循环并再次出现(你可以在时代广场看到的 srt :-)
【问题讨论】:
-
不用说(实际上说很重要...),我们将非常感谢您对这个主题的帮助。
我花了很多时间试图弄清楚如何将 UILabel 从屏幕的一侧边缘移动到另一侧。 基本上我希望我从用户那里得到的文本从右到左出现在屏幕上然后消失,并且它继续循环并再次出现(你可以在时代广场看到的 srt :-)
【问题讨论】:
一般来说,一个标签可以通过首先指定它的框架,设置它的属性(文本,背景颜色),将它添加到你的
视图的子视图,然后使用[UIView animateWithDuration:delay:options:animations:completion] 指定最终帧。这
方法需要更多“仅用于标签”的代码,但最终还是值得的。
以下代码将在左上角 (0,0) 放置一个标签,并将其动画到右下角 (确切的坐标取决于您的 iOS 设备)。通用步骤如下:
applicationFrame 而不是bounds。initWithFrame 创建标签
animations: 的块中完成的,我们指定
我们之前计算的右下角的帧的最终位置(请注意,我们不会在动画期间更改帧的大小)。这是一个非常通用的例子,这里可以做很多事情,
包括同时为多个标签设置动画以实现选取框效果等。当
添加额外的动画注意视图控制器可以被关闭,并且
动画仍在运行。通常最好使用[self.view.layer removeAllAnimations] 停止动画并删除可能仍然存在的子视图(我通常会保留我创建的所有标签的 NSMutableArray 以便我可以在viewWillDisappear 中清理它们) .
#define FONT_SIZE 14
#define DURATION 10
#define DELAY 10
-(void)viewWillAppear:(BOOL)animated {
NSString* string = @"My Label";
CGSize framesize = [string sizeWithFont:[UIFont fontWithName:@"Copperplate" size:FONT_SIZE]];
// Origin
float x0 = 0;
float y0 = 0;
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat appFrameWidth = appFrame.size.width;
CGFloat appFrameHeight = appFrame.size.height;
// Destination
float x1 = appFrameWidth - framesize.width;
float y1 = appFrameHeight - framesize.height;
UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(x0, y0, framesize.width, framesize.height)];
label.backgroundColor = [UIColor clearColor];
label.text = string;
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(1,2);
label.font = [UIFont fontWithName:@"Copperplate" size:FONT_SIZE];
[self.view addSubview:label];
[UIView animateWithDuration:DURATION
delay:DELAY
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
label.frame = CGRectMake(x1, y1, framesize.width, framesize.height);
}
completion:^(BOOL finished){
[label removeFromSuperview];
}];
}
【讨论】:
如果您希望您的标签保持在固定位置,您可以移动标签内的文本,例如使用 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
【讨论】:
- (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];
}
【讨论】:
您可以使用带有计时器和所有工具包的核心动画,或者您可以只使用 UIView 上的方法来启动并提交动画和动画事件以重新开始。
尝试类似:
-(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];
}
【讨论】: