環(huán)境:UIScrollView承載多個controller,頂部有選擇控制器的標題欄按鈕;
現(xiàn)象:頻繁的切換點擊標題欄按鈕,或左右滑動控制器,導致臨近兩控制的內(nèi)容發(fā)生重疊現(xiàn)象。
1.添加兩個布爾屬性
@property (nonatomic, strong) UIScrollView *contentView;
@property (nonatomic, assign) BOOL allowBtnCilck; //設置標志位,防止btn被頻繁的響應
@property (nonatomic, assign) BOOL isDragAction; //設置標志位,是否是拖動操作,防止btn被頻繁的響應
2. BOOL值的與判斷分別對應兩種情況:
- (void)titleClick:(UIButton *)button {
if (self.allowBtnCilck || self.isDragAction) {
/*保證拖動事件和按鈕點擊事件互不干擾*/
if (!self.isDragAction) {
self.allowBtnCilck = NO;
/*防止按鈕頻繁點擊,導致ScrollView內(nèi)容錯位*/
self.contentView.userInteractionEnabled = NO;
[self performSelector:@selector(allowBtnCilckAgain) withObject:nil afterDelay:0.3];
}
self.isDragAction = NO;
//下面的是按鈕響應的原先處理操作
..........................................
..........................................
}
}
- (void)allowBtnCilckAgain {
self.allowBtnCilck = YES;
self.contentView.userInteractionEnabled = YES;
}
1)頻繁切換點擊標題按鈕: self.allowBtnCilck 做一個延時處理;
2) 頻繁左右滑動ScrollView:
(1)在scrollView開始被拖動的時候,將 userInteractionEnabled 響應關(guān)掉:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
/*配對使用,開始拖動時候,不再允許響應*/
self.contentView.userInteractionEnabled = NO;
}
(2)在scrollView動畫結(jié)束的時候,將 userInteractionEnabled 響應打開:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
............................
............................
/*配對使用,滑動結(jié)束的時候,允許響應*/
self.contentView.userInteractionEnabled = YES;
}
3) 交叉情況:如果沒有 self.isDragAction 標志位,左右滑動時,會觸發(fā)0.3秒的計時器,操作過快會造成整個控制器的偏移,所以加上它,手動側(cè)滑時,忽略掉計時器。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self scrollViewDidEndScrollingAnimation:scrollView];
...................................
...................................
self.isDragAction = YES;
[self titleClick:self.titlesView.subviews[index]];
}