2017-11-13 10 views
1

ボーダーで表示する必要があり、ボーダーのみをクリック可能にする必要があります。空のビュー(クリック可能なボーダーのみ)

カスタムビューを作成するコードですが、ジェスチャを追加するとすべてのビューがクリック可能になります。

class RectangleView: UIView { 

    override func draw(_ rect: CGRect) { 
    let aPath = UIBezierPath() 
    aPath.move(to: CGPoint(x:0, y: 0)) 
    aPath.addLine(to: CGPoint(x:rect.width, y: 0)) 
    aPath.addLine(to: CGPoint(x:rect.width, y:rect.height)) 
    aPath.addLine(to: CGPoint(x:0, y:rect.height)) 
    aPath.close() 
    UIColor.green.setStroke() 
    aPath.stroke() 
    UIColor.clear.setFill() 
    aPath.fill() 
    } 
} 

編集、ここでは他のアプリからのスクリーンショットは、 enter image description here

あるので、私はこの1つは前面になり、クリック可能になり、私の上面図の下のビューをクリックしますので、私は小さいサブビューを追加カント

+0

それを反復処理するだけ無効にユーザーとの対話と小さいビューを置きますビューの前に – the4kman

+0

その場合、他のビューはトップビューになるとクリックできません 私の質問を編集しました。iamgeをご覧ください。 –

+0

何か試しましたか?ビューをタップするコードはどこにありますか? –

答えて

1

UIViewサブクラスの場合はfunc point(inside point: CGPoint, with event: UIEvent?) -> Boolをオーバーライドするのが最も正しい方法です。ここで

は一例です:

class CustomView: UIView { 
    private let shapeLayer = CAShapeLayer() 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     setupShapeLayer() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    private func setupShapeLayer() { 
     //Draw your own shape here 

     shapeLayer.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height) 

     let innerPath = UIBezierPath(arcCenter: center, radius: frame.width/3, startAngle: CGFloat(0), endAngle: CGFloat.pi * 2, clockwise: true) 

     let outerPath = UIBezierPath(arcCenter: center, radius: frame.width/4, startAngle: CGFloat(0), endAngle: CGFloat.pi * 2, clockwise: true) 

     //Subtract inner path 
     innerPath.append(outerPath.reversing()) 

     shapeLayer.path = innerPath.cgPath 
     shapeLayer.lineWidth = 10 
     shapeLayer.strokeColor = UIColor.red.cgColor 
     shapeLayer.fillColor = UIColor.yellow.cgColor 

     layer.addSublayer(shapeLayer) 
    } 

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { 
     return shapeLayer.path?.contains(point) ?? false 
    } 
} 

今、あなたはそれがどのように動作するか確認するためにUITapGestureRecognizerを追加することができます。tapAction

class ViewController: UIViewController { 

    @IBOutlet weak var resultLabel: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let customView = CustomView.init(frame: view.frame) 
     view.addSubview(customView) 

     let recognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction)) 
     view.addGestureRecognizer(recognizer) 
    } 

    @objc func tapAction(_ sender: UITapGestureRecognizer) { 
     let location = sender.location(in: sender.view) 
     let subview = view?.hitTest(location, with: nil) 

     if subview is CustomView { 
      resultLabel.text = "CustomView" 
     } 
     else { 
      resultLabel.text = "Other" 
     } 
    } 
} 

結果:

enter image description here

を使用すると、複数のが必要な場合ビュー、2つのオプティオがありますNS:point関数内1 CustomView内部

  • ストアCAShapeLayerし、それを反復処理
  • は、インスタンスごとにCustomViewを追加し、UITapGestureRecognizerタップアクション内
関連する問題