iOS 旋轉(zhuǎn)木馬 iCarousel 實現(xiàn)細(xì)節(jié)

最近在做一個選擇器,但不是系統(tǒng)的那種選擇器。最后在網(wǎng)上找到了一個 開源庫。。iCarousel

iCarousel

還算是比較好用的,但是在使用之前,必須要先熟悉他的一些熟悉和代理回調(diào)方法,不然比較難以實現(xiàn)。。有點摸不著頭腦的感覺,畢竟不熟悉的東西都會認(rè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)]; ? ? /// 設(shè)置代理 ? ? self.carous.delegate = self; ? ? self.carous.dataSource = self; ? ? [self.view addSubview:_carous]; ? ? self.carous.backgroundColor = [UIColor cyanColor]; ? ? /// 設(shè)置樣式 ? ? self.carous.type = iCarouselTypeLinear;?

? ? /// 設(shè)置數(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ù)源的設(shè)置,不然其他都是白搭

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {

? ? return self.dataArray.count;

}

/// 內(nèi)容視圖的設(shè)置,類似于tableView 這些列表視圖的設(shè)置。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {

?/// 這里沒有考慮復(fù)用,在實際項目中,還是的采用復(fù)用

? ? 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ù),需要通過這個代理去設(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;

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容