如何在iOS 9,swift 2中正确覆盖触摸功能

时间:2015-10-23 12:42:41

标签: ios xcode swift touch

好吧,我说changeColorTo(touch: touches.anyObject() as? UITouch) 行 这个错误"类型的值" Set"没有会员" anyObject"

我该如何解决问题?

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    changeColorTo(touch: touches.anyObject() as? UITouch)
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    changeColorTo(touch: touches.anyObject() as? UITouch)
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    changeColorTo(touch: touches.anyObject() as? UITouch)
}

func changeColorTo(touch  touch: UITouch? ) {
    if let contact = touch {
        changeColorTo(point: contact.locationInView(self))
    }
}

func changeColorTo(point  point: CGPoint ) {
    // Change the color model to the color displayed at the given view coordinate
    if let color = colorModel {
        let bounds = self.bounds
        if bounds.contains(point) {
            color.hue = Float((point.x-bounds.minX)/bounds.width*360)
            color.saturation = Float((point.y-bounds.minY)/bounds.height*100)
        }
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

您不应将UITouch设置传递给CGPoint。

尝试以下解决问题,

   override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        let touch = touches.anyObject()! as UITouch
        let location = touch.locationInView(self)
        changeColorTo(location)
    }

    func changeColorTo(point: CGPoint ) {

    }