如何知道.png的唯一可见区域是否在 Xcode 中被触摸



我已经将.png图像导入到Xcode中的UIImageView中,我想做的是当触摸图像时,它将被隐藏。

但我的问题是 png 图像包含透明部分,当我触摸透明部分时,操作会继续。我希望只有在触摸图像的可见部分时才能继续操作。如何解决问题?

Swift 或 Objective-C

我创建了一个自定义的UIButton子类,它的行为与您描述的完全一样,请看一下:https://github.com/spagosx/iOS-Shaped-Button-Swift

它是用 Swift 编写的,但它很容易转换为 Objective-c。

方法是从接触点获取像素数据并访问 RGBA 值,在这种情况下,我们读取 A (alpha) 并检查它是否高于我们的阈值。

查看一些代码:

func alphaFromPoint(point: CGPoint) -> CGFloat {
    var pixel: [UInt8] = [0, 0, 0, 0]
    let colourSpace = CGColorSpaceCreateDeviceRGB()
    let alphaInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
    let context = CGContext(data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colourSpace, bitmapInfo: alphaInfo.rawValue)
    context?.translateBy(x: -point.x, y: -point.y)
    self.layer.render(in: context!)
    let floatAlpha = CGFloat(pixel[3])
    return floatAlpha
}

您可以获取floatAlpha值并将其与可接受的 alpha 值进行比较:

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        return self.alphaFromPoint(point) >= 100
    }

我冒昧地将"Danny S"的答案更新为 Swift 5,并删除了无关的代码,修复了错误并为用户体验增加了一些额外的清晰度。

代码如下:

https://github.com/ZoeESummers/SOXShapedTapView-Updated.git

  -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    {
        UITouch *touch = [touches anyObject]; 
        CGPoint touch_point = [touch locationInView:self.view];
        if (![imageView pointInside:touch_point withEvent:event]) 
        {
            NSLog(@"you inside imageview");
// write here what you want
        }
    } 

在 Swift 4.2 中结合 Danny 和 Sport 的答案作为扩展。

extension UIButton{
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = event!.touches(for: self)?.first {
            let location = touch.location(in: self)
            if alphaFromPoint(point: location) == 0 {
                self.cancelTracking(with: nil)
                print("cancelled!")
            } else{
                super.touchesBegan(touches, with: event)
            }
        }
    }
    func alphaFromPoint(point: CGPoint) -> CGFloat {
        var pixel: [UInt8] = [0, 0, 0, 0]
        let colorSpace = CGColorSpaceCreateDeviceRGB();
        let alphaInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
        let context = CGContext(data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: alphaInfo.rawValue)
        context!.translateBy(x: -point.x, y: -point.y)
        self.layer.render(in: context!)
        let floatAlpha = CGFloat(pixel[3])
        return floatAlpha
    }
}

最新更新