2017-04-01 58 views
-4

我有里面一个UIButton一个UIView但按钮无法识别手势的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() 
    } 

} 
+0

为什么你不直接使用addtarget而不是手势 –

+0

它不工作aswell @ Anbu.Karthik – Aryan

+0

显示你创建HUDView并将其放入界面的代码。这是错误的代码。 – matt

回答

1

的问题是一致self.isUserInteractionEnabled = false

您要添加按钮的subviewHUDView。按钮点按不起作用,因为父视图即:HUDView禁用了用户交互,因此所有subViews的用户交互都将被禁用。 进行更改为self.isUserInteractionEnabled = true它将起作用。