2016-01-17 50 views
2

我試圖將我的代碼從obj-c移植到swift,但遇到了很多麻煩。如何覆蓋swift中的UIView.pointInside?

的問題之一是覆蓋一個UIView類的pointInside:如果我添加

/xxx.swift:37:10: Method 'pointInside(_:withEvent:)' with Objective-C selector 'pointInside:withEvent:' conflicts with method 'pointInside(_:withEvent:)' from superclass 'UIView' with the same Objective-C selector 

「:

class MyView : UIView{ 
func pointInside(point: CGPoint, withEvent event: UIEvent) -> Bool { 
    if point.x < 0 { 
     return false 
    } else { 
     return true 

    } 
}} 

如果我不加「覆蓋」,我會得到這個錯誤覆蓋「,我會得到這個錯誤:

/xxx.swift:37:19: Method does not override any method from its superclass 

根據文檔,應該有一個pointInside函數

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/pointInside:withEvent

回答

1

UIKit中的函數被聲明爲

func pointInside(_ point: CGPoint, withEvent event: UIEvent?) -> Bool 

(即​​)需要匹配的可選性。

此錯誤消息似乎不太有用;你可能想要file a bug

0

雨燕2.2語法

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { 
    return true 
} 

注意的UIEvent必須是選擇性相匹配的超

0

你可以用條件檢查覆蓋也很喜歡:

class PassThroughView: UIView { 

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { 
      for subview in subviews as [UIView] { 
       if !subview.hidden && subview.alpha > 0 && subview.userInteractionEnabled && subview.pointInside(convertPoint(point, toView: subview), withEvent: event) { 
        return true 
       } 
      } 
      return false 
     } 
    } 

原帖:Swift: hitTest for UIView underneath another UIView