iCarousel是一款运行在iPhone和iPad上的第三方轮播图控件,与TableView类似的实现方式,简单又方便。
工具/原料
Xcode
iCarousel
方法/步骤
1、iCarousel中iCarouselDataSource代理中必须实现的两个代理方法如下:-(NSInteger)numberOfItemsInCarousel:(iCarousel*)carousel;-(UIView*)carousel:(iCarousel*)carouselviewForItemAtIndex:(NSInteger)indexreusingView:(nullableUIView*)view;
2、在需要实现的控制器中导入iCarousel.h头文件,遵循iCarouselDelegate与iCarouselDataSource代理,如下:
3、然后在需要实现的控制器中初始化iCarousel,iCarousel继承于UI外狒芙梨View,使用时候只需要像其他控件一样初始化加在指定的视图上即可,如下:iCarousel*c锾攒揉敫arousel=[[iCarouselalloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)];carousel.type=iCarouselTypeCylinder;carousel.delegate=self;carousel.dataSource=self;[self.viewaddSubview:carousel];
4、iCarouselD瞢铍库祢ataSource的两个代理实现如下,分别设置视图个数以及单个视图设置,如下:-惺绅寨瞀(NSInteger)numberOfItemsInCarousel:(iCarousel*)carousel{return10;}-(UIView*)carousel:(iCarousel*)carouselviewForItemAtIndex:(NSInteger)indexreusingView:(nullableUIView*)view{if(view==nil){UIView*colorView=[[UIViewalloc]init];colorView.backgroundColor=[UIColorcolorWithRed:(arc4random()%256)/255.0green:(arc4random()%256)/255.0blue:(arc4random()%256)/255.0alpha:1.0];colorView.frame=CGRectMake(0,0,150,CGRectGetHeight(carousel.bounds));returncolorView;}else{view.backgroundColor=[UIColorcolorWithRed:(arc4random()%256)/255.0green:(arc4random()%256)/255.0blue:(arc4random()%256)/255.0alpha:1.0];returnview;}returnnil;}
5、iCarouselDelegate中-(void)carousel:(__unusediCarousel*)carouseldidSelectItemAtIndex:(NSInteger)index;代理方法在点击轮播视图时候响应,实现如下:-(void)carousel:(__unusediCarousel*)carouseldidSelectItemAtIndex:(NSInteger)index{NSLog(@"Tappedviewnumber:%ld",(long)index);}其他代理方法的实现可以根据自身需要进行设置。
6、使用iCarouselTypeCylinder类型的实现效果如下: