最近在做一個選擇器彩届,但不是系統(tǒng)的那種選擇器伪冰。最后在網(wǎng)上找到了一個 開源庫。樟蠕。iCarousel
還算是比較好用的贮聂,但是在使用之前,必須要先熟悉他的一些熟悉和代理回調(diào)方法坯墨,不然比較難以實現(xiàn)。病往。有點摸不著頭腦的感覺捣染,畢竟不熟悉的東西都會認為深不可測。停巷。
iCarouselType: 很重要的屬性耍攘,關(guān)乎著你的樣式。畔勤。
? ? iCarouselTypeLinear = 0,
? ? iCarouselTypeRotary,
? ? iCarouselTypeInvertedRotary,
? ? iCarouselTypeCylinder,
? ? iCarouselTypeInvertedCylinder,
? ? iCarouselTypeWheel,
? ? iCarouselTypeInvertedWheel,
? ? iCarouselTypeCoverFlow,
? ? iCarouselTypeCoverFlow2,
? ? iCarouselTypeTimeMachine,
? ? iCarouselTypeInvertedTimeMachine,
? ? iCarouselTypeCustom
還是直接上代碼:
? ? /// 初始化 ? ? self.carous = [[iCarousel alloc] initWithFrame:CGRectMake(0, 150, 375, 375)]; ? ? /// 設置代理 ? ? self.carous.delegate = self; ? ? self.carous.dataSource = self; ? ? [self.view addSubview:_carous]; ? ? self.carous.backgroundColor = [UIColor cyanColor]; ? ? /// 設置樣式 ? ? self.carous.type = iCarouselTypeLinear;?
? ? /// 設置數(shù)據(jù)源
self.dataArray = [NSMutableArray array]; ? ?
?self.titleArray = [NSMutableArray array]; ? ??
for (int i = 0; i < 40; i++) { ?? ? ? ? ? ? ? ??
UIColor *color = [UIColor colorWithWhite:arc4random()%255/255.0 alpha:1]; ? ??
[self.titleArray addObject:[NSString stringWithFormat:@"%d",i]]; ? ? ? ?
?[self.dataArray addObject:color]; ? ?
?}
? ? ///刷新數(shù)據(jù)源
? ? [self.carous reloadData];
代理方法實現(xiàn)
#pragma mark ---- iCarouselDataSource
/// 最重要的代理之一蕾各,數(shù)據(jù)源的設置,不然其他都是白搭
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
? ? return self.dataArray.count;
}
/// 內(nèi)容視圖的設置庆揪,類似于tableView 這些列表視圖的設置式曲。
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
?/// 這里沒有考慮復用,在實際項目中,還是的采用復用
? ? UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
? ? lb.textAlignment = NSTextAlignmentCenter;
? ? if (index < self.dataArray.count) {
? ? ? ? UIColor *color = self.dataArray[index];
? ? ? ? lb.backgroundColor = color;
? ? }
? ? if (index < self.titleArray.count) {
? ? ? ? NSString *title = self.titleArray[index];
? ? ? ? lb.text = title;
? ? }
? ? return lb;
}
另外吝羞,在下面的這個代理方法中,有幾個相對重要的參數(shù)兰伤,需要通過這個代理去設置,因為是只讀屬性钧排,一個是 iCarouselOptionWrap 敦腔,,另外一個是? iCarouselOptionVisibleItems
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value {
? ? switch (option) {
? ? ? ? case iCarouselOptionWrap:
? ? ? ? ? ? /// 是否是可以旋轉(zhuǎn)的? *****
? ? ? ? ? ? return self.wrapEnabled;
? ? ? ? ? ? break;
? ? ? ? case iCarouselOptionShowBackfaces:
? ? ? ? ? ? ///
? ? ? ? ? ? return self.showBackfaces;
? ? ? ? ? ? break;
? ? ? ? case iCarouselOptionOffsetMultiplier:
? ? ? ? ? ? /// 偏移乘數(shù)
? ? ? ? ? ? break;
? ? ? ? case iCarouselOptionVisibleItems:
? ? ? ? ? ? /// 可以看到的有多少個? *****
? ? ? ? ? ? break;
}
}