不多說,好不好先看效果,之前做過一個scrollView的輪播圖,但是很局限,很多多余代碼,今天貓貓重新做了一個用collectionView的流水布局做的一個,可以拿去做廣告輪播,也可以做系統新特性哦,來,看下效果吧。
源碼共享:https://github.com/znycat/NYCarouselView
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *ma = [NSMutableArray array];
for (int i = 1; i<5; i++) {
NSString *imageName = [NSString stringWithFormat:@"banner%d",i];
[ma addObject:imageName];
}
NYCarouselView *carouselView = [[NYCarouselView alloc]initWithFrame:CGRectMake(0, 0, NYScreenWidth, 190)imageNames:ma];
// [carouselView startTimer];//開啟時鐘自動輪播
[self.view addSubview:carouselView];
//給輪播圖增加點擊事件
carouselView.cellDidSelectItemAtIndexPath = ^(UICollectionView *collection,NSIndexPath *indexPath) {
NSLog(@"indexPath === %ld", indexPath.row);
};
}
看著貌似挺復雜的,其實簡單說就兩部 創建他,把一個放著要輪播的圖片數組放進去就ok了
NYCarouselView *carouselView = [[NYCarouselView alloc]initWithFrame:CGRectMake(0, 0, NYScreenWidth, 190)imageNames:ma];
調用點擊事件直接就是block,本來想寫代理的,但是一想代理會多好多代碼。。。總之,就是這樣了
//給輪播圖增加點擊事件
carouselView.cellDidSelectItemAtIndexPath = ^(UICollectionView *collection,NSIndexPath *indexPath) {
NSLog(@"indexPath === %ld", indexPath.row);
};
}