zl程序教程

您现在的位置是:首页 >  前端

当前栏目

关于UIScrollView响应touchesBegan和touchesEnd

响应 关于 touchesBegan UIScrollView
2023-06-13 09:12:40 时间

大家好,又见面了,我是你们的朋友全栈君。

ViewController中,常用touch事件关闭编辑事件,收起键盘。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}

但是addSubview的UIScorllView并不能直接响应touchesBegan和touchesEnd事件。

因此需要继承UIScrolleView类,并重写方法。

@interface myScrollView : UIScrollView 
@end 
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
  [super touchesBegan:touches withEvent:event]; 
  if ( !self.dragging ) { 
  [[self nextResponder] touchesBegan:touches withEvent:event]; 
  } 
} 
 
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
  [super touchesEnded:touches withEvent:event]; 
  if ( !self.dragging ) { 
      [[self nextResponder] touchesEnded:touches withEvent:event]; 
    } 
} 

使用重写过的UIScrollView就可以通过传递事件,来响应这两个方法。

这个问题背后反应的是iOS的Responder chain (响应链)。

参考:http://mobile.51cto.com/hot-404894.htm

Responder Chain(ios事件传递)

iOS中的触摸事件和手势处理

IOS开发之手势——UIGestureRecognizer 共存

转载于:https://www.cnblogs.com/karl87/p/5105162.html

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/164193.html原文链接:https://javaforall.cn