iphone - 如何在 iphone 中创建具有 Marquee Effect 的动态多个 uiview

标签 iphone ios uiview effect marquee

我需要创建多个具有 MARQUEE 效果的 UIView,如 HTML <marquee>标签。

我首先需要动态创建 UIView。

为动态创建的UIView添加MARQUEE效果后。

我们将不胜感激。

最佳答案

您的意思是一个 View 可以通过从右向左缓慢滚动来显示比 View 宽度更宽的字符串集合?你需要构建它。我做过一次,像这样子类化 UIScrollView:

// CrawlView.h

#import <UIKit/UIKit.h>

@interface CrawlView : UIScrollView

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

- (void)go;

@end


// CrawlView.m

#import "CrawlView.h"

// distance between neighboring strings.  could make this a public property
#define kPADDING 16.0

@interface CrawlView ()

@property (assign, nonatomic) CGFloat messagesWidth;

@end

@implementation CrawlView

@synthesize period=_period;
@synthesize messages=_messages;
@synthesize messagesWidth=_messagesWidth;

- (void)buildSubviews {

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

    CGFloat xPos = kPADDING;

    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 + kPADDING;
        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)setMessages:(NSMutableArray *)messages {

    if (_messages != messages) {
        _messages = messages;
        [self buildSubviews];
    }
}

- (void)go {

    if (!self.period) self.period = self.messagesWidth / 100;
    // so it always takes about the same (fudged, but reasonable) amount of time to scroll the whole array

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


@end

关于iphone - 如何在 iphone 中创建具有 Marquee Effect 的动态多个 uiview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9919455/

相关文章:

ios - 如何在自动布局约束中使用不等式?

ios - 对象自动设置为零

iphone - 从 NSArray 中删除 UIView

java - 处理多种语言编码

ios - iOS8 中的多行标签

iphone - 从字符串中获取 NSDate

ios - 无法运行自动增量构建脚本

ios - 如何在按钮触及内部时从 Superview 中删除以编程方式创建的 subview ?

ios - Swift:如何根据宽度排列按钮?间距都相等吗?

iphone - 尝试构建用于 iOS 的 SSCrypto 框架时构建失败