web-dev-qa-db-ja.com

プログラムでUIGestureRecognizerをサブビューに追加するSwift

現在、作成され、ViewDidLoad()UIViewに追加されたサブビューがあります。 UIGestureRecognizersを使用してタップを検出し、特定のボタンを再表示しようとしています。私の現在のコード:

_ override func viewDidLoad() {
    super.viewDidLoad()
    architectView = CustomClass(frame: self.view.bounds)
    self.view.addSubview(architectView)        
    let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
    gestureRecognizer.delegate = self
    architectView.addGestureRecognizer(gestureRecognizer)
}

func handleTap(gestureRecognizer: UIGestureRecognizer) {
    let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}
_

handleTap()関数は、タップが認識されているかどうかを確認する簡単なテストです。このコードは、押されたときにUIAlertをトリガーしませんか?何が欠けていますか?

8
Andrew Walz

私はここであなたのコードをテストしましたが、うまくいきます。ただし、UIGestureRecognizerDelegateプロトコルをView Controllerに追加できない場合があると思います。下記参照:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

    var architectView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        architectView = UIView(frame: self.view.bounds)
        self.view.addSubview(architectView)
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
        gestureRecognizer.delegate = self
        architectView.addGestureRecognizer(gestureRecognizer)
    }

    func handleTap(gestureRecognizer: UIGestureRecognizer) {
        let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
12
Raphael Silva

Raphael Silvaの回答に基づくSwift 3バージョンコード:

import UIKit

class ViewController: UIViewController, UIGestureRecognizerDelegate {

    var architectView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        architectView = UIView(frame: self.view.bounds)
        self.view.addSubview(architectView)
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(gestureRecognizer:)))
        gestureRecognizer.delegate = self
        architectView.addGestureRecognizer(gestureRecognizer)
    }

    func handleTap(gestureRecognizer: UIGestureRecognizer) {
        let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
10
mriaz0011