How To Create Dynamic more than one uiview with Marquee Effect in iphone

北城以北 提交于 2019-12-13 01:24:14

问题


I need to create more than one UIView with MARQUEE effect like HTML <marquee> tag.

I first need to create UIView dynamically.

After add a MARQUEE effect for dynamically created UIView.

Any help will be appreciated.


回答1:


Do you mean a view that can display a collection of strings that are wider than the view's width which it displays by slowly scrolling them from right to left? You'd need to build it. I did once, by subclassing UIScrollView like this:

// 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


来源:https://stackoverflow.com/questions/9919455/how-to-create-dynamic-more-than-one-uiview-with-marquee-effect-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!