2017-09-15 82 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