求助scrollview内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];
}
温馨提示:答案为网友推荐,仅供参考
大家正在搜