2017-09-15 52 views
1

我想添加一个点击手势到一个UIView,但手势没有得到承认。点击手势事件不工作在UIView

“iconBadgeView”是一个UIView,它带有一个在参数中传递的已定义大小的图像。

lazy var cardioVascularIcon : IconBadgeView! = { 

    let iconBadgeView = IconBadgeView(frame: CGRect(x: 0, y: 0, width: 95, height: 95), data:["big":"db_history"]) 

    let tapEvent = UITapGestureRecognizer(target: self, action: #selector(loadNewView(sender:))) 

    tapEvent.numberOfTapsRequired = 1 
    iconBadgeView.translatesAutoresizingMaskIntoConstraints = false 

    iconBadgeView.isUserInteractionEnabled = true  
    iconBadgeView.addGestureRecognizer(tapEvent) 
}() 

有连接到同一类中的委托和功能实现如下:

func loadNewView(sender: UITapGestureRecognizer) { 
    print("Tapped") 
} 

功能loadNewView是没有得到调用。我不确定代码中的错误。如果有人能帮忙,请帮忙。

我加入iconBadgeView为低于上海华:

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

    addSubview(cardioVascularIcon) 

    cardioVascularIcon.pinToSuperview([.Top]) 
    cardioVascularIcon.pinToSuperview([.Left], constant: 92) 
    cardioVascularIcon.pinToSuperview([.Right], constant: 92) 
} 
+0

'让tapEvent = UITapGestureRecognizer(目标:自我,动作:#selector(loadNewView(发件人:))) tapEvent.addTarget(iconBadgeView,动作:#selector(loadNewView(发件人:))) '你为什么要重复你自己在这里我的朋友哈哈。如果你要创建一个带有'default constructor'意味着'UITapGestureRecognizer()'的手势,那么你将需要为你的选择器提供'addTarget'方法,但是由于你使用了需要选择器和目标的自定义构造器,所以需要有第二行代码:) – Lamar

+0

我已经尝试删除重复的行,但它没有帮助 – user2122178

+0

你有没有得到任何错误/问题?仅仅因为好奇心,你把'iconBadgeView'添加到你的superView中了吗?也因为你禁用'translatesAutoresizingMaskIntoConstraints'确保你将这些约束添加到它,如果现在你可能认为你在屏幕上查看,它不是:) – Lamar

回答

1

我找到了解决方法。我已经使用了一个按钮,而不是一个标签,现在的事情工作得很好。下面是我使用的代码:

func createButton (buttonWidth : CGFloat?, buttonTitle : String?, buttonFont : UIFont?, imageName : String?, buttonColor : UIColor?) -> UIButton { 
    let newButton = UIButton(type: . custom) 
    newButton.showsTouchWhenHighlighted = false 
    newButton.translatesAutoresizingMaskIntoConstraints = false 
    newButton.adjustsImageWhenHighlighted = false 

    if let title = buttonTitle { 
     newButton.setTitle(title, for: .normal) 
    } 
    if let color = buttonColor { 
     if let _ = newButton.titleLabel { 
      newButton.setTitleColor(color, for: .normal) 
     } 
    } 

    if let btnWidth = buttonWidth { 
     newButton.frame = CGRect(x: 0, y: 0, width: btnWidth, height: btnWidth) 
     newButton.layer.cornerRadius = 0.5 * newButton.bounds.size.width 
     newButton.clipsToBounds = true 
    } 
    if let img = imageName { 
     newButton.setImage(UIImage(named: img), for: .normal) 
    } 
    if let font = buttonFont { 
     newButton.titleLabel?.font = font 
    } 

    return newButton 
} 
let addDiagButton = self.createButton(buttonWidth: nil, buttonTitle: addButtonTitle, buttonFont: UIFont.regularDisplayOfSize(30), imageName: nil, buttonColor: UIColor(red: 111, green: 160, blue: 186)) 

addDiagButton.addTarget(self, action: #selector(addDiag(sender:)), for: .touchUpInside) 

上面的代码有它创建了一个按钮,也与它重视的触发事件的共同作用。代码工作正常。

为了使其表现类似于标签点击,我在createButton函数中添加了一行。

newButton.adjustsImageWhenHighlighted =假

点击时这将限制按钮的闪光效果。

0

你iconBadgeView消失,因为它是局部变量。

您必须先启动cardioVascularIcon var。

lazy var cardioVascularIcon : IconBadgeView! = { 

    cardioVascularIcon.frame = CGRect(x: 0, y: 0, width: 95, height: 95) 
    //here call function which sets data property 

    let tapEvent = UITapGestureRecognizer(target: self, action: #selector(loadNewView(sender:))) 

    tapEvent.numberOfTapsRequired = 1 
    cardioVascularIcon.translatesAutoresizingMaskIntoConstraints = false 

    cardioVascularIcon.isUserInteractionEnabled = true  
    cardioVascularIcon.addGestureRecognizer(tapEvent) 
}() 
+0

嗨,谢谢为了答案,但恐怕它不起作用。 – user2122178

+0

这是在屏幕上可见的视图?缺少添加这个新视图来查看层次结构的代码。 – Dan

+0

是的,该视图在屏幕上可见。 – user2122178