實現(xiàn)這個漸變導航條的功能琳水,需要拿到前后兩個button,所以在創(chuàng)建button的時候要把它們加入到一個可變的數(shù)組中滋恬。
// 滾動中調(diào)用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offsetX = scrollView.contentOffset.x;
// 取出左右兩個button的index
NSInteger leftIndex = offsetX / ZHJScreenW;
NSInteger rightIndex = leftIndex + 1;
// 取出左邊的button
UIButton *leftButton = self.buttons[leftIndex];
// 取出右邊的button,考慮到在最右邊的時候不用再滑動了,所以要判斷下
UIButton *rightButton = nil;
if (rightIndex < self.buttons.count) {
rightButton = self.buttons[rightIndex];
}
// 取出左邊的縮放值
CGFloat scaleR = offsetX / ZHJScreenW - leftIndex;
// 取出右邊的縮放值
CGFloat scaleL = 1 - scaleR;
// maxTitleScale = 1.3 是最大縮放大小
CGFloat transScale = maxTitleScale - 1;
leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1);
rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1);
// 用rgb來實現(xiàn)顏色的漸變
UIColor *rightColor = [UIColor colorWithRed:scaleR green:scaleR blue:scaleR alpha:1];
UIColor *leftColor = [UIColor colorWithRed:scaleL green:scaleL blue:scaleL alpha:1];
[leftButton setTitleColor:leftColor forState:UIControlStateNormal];
[rightButton setTitleColor:rightColor forState:UIControlStateNormal];
// 設(shè)置指示器的位置
// 算出button的寬度
CGFloat buttonW = (self.view.width - 100) / 3.0;
// 設(shè)置動畫
self.indicator.transform = CGAffineTransformMakeTranslation(buttonW * (offsetX / ZHJScreenW), 0);
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者