iOS Swift点击位置判断和坐标转换

2022-04-20  本文已影响0人  rome753
image.png

当前View里面有两个View,绿色的bigView和红色的smallView,smallView在bigView里面。现在要在当前View的touchesBegan方法判断点击位置是否在最里层的smallView里。

有多种方法可以判断。其实它们本质都是一样的,最重要的是转换参考系。

class OurView: UIView {
    
    lazy var bigView = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
    
    lazy var smallView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        
        bigView.backgroundColor = .green
        addSubview(bigView)
        
        smallView.backgroundColor = .red
        bigView.addSubview(smallView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            // 方法1:用bigView参考系
            let point = touch.location(in: bigView)
            let contains = smallView.frame.contains(point)
            print("smallView touches: \(contains)")

            // 方法2:用smallView参考系
            let point = touch.location(in: smallView)
            let contains = smallView.bounds.contains(point)
            print("smallView touches: \(contains)")

            // 方法3:获取点击位置在self参考系坐标,用convert方法转换成bigView参考系坐标
            var point = touch.location(in: self)
            point = self.convert(point, to: bigView)
            let contains = smallView.frame.contains(point)
            print("smallView touches: \(contains)")

            // 方法4:获取点击位置在self参考系坐标,手动转换成bigView参考系坐标
            var point = touch.location(in: self)
            point.x -= bigView.frame.minX
            point.y -= bigView.frame.minY
            let contains = smallView.frame.contains(point)
            print("smallView touches: \(contains)")

            break
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读