【问题标题】:How to get touch behind the imageview on transparent part of image on iOS?如何在iOS上图像透明部分的imageview后面触摸?
【发布时间】:2013-01-19 04:27:03
【问题描述】:

在这里,我想在按钮点击时获得按钮操作事件的触摸 - B

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

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

我已经尝试过但无法在覆盖部分上获得按钮操作:(

【问题讨论】:

  • 您可以添加手势识别器并可以获取图像的x,y并让您知道图像被触摸的位置
  • @Shashank Kulshrestha 我已经添加了手势识别器,但是 comapring x,y 有点笨拙的任务,如果可能的话,我正在寻找更好的解决方案
  • 简单 -> image.userInteractionEnabled = NO; 哦,希望图像不会有手势
  • @ codesburner 但我也为图像添加了点击手势:(

标签: iphone ios touch user-interaction image-masking


【解决方案1】:

在该部分上放置一个透明按钮

更新

 -(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
    }

}

【讨论】:

【解决方案2】:

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

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

【讨论】:

  • Saurabh shukla yaaahhhhh 但是..这将隐藏我添加了要执行的点击手势事件的图像部分:(
  • 当用户触摸图像时,您可以计算 Y 坐标的值并可以像这样进行比较: if(touchValueOfY
  • 有点工作 :) 但每次 buttonTapped 都会在下面的代码中执行,否则部分不会在任何时候执行 if(touchPoint.y
【解决方案3】:

只是避免以这种方式将按钮与图像重叠。看起来很糟糕。

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

【讨论】:

    【解决方案4】:

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

    在 hittest 中,您必须通过检查坐标来决定按下哪个按钮。

    http://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/hitTest:withEvent:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      相关资源
      最近更新 更多