在view上面加了个ScrollView,怎样响应touch事件
1个回答
展开全部
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];
}
要想实现,必须对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];
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询