【发布时间】:2013-08-08 17:45:40
【问题描述】:
我想知道如何在 xCode 中的 UIScrollView 内使用触摸方法来处理 UIImageView。
当我将UIImageView 子视图添加到 self.view 时,我可以使用 touch 方法。但是当我将 UIImageView 子视图添加到 UIScrollView 时,我不能。我该如何解决这个问题?
这是我的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
touches = [event allTouches];
for (UITouch *touch in touches) {
NSLog(@"Image Touched");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 0.9)];
scrollView.scrollEnabled = TRUE;
scrollView.bounces = TRUE;
scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
scrollView.userInteractionEnabled = YES;
[self.view addSubview:scrollView];
UIImageView *ImageView = [UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * 0.04, 10, [UIScreen mainScreen].bounds.size.width * 0.28, [UIScreen mainScreen].bounds.size.height * 0.22)];
ImageView.image = [UIImage imageNamed(@"image.png")];
ImageView.layer.borderColor = [UIColor whiteColor].CGColor;
ImageView.layer.borderWidth = 1;
ImageView.userInteractionEnabled = YES;
[scrollView addSubview:ImageView];
}
【问题讨论】:
-
你应该改用 UITapGestureRecognizer ..
-
当我使用它时,整个 UIScrollView 区域都是可触摸的。我只希望 UIScrollView 内的 UIImageView 是可触摸的。
-
在这种情况下,您应该将手势添加到 UIImageView 而不是 UIscrollView
-
但是 UIScrollView 不允许 UIImageView 的触摸能力。
标签: objective-c cocoa-touch uiscrollview uiimageview