文字無限輪播,適用于廣播消息的上下無限輪播展示。
DEMO下載鏈接:https://github.com/IMCCP/CCPScrollView
DEMO GIF:
scrollView.gif
DEMO 描述:
通過使用 UILabel 與 UIScrollView 來實(shí)現(xiàn)輪播效果。
實(shí)現(xiàn)的思路:
打個(gè)比方,如果需要展示5條數(shù)據(jù),就在ScrollView上創(chuàng)建5 + 1個(gè)label,使得數(shù)據(jù)的顯示順序?yàn)?-1-2-3-4-5。首次顯示1的位置,然后滑動(dòng),等滑動(dòng)到最后一個(gè)
label即數(shù)據(jù)為5時(shí),無動(dòng)畫(一定是無動(dòng)畫效果)切換到第一個(gè)label的位置也就是第一個(gè)數(shù)據(jù)為5的位置,就可以實(shí)現(xiàn)文字的無限輪播滾動(dòng)了。詳細(xì)的實(shí)現(xiàn)過程可以查
看DEMO。
DEMO方法介紹:
/**
* 文字?jǐn)?shù)組
*/
@property (nonatomic,strong) NSArray *titleArray;
/**
* 拼接后的文字?jǐn)?shù)組
*/
@property (nonatomic,strong) NSMutableArray *titleNewArray;
/**
* 是否可以拖拽
*/
@property (nonatomic,assign) BOOL isCanScroll;
/**
* block回調(diào)
*/
@property (nonatomic,copy)void(^clickLabelBlock)(NSInteger index,NSString *titleString);
/**
* 字體顏色
*/
@property (nonatomic,strong) UIColor *titleColor;
/**
* 背景顏色
*/
@property (nonatomic,strong) UIColor *BGColor;
/**
* 字體大小
*/
@property (nonatomic,assign) CGFloat titleFont;
/**
* 關(guān)閉定時(shí)器
*/
- (void)removeTimer;
/**
* 添加定時(shí)器
*/
- (void)addTimer;
/**
* label的點(diǎn)擊事件
*/
- (void) clickTitleLabel:(clickLabelBlock) clickLabelBlock;
DEMO 使用示例
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
CCPScrollView *ccpView = [[CCPScrollView alloc] initWithFrame:CGRectMake(65, 0, self.testView.frame.size.width - 80, self.testView.frame.size.height)];
ccpView.titleArray = [NSArray arrayWithObjects:@"iPhone6s上線32G內(nèi)存手機(jī)你怎么看?",@"親愛的朋友們2016年還有100天就要過去了,2017年您準(zhǔn)備好了嗎?",@"今年雙11您預(yù)算了幾個(gè)月的工資?",@"高德與百度互掐,你更看好哪方?", nil];
ccpView.titleFont = 18;
ccpView.titleColor = [UIColor blackColor];
ccpView.BGColor = CCPRGBColor(221, 221, 221);
[ccpView clickTitleLabel:^(NSInteger index,NSString *titleString) {
NSLog(@"%ld-----%@",index,titleString);
//自定義的彈出view,可以對(duì)彈出視圖進(jìn)行高度的自定義
UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 15 * 2, 250)];
alertView.backgroundColor = [UIColor whiteColor];
alertView.layer.cornerRadius = 8;
alertView.layer.masksToBounds = YES;
UILabel *alertLabel = [[UILabel alloc] init];
alertLabel.textAlignment = NSTextAlignmentCenter;
alertLabel.text = titleString;
alertLabel.font = [UIFont systemFontOfSize:20];
alertLabel.numberOfLines = 0;
alertLabel.width = [UIScreen mainScreen].bounds.size.width - 15 * 2;
[alertLabel sizeToFit];
alertLabel.centerX = alertView.centerX;
alertLabel.centerY = alertView.centerY;
[alertView addSubview:alertLabel];
//彈出自定義彈窗
CCPActionSheetView *actionSheetView = [[CCPActionSheetView alloc] initWithAlertView:alertView];
actionSheetView.viewAnimateStyle = ViewAnimateScale;
}];
[self.testView addSubview:ccpView];
}