1

我想要实现的是创建一个功能,当用户单击图像时,它会推送到 navController 中的下一个 VC。

在 ViewDidLoad 中:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[self.scroll addGestureRecognizer:singleTap];

在 singleTapAction 中:

- (void)singleTapAction:(UIGestureRecognizer *)singleTap {
UIView *view = singleTap.view;
if([view isKindOfClass:[UIImageView class]]){
    NSLog(@"tapped on image");
}
else{
    NSLog(@"Just tapped");
}
}

if 逻辑似乎不起作用。它总是在日志中给出“刚刚点击”。即使当我点击 UIImageView 时,它也会检测到我点击了滚动视图,这是 ImageView 的容器。

(lldb) po view;
$0 = 0x1f5ed6e0 <UIScrollView: 0x1f5ed6e0; frame = (0 0; 320 416); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1f5edc20>; layer = <CALayer: 0x1f5ecb80>; contentOffset: {0, 0}>

编辑:

我用 imageviews 填充滚动视图的方式是:

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                             usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                                 if (nil != group) {
                                     // be sure to filter the group so you only get photos
                                     [group setAssetsFilter:[ALAssetsFilter allPhotos]];
                                     NSLog(@"%d images found", group.numberOfAssets);
//                                         for(int i = group.numberOfAssets - 5; i<group.numberOfAssets - 1; i++){
                                     dispatch_apply(4, dispatch_get_global_queue(0, 0), ^(size_t i){
                                     [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i]
                                                             options:0
                                                          usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                                                              if (nil != result) {
                                                                  ALAssetRepresentation *repr = [result defaultRepresentation];
                                                                  // this is the most recent saved photo
                                                                  UIImage *img = [self fixrotation:[UIImage imageWithCGImage:[repr fullResolutionImage]]];
                                                                  UIImageOrientation orient = img.imageOrientation;
                                                                  NSLog(@"orientation: %d", orient);
                                                                  CGFloat aspectRatio = img.size.width/img.size.height;
                                                                  UIImageView *imgView = [[UIImageView alloc] init];

                                                                  imgView.frame = CGRectMake(10, self.yCord, 300, 300 /aspectRatio);

                                                                  self.yCord += margin + (300/aspectRatio);

                                                                  imgView.image = img;
                                                                  imgView.layer.shadowRadius = 5.0;
                                                                  imgView.layer.shadowColor = [UIColor blackColor].CGColor;

                                                                  imgView.layer.cornerRadius = 4.0;
                                                                  [imgView setUserInteractionEnabled:YES];
                                                                  [self.scroll addSubview:imgView];

                                                                  NSLog(@"%@", imgView);
                                                                  NSLog(@"%f, %f, %f, %f", imgView.frame.origin.x, imgView.frame.origin.y, imgView.frame.size.width, imgView.frame.size.height);
                                                                  *stop = YES;
                                                                  self.scrollViewHeight = self.yCord + imgView.frame.size.height + margin;
                                                                  CGSize scrollViewSize = CGSizeMake(320, self.scrollViewHeight);
                                                                  [self.scroll setContentSize:scrollViewSize];
                                                                  //self.scroll.frame = CGRectMake(0,0, 320, self.scrollViewHeight);
                                                              }
                                                          }];
                                                    });
                                 }
                                 *stop = NO;
                             } failureBlock:^(NSError *error) {
                                 NSLog(@"error: %@", error);
                             }];

现在出现的另一个问题是,setUserInteraction在块内有效吗?

4

4 回答 4

4

为什么不在 ImageView 上添加点击手势而不是

添加滚动...

并制作代码

[self.imageView setUserInteractionEnabled:YES];
于 2013-05-24T12:09:15.437 回答
2

您可以像这样检测触摸是否在图像视图内...

- (void)singleTapAction:(UIGestureRecognizer *)singleTap {
    CGPoint touchPoint = [singleTap locationInView:self.scroll];

    if(CGRectContainsPoint(self.tappableImageView.frame, touchPoint){
        NSLog(@"tapped on image");
    }
    else{
        NSLog(@"Just tapped");
    }
}

如果你有多个图像视图然后做这样的事情......

- (void)singleTapAction:(UIGestureRecognizer *)singleTap {
    CGPoint touchPoint = [singleTap locationInView:self.scroll];

    UIImageView *theTappedView;

    for (UIView *subView in [self.scroll subViews]) {
        if(CGRectContainsPoint(self.tappableImageView.frame, touchPoint)
               && [subView isKindOfClass:[UIImageView class]]) {
            theTappedView = subView;
            break;
        }
    }

    if(theTappedView != nil){
        NSLog(@"tapped on image");
    }
    else{
        NSLog(@"Just tapped");
    }
}

笔记

这假设所有将被点击的图像视图都是滚动视图的self.scroll子视图。

于 2013-05-24T12:10:05.830 回答
1

在您的代码singleTap.view中始终是点击手势附加到的视图,而不是您认为被点击的视图。

您应该为每个图像视图单独创建并附加一个点击手势,而不是向容器视图添加一个。

于 2013-05-24T12:07:32.207 回答
-1

你为什么不使用 UIButton 呢?

您可以为按钮设置自定义图像。

这样,按钮会为您处理点击事件,而不是您编写自己的点击手势处理程序。

我在您的代码中注意到,您有:

UIView *view = 手势.view;

您是否尝试将其更改为:

UIImageView *view = 手势.view;

顺便说一句,当您将图像添加到滚动视图时,您可能希望立即将点击​​手势识别器添加到该图像,而不是将其添加到滚动视图。

于 2013-05-24T12:08:18.447 回答