Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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
Iphone 如何在iOS上的图像透明部分的imageview后面进行触摸?_Iphone_Ios_Touch_User Interaction_Image Masking - Fatal编程技术网

Iphone 如何在iOS上的图像透明部分的imageview后面进行触摸?

Iphone 如何在iOS上的图像透明部分的imageview后面进行触摸?,iphone,ios,touch,user-interaction,image-masking,Iphone,Ios,Touch,User Interaction,Image Masking,在这里,我想在按钮点击-B 当点击图像时(在图像的可见区域),点击手势方法将调用 但问题是图像在按钮上,它覆盖了按钮的某些部分 我想在覆盖部分上执行按钮操作事件-A 我已尝试过,但无法在覆盖部分执行按钮操作:( 在那部分上放一个透明按钮 更新 -(void)singleTapImageview:(UITapGestureRecognizer *)gesture { NSLog(@"single-tap"); CGPoint touchLocation = [gesture l

在这里,我想在按钮点击-B

当点击图像时(在图像的可见区域),点击手势方法将调用

但问题是图像在按钮上,它覆盖了按钮的某些部分 我想在覆盖部分上执行按钮操作事件-A

我已尝试过,但无法在覆盖部分执行按钮操作:(


在那部分上放一个透明按钮

更新

 -(void)singleTapImageview:(UITapGestureRecognizer *)gesture
 {
    NSLog(@"single-tap");
    CGPoint touchLocation = [gesture locationInView:self.imageViewParent];
    float x = touchLocation.x;
    float y = touchLocation.y;

    //Using gesture recogniser you can get the current position of touch

    UIImage* image = imageViewParent.image;
    CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
    UInt8 *pixels = (UInt8 *)CFDataGetBytePtr(data);

    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1);
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor redColor].CGColor);
    int index = ((x + (y * (int)image.size.width)) * 4) + 3;
    CGFloat alpha = pixels[index];
    if (alpha == 0)
    {
        // call button acton
    }

}

您可以添加一个新的透明按钮,该按钮将与图像部分和现有按钮重叠,并具有框架


CGRectMake(button.frame.origin.x,button.frame.origin.y,button.frame.size.width,button.frame.size.height);

只需避免以那种方式将按钮与图像重叠即可。看起来很糟糕


对于更长期的解决方案,我建议与UX设计师和图形艺术家交朋友。如果幸运的话,他们可能是同一个人。

您可以通过为重叠按钮编写hittest函数来解决问题

在命中测试中,您必须通过检查坐标来确定哪个按钮被击中


你可以添加手势识别器,可以获取图像的x,y,并让你知道图像被触摸的位置@我已经添加了手势识别器,但是使用x,y有点笨拙,我正在寻找更好的解决方案,如果可能的话,通过其他方式简单->
image.userInteractionEnabled=NO;
哦,希望图像不会得到手势@但我也为图像添加了点击手势:(yaaahhhh但是..这将隐藏我添加了点击手势事件要执行的图像部分:(每当用户触摸图像时,您可以计算Y坐标的值,并可以这样比较:if(touchValueOfY<(button.frame.origin.Y+button.frame.size.width)){调用按钮操作}else调用图像actionBit worked:)但每次buttonTapped都将在下面的代码中执行,否则任何时候都不会执行(touchPoint.y<(btndown.frame.origin.y+btndown.frame.size.width)){NSLog(@“buttonTapped”);}else{NSLog(@“imgTapped”);}+10谢谢,它适用于大多数触摸区域,但在图像之外的某个点,它仍然会调用按钮操作。我只希望图像点击在图像唯一可见的部分可用。请检查此链接