简体   繁体   English

iPhone上的UILabel /文本动画

[英]UILabel / text animation on IPhone

Spent quite a lot of time trying to figure out how i could animate the movement of a UILabel from one one edge of the screen to the opposite one. 花了很多时间试图弄清楚如何将UILabel的运动从屏幕的一个边缘动画到另一个边缘。 Basically i want a text that i got from the user to appear on screen from right to left and the disappear, and it keeps on looping and appearing again (srt of what you can see on Times Square :-) 基本上我想要一个从用户那里得到的文本从右到左显示在屏幕上并消失,它会一直循环并再次出现(你可以在时代广场上看到的内容:-)

In the generic sense, a label can be animated by first specifying its frame, setting it's attributes (text, background color), adding it to your view's subview, and then using [UIView animateWithDuration:delay:options:animations:completion] to specify the final frame. 在一般意义上,标签可以通过首先指定其框架,设置其属性(文本,背景颜色),将其添加到视图的子视图,然后使用[UIView animateWithDuration:delay:options:animations:completion]来指定[UIView animateWithDuration:delay:options:animations:completion]最后一帧。 This method requires a little bit more code "just for a label" but it's worth it in the end. 这种方法需要更多的代码“仅用于标签”,但最终还是值得的。

The following code will place a label in the upper-left (0,0) and animate it to the lower-right (the exact coordinates will depend on your iOS device). 以下代码将在左上角(0,0)中放置一个标签,并将其设置为右下角(确切的坐标将取决于您的iOS设备)。 The generic steps are as follows: 通用步骤如下:

  • Calculate the size of the frame required for your label, this is dependent upon the font face and size for your label 计算标签所需框架的大小,这取决于标签的字体和大小
  • Obtain your application frame, see Offset on UIWindow addSubview on why to use applicationFrame as opposed to bounds . 获取应用程序框架,请参阅UIWindow addSubview上的Offset ,了解为什么要使用applicationFrame而不是bounds
  • Calculate your destination coordinates; 计算目的地坐标; in this example we want the lower-right of the label to touch the lower-right of the screen, so we calculate the upper-left coordinates to account for the label's frame 在这个例子中,我们希望标签的右下角触摸屏幕的右下角,因此我们计算左上角坐标以考虑标签的框架
  • Create the label with initWithFrame 使用initWithFrame创建标签
  • Style the label with a background color, set the text, font, etc. Note: You obviously must use the same font face and size you specified when calculating your frame! 使用背景颜色设置标签样式,设置文本,字体等。注意:显然您必须使用在计算框架时指定的相同字体和大小!
  • Add the subview (UILabel) to your current view 将子视图(UILabel)添加到当前视图
  • Animate! 动画! This is done in the block passed as the parameter to animations: , we are specifying the final location of the frame (note that we are not changing the frame's size during the animation) that we previously calculated to be in the lower-right. 这是在作为参数传递给animations:的块中完成的animations:我们指定帧的最终位置(请注意,我们不会在动画期间更改帧的大小),我们之前计算的位于右下方。
  • Finally, once the animation is complete, remove the label with removeFromSuperview 最后,动画完成后,使用removeFromSuperview删除标签

This is a very generic example, and there are lot of things that can be done here, including animating several labels simultaneously for a marquee effect, etc. When adding additional animations be aware the view controller can be dismissed and that the animations are still running. 这是一个非常通用的例子,这里可以做很多事情,包括同时动画几个标签以获得选框效果等。当添加其他动画时,请注意视图控制器可以被解除并且动画仍然在运行。 It's generally a good idea stop the animations with [self.view.layer removeAllAnimations] as well as remove the subviews that may still be hanging around (I typically keep an NSMutableArray of all of the labels I've created so I can clean them up in viewWillDisappear ). 通常使用[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];
                   }];
}

If you want your label remain at a fixed location you can move the text inside the label, for example using a NSTimer that fires and calls a methods to update your UiLabel in regular (second or so) Intervals: 如果您希望标签保留在固定位置,您可以移动标签内的文本,例如使用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]; 
 }

You can use core animation for that with timer and all the kit or you can just use the methof on UIView to start and commit animation and animation event to start over again. 您可以使用计时器和所有工具包使用核心动画,或者您可以使用UIView上的methof来启动并提交动画和动画事件以重新开始。

Try something like : 尝试类似的东西:

-(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];    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM