我需要使用MARQUEE效果创建多个UIView,例如HTML <marquee>
标记。
我首先需要动态创建UIView。
为动态创建的UIView添加MARQUEE效果。
任何帮助将不胜感激。
答案 0 :(得分:1)
你的意思是一个视图可以显示一个比视图宽度宽的字符串集合,它通过从右向左缓慢滚动显示它?你需要建立它。我曾经做过一次,通过像这样继承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