Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 检测用户是否点击了UIImageView_Ios_Uigesturerecognizer - Fatal编程技术网

Ios 检测用户是否点击了UIImageView

Ios 检测用户是否点击了UIImageView,ios,uigesturerecognizer,Ios,Uigesturerecognizer,我试图实现的是创建一个功能,当用户单击图像时,它会推送到navController中的下一个VC 在ViewDidLoad中: UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)]; [singleTap setNumberOfTapsRequired:1]; [singleTap setNumberOf

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

在ViewDidLoad中:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[self.scroll addGestureRecognizer:singleTap];
在单点行动中:

- (void)singleTapAction:(UIGestureRecognizer *)singleTap {
UIView *view = singleTap.view;
if([view isKindOfClass:[UIImageView class]]){
    NSLog(@"tapped on image");
}
else{
    NSLog(@"Just tapped");
}
}
如果逻辑似乎不起作用。它总是只在日志中点击。即使我点击UIImageView,它也会检测到我点击了scrollview,这是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}>
编辑:

我用ImageView填充scrollview的方式是:

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是否在块内工作?

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

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

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


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

为什么不使用UIButton

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

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

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

UIView*view=手势.view

您是否尝试将其更改为:

UIImageView*视图=手势。视图


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

为什么不改用UIButton

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

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

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

UIView*view=手势.view

您是否尝试将其更改为:

UIImageView*视图=手势。视图


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

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

正在添加滚动

并编写代码

[self.imageView setUserInteractionEnabled:YES];

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

正在添加滚动

并编写代码

[self.imageView setUserInteractionEnabled:YES];

您可以检测触摸是否在图像视图中,如下所示

- (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");
    }
}
当然,如果您有多个图像视图,请执行以下操作

- (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 scroll视图的子视图。

您可以检测触摸是否在图像视图内,如下图所示

- (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");
    }
}
当然,如果您有多个图像视图,请执行以下操作

- (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 scrollView的子视图。

之所以会出现这种情况,是因为您向scrollView添加了手势。还要确保您的imageview已启用UserInteraction。这是因为您向scrollview添加了手势。还要确保您的imageview已启用用户交互。这是一种攻击。我的应用程序是关于图像编辑的。可能会在以后的阶段产生问题。这是一种黑客行为。我的应用程序是关于图像编辑的。可能会在以后产生问题。实际上,我正在这样做。但是图像是在GCD块中填充的,我必须测试它在块中是否有效。实际上,我正在这样做。但是图像是在GCD块中填充的,我必须测试它在块中是否有效。我正在编辑问题并在创建ImageView的地方添加块,你能看看这是否正确吗?我正在编辑问题并在创建ImageView的地方添加块,你能看看这是不是正确的方法吗