#import <UIKit/UIKit.h>
@protocol MarqueeControlDelegate <NSObject>
//點擊跑馬燈回調(diào)方法
-(void)selectTitleForIndex:(int)index andTitle:(NSString*)title;
@end
@interface MarqueeControl : UIView
//內(nèi)容數(shù)組
@property(strong ,nonatomic)NSArray *titleArr;
//點擊回調(diào)
@property(assign ,nonatomic)id<MarqueeControlDelegate> delegate;
//初始化方法
-(instancetype)initWithFrame:(CGRect)frame andContent:(NSArray*)titleArr;
-(void)start;
-(void)stop;
//重新加載跑馬燈數(shù)據(jù)
-(void)resetMarqueeControlView:(NSArray*)titles;
@end
主要的思想就是跑馬燈的內(nèi)容展示在btn上篷牌,btn放入數(shù)組中援岩,然后修改坐標(biāo)X榜揖,動畫中實現(xiàn)左移效果沙绝,當(dāng)?shù)谝粋€btn移出視圖傍菇,那么第一個btn要放到最后距淫,數(shù)組中也是從第一個刪除污朽,加到最后
#import "MarqueeControl.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
//字體大小
#define WORD_FONT 17
//動畫時間
#define ANIMATE_TIME 0.05
//動畫延時時間
#define ANIMATE_DELAY 0
//每條信息間隔距離
#define BUTTON_SPACING 20
#define BUTTON_TAG 100
@interface MarqueeControl()
//裝載內(nèi)容按鈕數(shù)組
@property (strong ,nonatomic)NSMutableArray *titleBtnArr;
//是否在滾動
@property (assign ,nonatomic)BOOL revolve;
@end
@implementation MarqueeControl
//無數(shù)據(jù)初始化方法
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initData];
}
return self;
}
//有數(shù)據(jù)初始化方法
-(instancetype)initWithFrame:(CGRect)frame andContent:(NSArray*)titleArr
{
self = [super initWithFrame:frame];
if (self) {
[self initData];
if (titleArr) {
self.titleArr = titleArr;
[self layoutContentView];
}
}
return self;
}
//初始化滾動數(shù)據(jù)
-(void)initData{
//這句話的意思就是說傲诵,內(nèi)容是不是只在本視圖展示髓介,移出界面是否還展示
self.clipsToBounds = YES;
self.titleBtnArr = [[NSMutableArray alloc]init];
self.revolve = NO;
}
//布局界面
#pragma mark -布局
-(void)layoutContentView
{
//利用UILabel的sizeToFit方法惕鼓,得到文字長度
UILabel *tempLabel = [UILabel new];
tempLabel.font = [UIFont boldSystemFontOfSize:WORD_FONT];
for(int i=0;i<self.titleArr.count;i++)
{
NSString *title = self.titleArr[i];
tempLabel.text = title;
[tempLabel sizeToFit];
//每一條內(nèi)容都是一個button展示
UIButton *titleBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];;
titleBtn.titleLabel.font = [UIFont boldSystemFontOfSize:WORD_FONT];
titleBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
titleBtn.tag = BUTTON_TAG+i;
[titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
if (self.titleArr.count>0 && i > 0) {
UIButton *btn = self.titleBtnArr[i-1];
//btn的frame.x由前個按鈕的坐標(biāo)+間隔,長度由label提供
titleBtn.frame = CGRectMake(btn.frame.origin.x+btn.frame.size.width, 0, tempLabel.frame.size.width+BUTTON_SPACING, self.frame.size.height);
}else{
如果是第一個按鈕,就不需要它之前的按鈕坐標(biāo)判斷位置了
titleBtn.frame = CGRectMake(0, 0, tempLabel.frame.size.width+BUTTON_SPACING, self.frame.size.height);
}
[titleBtn setTitle:title forState:UIControlStateNormal];
[titleBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
titleBtn.titleLabel.font = [UIFont boldSystemFontOfSize:WORD_FONT];
//添加到頁面上
[self addSubview:titleBtn];
//添加到數(shù)組中
[self.titleBtnArr addObject:titleBtn];
}
}
//重新加載滾動內(nèi)容
-(void)resetMarqueeControlView:(NSArray*)titles
{
//先暫停動畫
[self stop];
//刪除按鈕
for(UIButton *btn in self.titleBtnArr)
{
[btn removeFromSuperview];
[btn.layer removeAllAnimations];
}
[self.titleBtnArr removeAllObjects];
//重新加載數(shù)據(jù)
if (titles) {
self.titleArr = titles;
[self layoutContentView];
}
}
#pragma mark -操作跑馬燈事件
//開始動畫
-(void)start
{
if (!self.revolve) {
self.revolve = YES;
[self startRevolve];
}
}
//停止動畫
-(void)stop{
self.revolve = NO;
}
-(void)startRevolve
{
//UIViewAnimationOptionAllowUserInteraction在動畫過程中唐础,允許點擊交互
[UIView animateWithDuration:ANIMATE_TIME delay:ANIMATE_DELAY options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveLinear animations:^{
//執(zhí)行動畫,數(shù)組中的所有按鈕frame.x坐標(biāo)往左移動每次-1
for (UIButton *btn in self.titleBtnArr) {
CGRect frame = btn.frame;
frame = CGRectMake(frame.origin.x-1, frame.origin.y, frame.size.width, frame.size.height);
btn.frame = frame;
}
} completion:^(BOOL finished) {
//取第一個按鈕
UIButton *btn = self.titleBtnArr[0];
//取最后一個按鈕
UIButton *lastBtn = self.titleBtnArr[self.titleBtnArr.count-1];
CGRect frame = btn.frame;
//判斷數(shù)組中的第一個按鈕像左移動箱歧,是不是已經(jīng)移出視圖中
if (frame.size.width + frame.origin.x<0) {
//如果最后一個按鈕x+width小于當(dāng)前視圖寬度
if (lastBtn.frame.origin.x+lastBtn.frame.size.width<self.frame.size.width) {
//第一個按鈕的坐標(biāo)要放到視圖的最右邊,然后慢慢左移滑入
frame = CGRectMake(self.frame.size.width, frame.origin.y, frame.size.width, frame.size.height);
}else{
//第一個按鈕的坐標(biāo)排在最后一個按鈕的后面一膨,慢慢左移滑入
frame = CGRectMake(lastBtn.frame.origin.x+lastBtn.frame.size.width, frame.origin.y, frame.size.width, frame.size.height);
}
//數(shù)組中刪除第一個按鈕
[self.titleBtnArr removeObjectAtIndex:0];
btn.frame = frame;
//按鈕加到最后面叫胁,也就是說第一個按鈕,左移出了界面汞幢,就要放到界面的最后
[self.titleBtnArr addObject:btn];
}
//判斷是否繼續(xù)滾動了
if(self.revolve){
[self startRevolve];
}
}];
}
#pragma mark -代理回調(diào)
-(void)titleBtnClick:(UIButton*)sender
{
int index =(int)sender.tag-BUTTON_TAG;
[self.delegate selectTitleForIndex:index andTitle:self.titleArr[index]];
}