我想提出以下解决方案。
AIImageView.m:
#import "AIImageView.h"
@implementation AIImageView
{
NSInteger _cnt;
}
- (id)initWithImage: (UIImage*)image
{
if ((self = [super initWithImage:image]))
{
[self setUserInteractionEnabled: YES];
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: self
action: @selector(handleTapGesture:)];
tapGesture.delegate = self;
[self addGestureRecognizer: tapGesture];
_cnt = 0;
}
return self;
}
- (UIView*) hitTest: (CGPoint) point withEvent: (UIEvent*) event
{
CGPoint result = [self convertPoint: point toView: self.scrollView];
_cnt++;
if (CGRectContainsPoint(self.frame, result))
{
if (_cnt > 2)
{
_cnt = 0;
}
else
{
return self.scrollView;
}
}
return [super hitTest: point withEvent: event];
}
- (void) handleTapGesture: (id) sender
{
NSLog(@"TAP!!!");
}
@end
AIImageView.h:
#import <UIKit/UIKit.h>
@interface AIImageView : UIImageView <UIGestureRecognizerDelegate>
@property(nonatomic, strong) UIScrollView* scrollView;
@end
创建的ImageView:
UIImage* image = [UIImage imageNamed: @"default_cover.png"];
imageView = [[AIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(100, 400, 100, 100);
imageView.scrollView = self.scrollView;
[self.view addSubview: imageView];
如果你会尝试它,你可以看到这个解决方案的工作,但不总是:)。但我相信它可以帮助你。
这会使图像视图无法点击。 –
我更新了我的答案 – stosha
与上面的解决方案一样,从我的测试看来,这只有在UIImage是UIScrollView的子视图时才有效。我试图避免这一点,但我想这似乎是不可避免的。 – Aron