2

I'm trying to port my code from obj-c to swift, but experienced a lot of troubles.

one of the issues is overriding pointInside of a UIView class:

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

    }
}}

if I don't add "override", I will get this error:

/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

If I add "override", I will get this error:

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

according to the doc, there should be a pointInside function

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

Bill Yan
  • 3,369
  • 4
  • 27
  • 42

3 Answers3

1

The function in UIKit is declared as

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

The optionality (namely UIEvent?) needs to match.

This error message seems less than useful; you might want to file a bug.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
1

You can override with conditional check also like:

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
        }
    }

Original post: Swift: hitTest for UIView underneath another UIView

Rahul
  • 300
  • 2
  • 12
0

Swift 2.2 syntax

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

Note UIEvent must be optional to match the superclass

Fraser
  • 953
  • 11
  • 21