
索引.gif
索引2.gif

索引2.gif
索引條部分使用UIView 的 layoutSubviews繪制字母和邊框線
setNeedsDisplay會調(diào)用自動調(diào)用drawRect方法,這樣可以拿到UIGraphicsGetCurrentContext,就可以畫畫了。而setNeedsLayout會默認(rèn)調(diào)用layoutSubViews,就可以處理子視圖中的一些數(shù)據(jù)。
宗上所訴,setNeedsDisplay方便繪圖,而layoutSubViews方便出來數(shù)據(jù)。
因為這兩個方法都是異步執(zhí)行的,所以一些元素還是直接繪制的好
使用CAShapeLayer與UIBezierPath可以實現(xiàn)不在view的drawRect方法中就畫出一些想要的圖形,在這里使用CAShapeLayer繪制索引表的邊框線
根據(jù)手勢來判斷顯示中間的索引塊
//開始觸摸
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}
//手勢移動
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event{}
//手勢結(jié)束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}
根據(jù)觸摸事件的觸摸點位置來算出點擊的是第幾個section
-(void)sendEventToDelegate:(UIEvent*)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint point = [touch locationInView:self];
NSInteger indx = ((NSInteger) floorf(point.y) / _letterHeight);
if (indx< 0 || indx > self.titleIndexes.count - 1) {
return;
}
[self.collectionDelegate collectionViewIndex:self didselectionAtIndex:indx withTitle:self.titleIndexes[indx]];
}