IOS:将正在运行的文本作为“在电视中播放新闻线”?

时间:2013-03-28 14:23:49

标签: ios


我很有意思,怎样才能让文字从右到左像“电视中的新闻线”一样移动?
我可以在UILabel中从右到左(或任何其他)移动文本,但这个动画移动应该是无限循环,而不仅仅是一次。

3 个答案:

答案 0 :(得分:5)

答案 1 :(得分:1)

这样怎么样:

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

}

答案 2 :(得分: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