让UIScrollView上的subView响应触摸事件
来源:互联网 发布:常州软件开发公司 编辑:程序博客网 时间:2024/05/29 04:36
UIScrollView本身是无法处理touch事件的。所以要想让UIScrollView上的子view响应Touch事件有俩个思路:
第一, 就是重写这个子View类。
例如我要让UIScrollView上的UIImageView响应触摸事件,那我就写一个UIImageView的子类,在这个重定义的类里写代理方法响应触摸。
.h文件:
#import <UIKit/UIKit.h>@protocol ImageTouchDelegate<NSObject>-(void)imageTouch:(NSSet *)touches withEvent:(UIEvent *)event whichView:(id)imageView;@end@interface ImageTouchView : UIImageView@property(nonatomic,weak)id<ImageTouchDelegate> delegate;@end
.m文件:
- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.userInteractionEnabled=YES; } return self;}- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view{ return YES;}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ if (_delegate && [_delegate respondsToSelector:@selector(imageTouch:withEvent:whichView:)]) { [_delegate imageTouch:touches withEvent:event whichView:self]; }}
第二,可以写个UIScrollView的子类,把事件从UIScrollView传出去。
.h文件:
#import <UIKit/UIKit.h>@interface ScrollTouchView : UIScrollView-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;@end
.m文件:
@implementation ScrollTouchView- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.userInteractionEnabled=YES; } return self;}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesBegan:touches withEvent:event]; [super touchesBegan:touches withEvent:event];}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{ [[self nextResponder]touchesMoved:touches withEvent:event]; [super touchesMoved:touches withEvent:event];}@end
以上俩个方法都可以(貌似我用的第一个方法写的划得更流畅一些,至于原因,我现在也母鸡啊)
关于UIScrollView 原理 我看的这个链接:http://www.cocoachina.com/bbs/read.php?tid-40965-page-1.html 很厉害的样子。。。
0 0
- 让UIScrollView上的subView响应触摸事件
- IOS_让UIScrollView的子view响应触摸事件
- 让UIScrollView的子view响应触摸事件
- 让UIScrollView的子view响应触摸事件
- UIScrollView 响应触摸事件的机制
- 如何让UIScrollView里面的UIImageView响应touch事件
- subview在SuperView的bounds之外接收触摸信息。以及subview的响应
- UIScrollView的事件响应原理
- 响应触摸事件的方法
- UIScrollView 中处理subView点击事件
- UIScrollView touch 触摸事件
- UIScrollView无法响应touch事件的解决办法
- UIScrollView无法响应touch事件的解决办法
- UIScrollView不能响应UITouch事件的解决办法
- UIScrollView不能响应UITouch事件的解决办法
- UIScrollView不能响应UITouch事件的解决办法
- UIScrollView不能响应touch事件的解决办法
- UIScrollView无法响应touch事件的解决办法
- Kafka消费者生产者编程模型(二)
- lightoj-1109 - False Ordering【思维】
- TextView的setTextSize和getTextSize单位不同
- day01开始学习Java的第一步
- 事件监听器_Demo03.java
- 让UIScrollView上的subView响应触摸事件
- 富文本常用封装(NSAttributedString浅析)
- LightOJ 1109 False Ordering
- javaScript 基本类型数据对象规律
- qt之qmake
- css选择器 助记(一)
- <Android>监听软键盘打开收起事件(软键盘自带收起按钮)
- SQL 优化过程
- Android文件操作的总结