How to add marquee to a label

2020-02-23 04:17发布

I have a label at the top of my UIView.I am displaying some messages on it through array with the help of the timer.But now i want this messages to be displayed in MARQUEE style.I am not getting any way to start.Any source code,methods to use,any animation style,any different approach. Thanks in advance

标签: iphone
2条回答
神经病院院长
2楼-- · 2020-02-23 05:08

Here's an idea:

  • Set clipsToBounds to true on the UIView that your UILabel is sitting in.
  • Then start a UIViewAnimations block (look in the docs for how to use it, its very simple)
    • Create a new label with its frame set to a position that is outside of the bounds of the enclosing UIView
    • set the previous label's position to somewhere to the left outside the bounds of the enclosing UIView
    • set the next label's position to where the previous label used to be.
  • commit the animations

That should give you the sliding animation of one label moving off screen and a new label moving on screen.

You'll have to play around with the animationDuration property and the precise positioning and sizing properties of the labels and views to get it just right, but it should be quite simple.

查看更多
▲ chillily
3楼-- · 2020-02-23 05:20

I followed your idea, and I did that:

*I Created a container view.

messageView = [[UIView alloc] initWithFrame:CGRectMake(27, 0, 235, 19)];
[messageView setClipsToBounds:YES];//With This you prevent the animation to be drawn outside the bounds.

*Then I created a UILabel with the text to be displayed and added to my container view

lblTime = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 235, 19)];
[lblTime setBackgroundColor:[UIColor clearColor]];
[messageView addSubview:lblTime];

*Finally I created a function like this:

- (void)sendNotification:(NSString*)txt{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:messageView cache:YES];
    [lblTime setText:txt];
    [lblTime setTextAlignment:UITextAlignmentLeft];
    lblTime.frame = CGRectMake(260, 0, 258, 19);
    [UIView commitAnimations];
}
查看更多
登录 后发表回答