gpt4 book ai didi

ios - Xcode Swift - 检测屏幕上颜色的触摸

转载 作者:搜寻专家 更新时间:2023-10-31 22:23:51 24 4
gpt4 key购买 nike

我正在使用带有 Swift 的 Xcode 6 Beta,我想创建一个方法,当屏幕上的手指触摸特定的 UIColour(例如灰色)时调用该方法。

我该怎么做?

最佳答案

Objective-C 已经回答了这个问题.该代码在 Swift 中略有不同,但我还是对其进行了转换;

extension UIView
{
func colorOfPoint (point: CGPoint) -> UIColor
{
var pixel = UnsafePointer<CUnsignedChar>.alloc(4)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo.fromRaw(CGImageAlphaInfo.PremultipliedLast.toRaw())!
let context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, bitmapInfo)

CGContextTranslateCTM(context, -point.x, -point.y)

self.layer.renderInContext(context)

CGContextRelease(context)
CGColorSpaceRelease(colorSpace)

return UIColor(red: Float(pixel [0]) / 255.0, green: Float (pixel [1]) / 255.0, blue: Float (pixel [2]) / 255.0 , alpha: Float (pixel [3]) / 255.0)
}
}

在你的 View Controller 中;

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!)
{
let touch : UITouch = event.allTouches().anyObject() as UITouch
let location = touch.locationInView(self.view)
pickedColor = self.view.colorOfPoint (location)
// Do something with picked color.
}

关于ios - Xcode Swift - 检测屏幕上颜色的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24714052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com