首页 > 其他 > 详细

关于UIScrollView响应touchesBegan和touchesEnd

时间:2016-01-06 14:06:38      阅读:256      评论:0      收藏:0      [点我收藏+]

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 共存

关于UIScrollView响应touchesBegan和touchesEnd

原文:http://www.cnblogs.com/karl87/p/5105162.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!