【问题标题】:event.touchesForView().AnyObject() not working in Xcode 6.3event.touchesForView().AnyObject() 在 Xcode 6.3 中不起作用
【发布时间】:2015-04-10 16:57:51
【问题描述】:

这在以前很有效:

func doSomethingOnDrag(sender: UIButton, event: UIEvent) {
    let touch = event.touchesForView(sender).AnyObject() as UITouch
    let location = touch.locationInView(sender)
}

但在 Xcode 6.3 中,我现在收到错误:

不能在没有参数的情况下调用“AnyObject”

我该如何解决这个问题?

【问题讨论】:

    标签: ios xcode swift uitouch uievent


    【解决方案1】:

    在 1.2 中,touchesForView 现在返回原生 Swift Set 而不是 NSSet,并且 Set 没有 anyObject() 方法。

    它确实有一个first 方法,这几乎是一回事。另请注意,您将无法再使用as?,您必须使用as? 进行转换并处理 nil 的可能性,这是一种方法:

    func doSomethingOnDrag(sender: UIButton, event: UIEvent) {
        if let touch = event.touchesForView(sender)?.first as? UITouch,
               location = touch.locationInView(sender) {
                // use location
        }
    }
    

    【讨论】:

      【解决方案2】:
      func doSomethingOnDrag(sender: UIButton, event: UIEvent) {
          let buttonView = sender as! UIView;
          let touches : Set<UITouch> = event.touchesForView(buttonView)!
          let touch = touches.first!
          let location = touch.locationInView(buttonView)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-29
        • 1970-01-01
        • 2015-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多