如何在iphone中使用Marquee Effect创建动态多个uiview [英] How To Create Dynamic more than one uiview with Marquee Effect in iphone
本文介绍了如何在iphone中使用Marquee Effect创建动态多个uiview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建多个带有 MARQUEE 效果的 UIView,例如 HTML 标签.
I need to create more than one UIView with MARQUEE effect like HTML <marquee>
tag.
我首先需要动态创建 UIView.
I first need to create UIView dynamically.
为动态创建的 UIView 添加 MARQUEE 效果后.
After add a MARQUEE effect for dynamically created UIView.
任何帮助将不胜感激.
推荐答案
您的意思是这样的视图可以通过从右向左缓慢滚动来显示比视图宽度更宽的字符串集合吗?你需要建造它.我做过一次,像这样子类化 UIScrollView:
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
这篇关于如何在iphone中使用Marquee Effect创建动态多个uiview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文