跑馬燈效果的滾動條,一般出現在ios應用的底部。用於顯示動態變化的信息或內容較長的信息,在個類應用中使用廣泛
以下兩種可用的跑馬燈滾動MarqueeBar的實現。
1.直接在ViewController中實現對UIView的位置定時移動來實現,以下代碼直接加入到ViewController中,在viewWillAppear中調用loadView即可。
代碼如下 復制代碼 - (void)marqueeView
2.自行定義滾動條控件,讓view自己滾動起來,通過不斷的相互方法調用實現循環滾動
UIMarqueeBarView.h定義
/**
*UIMarqueeBarView.h
*/
@interface UIMarqueeBarView : UIView
{
}
- (void)start;
- (void)stop;
@end
UIMarqueeBarView.m實現
/**
*UIMarqueeBarView.m
*/
#import "UIMarqueeBarView.h"
@implementation UIMarqueeBarView
- (void)dealloc
{
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setupView];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
if( (self = [super initWithCoder:aDecoder]) ) {
// Initialization code
[self setupView];
}
return self;
}
- (void)setupView
{
[self setBackgroundColor:[UIColor lightGrayColor]];
[self setClipsToBounds:YES];
[self setOpaque:YES];
UILabel *lblContent = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150 ,16)]autorelease];
[lblContent setText:@"這裡是滾動條。。。。。"];
[lblContent setTextColor:[UIColor whiteColor]];
[lblContent setBackgroundColor:[UIColor clearColor]];
[lblContent setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[lblContent setNumberOfLines:1];
[lblContent setOpaque:YES];
[self addSubview:lblContent];
}
- (void)start
{
if (self.viewContainer == NULL) {
[self setupView];
}
[self startAnimation];
}
- (void)stop
{
}
-(void)startAnimation
{
[UIView beginAnimations:@"MarqueeBarAniamation" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:25];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
CGRect viewFrame = self.viewContainer.frame;
viewFrame.origin.x = -320;
[self.viewContainer setFrame:viewFrame];
[UIView commitAnimations];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
CGRect viewFrame = self.viewContainer.frame;
viewFrame.origin.x = 320;
[self.viewContainer setFrame:viewFrame];
[self startAnimation];
}