UIScrollView點擊事件

爲UIScrollView添加點擊事件,從而實現scrollview上的點擊背景退出編輯狀態。ui

直接代碼呈上:
spa

1、先要支持協議 UIGestureRecognizerDelegate3d

2、爲你須要添加點擊事件的scrollview添加點擊手勢
事件

//srcollView添加點擊響應
   
UITapGestureRecognizer *sigleTapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBackView)];
    sigleTapRecognizer.
delegate=self;
    sigleTapRecognizer.
numberOfTapsRequired = 1;
get

    [_baseScrollView addGestureRecognizer:sigleTapRecognizer];it

3、點擊後的處理-----這裏是點擊scrollview上空白處才進行處理
io

//點擊背景取消全部編輯狀態class

-(void)clickBackView
{
   
NSLog(@"點了一下背景");
    [
_baseScrollView endEditing:YES];
}

#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
   
// 輸出點擊的view的類名
   
NSLog(@"%@", NSStringFromClass([touch.view class]));
   
   
// 若爲ScrollView的點擊事件才響應,scrollview上的UIButton、UILabel啥的點了也不會進哦
   
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UIScrollView"]) {
       
return YES;
    }
   
return  NO;cli

}select

相關文章
相關標籤/搜索