2


I'm interesting, how can it is possible to make text moving from right to left like a "running news line in TV"?
I can do text moving from right to left (or any other) in UILabel, but this animation moving should be infinity loop, not only one time.

woz
  • 10,888
  • 3
  • 34
  • 64
boder
  • 47
  • 1
  • 4

3 Answers3

1

How about like this:

-(void) animate {

    label.center = CGPointMake(self.view.bounds.size.width + label.bounds.size.width/2, label.center.y);
    [UIView animateWithDuration:20 delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionRepeat) animations:^{

        label.center = CGPointMake(0 - label.bounds.size.width/2, label.center.y);

    } completion:nil];

}
jjv360
  • 4,120
  • 3
  • 23
  • 37
0
// CrawlView.h

#import <UIKit/UIKit.h>

@interface CrawlView : UIScrollView

@property (assign, nonatomic) NSTimeInterval period;
@property (strong, nonatomic) NSMutableArray *messages;

- (void)go;

@end

// CrawlView.m

#define kWORD_SPACE  16.0f

#import "CrawlView.h"

@interface CrawlView ()
@property (assign, nonatomic) CGFloat messagesWidth;
@end

@implementation CrawlView


- (void)buildSubviews {

    for (UIView *subview in [self subviews]) {
        if ([subview isKindOfClass:[UILabel self]]) {
            [subview removeFromSuperview];
        }
    }

    CGFloat xPos = kWORD_SPACE;

    for (NSString *message in self.messages) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.text = message;
        CGSize size = [message sizeWithFont:label.font];
        CGFloat width = size.width + kWORD_SPACE;
        label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
        [self addSubview:label];
        xPos += width;
    }
    self.messagesWidth = xPos;
    self.contentSize = CGSizeMake(xPos, self.frame.size.height);
    self.contentOffset = CGPointMake(-self.frame.size.width, 0.0);
}

- (void)go {

    [self buildSubviews];
    if (!self.period) self.period = self.messagesWidth / 100;

    [UIView animateWithDuration:self.period
                          delay:0.0
                        options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionRepeat
                     animations:^{
                         self.contentOffset = CGPointMake(self.messagesWidth, 0.0);
                     } completion:^(BOOL finished){
                         [self buildSubviews];}];
}

@end
danh
  • 62,181
  • 10
  • 95
  • 136