UIVut内部的UIVut没有响应手势

时间:2017-04-01 16:09:32

标签: ios swift uiview uibutton

我有一个内部有UIButton的UIView,但按钮无法识别手势

任何想法为什么?我尝试在与此相关的问题中使用解决方案,但没有成功。

全班代码:

import UIKit
import Foundation

public class HUDView: UIView , UIGestureRecognizerDelegate {
    var stopwatch: StopwatchView
    var gamePoints: CounterLabelView

     var hintButton: UIButton!

    required public init(coder aDecoder: NSCoder) {
        fatalError("Never call this... Use init(frame:)")
    }


    override init(frame: CGRect) {
        self.stopwatch = StopwatchView(frame: CGRect(x: ScreenWidth/2 - 150, y: 0, width: 300, height: 100))
        self.stopwatch.setSecondsRemaining(0)

        self.gamePoints = CounterLabelView(font: FontHUD!, frame: CGRect(x: ScreenWidth - 200, y: 30, width: 320, height: 70))
        gamePoints.textColor = UIColor.black
        gamePoints.value = 0

        super.init(frame: frame)

        self.addSubview(gamePoints)
        let pointsLabel = UILabel(frame: CGRect(x: ScreenWidth - 340, y: 30, width: 140, height: 70))
        pointsLabel.backgroundColor = UIColor.clear
        pointsLabel.font = FontHUD
        pointsLabel.text = " Points:"
        self.addSubview(pointsLabel)

        self.isUserInteractionEnabled = false
        self.addSubview(self.stopwatch)

        //load the button image
        let hintButtonImage = UIImage(named: "btn")!

        //the help button
        self.hintButton = UIButton()
      //  hintButton.perform(#selector(HUDView.s))
        hintButton.setTitle("Hint!", for:.normal)
        hintButton.titleLabel?.font = lHud
        hintButton.setBackgroundImage(hintButtonImage, for: .normal)
        hintButton.frame = CGRect(x: 50, y: 30, width: hintButtonImage.size.width, height: hintButtonImage.size.height)

       // hintButton.center = self.center
        //50, 30, hintButtonImage.size.width, hintButtonImage.size.height
        hintButton.alpha = 1.0
        hintButton.isUserInteractionEnabled = true
        self.addSubview(hintButton)

    //    hintButton.addTarget(self, action: #selector(self.tapButton(_:)), for: .touchUpInside)

        let g = UITapGestureRecognizer(target: self, action: #selector(self.h(_:)))
        g.delegate = self
        hintButton.addGestureRecognizer(g)
    }

    func h(_ sender: UITapGestureRecognizer) {
        print("hey!")
        fatalError()
    }

}

1 个答案:

答案 0 :(得分:2)

问题在于行 </head> <BODY> <script> function Something(date) { this.date = date; this.category = 1; } var crisislist = []; var hazelabel = []; var hazedata = []; crisislist[0] = new Something("01-02-2017"); crisislist[1] = new Something("22-03-2017"); crisislist[2] = new Something("22-03-2017"); crisislist[3] = new Something("07-08-2017"); for(var i = 0; i< crisislist.length; i++){ if(crisislist[i].category == 1) { if(!hazelabel[crisislist[i].date]) { hazelabel[crisislist[i].date] = crisislist[i].date; hazedata[crisislist[i].date] = 1; } else { hazedata[crisislist[i].date]++; } } } for(var key in hazelabel) { console.log(hazelabel[key]); console.log(hazedata[key]); } </script> </BODY> </HTML>

您要将self.isUserInteractionEnabled = false添加到subview。按钮点按不起作用,因为父视图即HUDView禁用了用户交互,因此将禁用所有HUDView的用户交互。 将更改设为subViews它将起作用。