Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

ios - Scrolling UILabel like a marquee in a subview

I have a UILabel in the main view with text - "Very Very long text". The proper width to this would be 142, but i've shortened it to 55.

Basically I want to implement a marquee type scroll, so I wrote code to add it onto a subview and animate it within the bounds of that view.

CODE --

    CGRect tempLblFrame = _lblLongText.frame;
    UIView *lblView = [[UIView alloc] initWithFrame:tempLblFrame];

    //Add label to UIView at 0,0 wrt to new UIView
    tempLblFrame.origin.x = 0;
    tempLblFrame.origin.y = 0;

    [_lblLongText setFrame:tempLblFrame];
    [_lblLongText removeFromSuperview];
    [lblView addSubview:_lblLongText];

    //SetClipToBounds so that if label moves out of bounds of its superview, it wont be displayed
    [lblView setClipsToBounds:YES];
    [lblView setBackgroundColor:[UIColor cyanColor]];
    [self.view addSubview:lblView];

After this I get this output on the simulator --> enter image description here

The problem occurs when i try the Animation with this code -

    tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width;        
    [UIView animateWithDuration:2.0 delay:1.0 options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         [_lblLongText setFrame:tempLblFrame];
                     }
                     completion:^(BOOL finished) {
                         NSLog(@"completed");
                     }];

I was hoping I would see the entire "Very Very long text", rather only "Very..." scrolls from left to right.

To solve this I added one line of code --

    //Add label to UIView at 0,0 wrt to new UIView
    tempLblFrame.origin.x = 0;
    tempLblFrame.origin.y = 0;

    tempLblFrame.size.width = _lblLongText.intrinsicContentSize.width; //THIS LINE WAS ADDED

    [_lblLongText setFrame:tempLblFrame];
    [_lblLongText removeFromSuperview];
    [lblView addSubview:_lblLongText];

I thought the full text will be set inside the newly added UIView and it would scroll properly. But running in the simulator gave me this --

enter image description here

And again, only "Very..." was scrolling from left to right.

What am I doing wrong? Please help!!

EDIT

Apparently the culprit was AutoLayout.

I have no clue why, but once I unchecked "Use Autolayout" for the view in the XIB, everything started working as expected. Setting tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width; was working properly and so was the scroll.

Any explanation on this!!?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This question is possibly Duplicate of.

Although there is nice code snippet written by Charles Powell for MarqueeLabel,

also take a look at This link.

I hope this will help you and will save your time by giving a desired output.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...