2


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

4

3 回答 3

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

}
于 2013-03-28T14:27:35.130 回答
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
于 2013-03-28T14:42:32.923 回答