跑馬燈效果的滾動條通常出現在 App 底部,用來顯示動態變化的資訊或過長而無法完整呈現的文字,在音樂播放器、新聞列表與看板類應用中使用相當廣泛。以下提供兩種實作思路。
做法是以固定間隔遞迴呼叫,每次把容器往左挪一點,超出範圍後重設起始位置。這種寫法直接寫進 ViewController,在 viewWillAppear 中呼叫 loadView 即可。
- (void)marqueeView
{
CGRect frame = self.vMarqueeContainer.frame;
frame.origin.x = frame.origin.x - 2;
if (frame.origin.x < -frame.size.width)
{
frame.origin.x = 320;
}
self.vMarqueeContainer.frame = frame;
// 延時遞迴呼叫
[self performSelector:@selector(marqueeView) withObject:nil afterDelay:0.04];
}
- (void)loadView
{
// marquee bar 背景,控制位置與高度
UIView *viewMarqueeBar = [[[UIView alloc] initWithFrame:CGRectMake(0, 347, 320, 20)] autorelease];
[viewMarqueeBar setBackgroundColor:[UIColor darkGrayColor]];
// 滾動容器,決定可視範圍
UIView *viewMarqueeContainer = [[[UIView alloc] initWithFrame:CGRectMake(320, 3, 360, 14)] autorelease];
[viewMarqueeContainer setBackgroundColor:[UIColor clearColor]];
[viewMarqueeContainer setClipsToBounds:YES];
[viewMarqueeContainer setOpaque:YES];
// 文字內容
UILabel *lblContent = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 14)] autorelease];
[lblContent setText:@"這裡是滾動條。。。。。"];
[lblContent setTextColor:[UIColor whiteColor]];
[lblContent setBackgroundColor:[UIColor clearColor]];
[lblContent setFont:[UIFont fontWithName:@"Helvetica" size:12]];
[lblContent setOpaque:YES];
self.view = viewMarqueeBar;
self.vMarqueeContainer = viewMarqueeContainer;
[self.view addSubview:viewMarqueeContainer];
[self marqueeView];
}
把滾動邏輯封進自訂 UIView,對外只暴露 start / stop 兩個方法,並以動畫委派實作無縫循環。
/**
* UIMarqueeBarView.h
*/
@interface UIMarqueeBarView : UIView
{
}
- (void)start;
- (void)stop;
@end
/**
* UIMarqueeBarView.m
*/
#import "UIMarqueeBarView.h"
@implementation UIMarqueeBarView
- (void)dealloc
{
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupView];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[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
{
[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];
}
@end
兩案取捨:方案一簡單直觀,適合只有一處使用的情況,缺點是邏輯寫在 ViewController 中不易重用,且需自行管理 timer 生命週期;方案二可重複使用,並以 UIView 動畫取代手動 timer,效能與流暢度都更好。實務上建議直接採用方案二。
使用方案二時別忘了在 viewContainer 上設定 clipsToBounds,否則文字會溢出可視區域,跑馬燈效果就失去意義了。