环境:UIScrollView承载多个controller,顶部有选择控制器的标题栏按钮;
现象:频繁的切换点击标题栏按钮,或左右滑动控制器,导致临近两控制的内容发生重叠现象。
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内容错位*/
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 响应关掉:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
/*配对使用,开始拖动时候,不再允许响应*/
self.contentView.userInteractionEnabled = NO;
}
(2)在scrollView动画结束的时候,将 userInteractionEnabled 响应打开:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
............................
............................
/*配对使用,滑动结束的时候,允许响应*/
self.contentView.userInteractionEnabled = YES;
}
3) 交叉情况:如果没有 self.isDragAction 标志位,左右滑动时,会触发0.3秒的计时器,操作过快会造成整个控制器的偏移,所以加上它,手动侧滑时,忽略掉计时器。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self scrollViewDidEndScrollingAnimation:scrollView];
...................................
...................................
self.isDragAction = YES;
[self titleClick:self.titlesView.subviews[index]];
}
网友评论