IOS学习之UIScrollView touch触摸事件是本文要介绍的内容,UIScrollView本身无法处理touch事件。要想实现,必须对UIScrollView上的subView做touch处理原理十分简单,好比要响应scrollView上的UIImageView,那么请创建一个UIImageVIew的子类,由这个自定义的UIImageView来处理touch事件。
头文件声明如下,供参考:
- #import <Foundation/Foundation.h>
- @protocol ImageTouchDelegate
- -(void)imageTouch:(NSSet *)touches withEvent:(UIEvent *)event whichView:(id)imageView;
- @end
- @interface ImageTouchView : UIImageView
- {
- id<ImageTouchDelegate> delegate;
- BOOL delegatrue;
- }
- @property(nonatomic,assign)id<ImageTouchDelegate> delegate;
- @end
这个是头文件,源文件可以是这个这样子
- @implementation ImageTouchView
- @synthesize delegate;
- -(id)initWithFrame:(CGRect)frame
- {
- if (self == [super initWithFrame:frame])
- {
- [self setUserInteractionEnabled:YES];
- delegatrue=YES;
- }
- return self;
- }
- - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
- {
- return YES;
- }
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if (delegatrue)
- {
- [delegate imageTouch:touches withEvent:event whichView:self];
- }
小结:IOS学习之UIScrollView touch触摸事件的内容介绍完了,希望本文对你有所帮助!