首页 > subviews的tapgesture有时候能识别到,有时候不能

subviews的tapgesture有时候能识别到,有时候不能

我给所有的子view设置点击事件,但是这些view的点击事件有时候可以响应有时候不行

import UIKit

class BottomContainerView: UIView {
    
    var clickItemProtocol : ClickItemProtocol?
    
    override func layoutSubviews() {
        
        let viewWidth = self.bounds.width
        let subDeviceWidth = (viewWidth-12)/3
        var startXpos : CGFloat =  2
        var startXXpos : CGFloat  =  2
        for i in 0 ..< subviews.count{
            let view = subviews[i]
            let uiTapGesture = UITapGestureRecognizer(target: self, action: "click:")
            uiTapGesture.numberOfTapsRequired = i
            view.userInteractionEnabled = true
            view.addGestureRecognizer(uiTapGesture)
            if i < 3{
                view.frame.origin.x = startXpos
                view.frame.origin.y = 2
                view.bounds.size.width = subDeviceWidth
                view.bounds.size.height = subDeviceWidth
                startXpos = startXpos + 4 + subDeviceWidth
            }else if i>=3 && i < 6{
                view.frame.origin.x = startXXpos
                view.frame.origin.y = 6 + subDeviceWidth
                view.bounds.size.width = subDeviceWidth
                view.bounds.size.height = subDeviceWidth
                startXXpos = startXXpos + 4 + subDeviceWidth
            }
        }
    }
    
    func setOnItemClickProtocol(clickItemProtocol : ClickItemProtocol){
        self.clickItemProtocol = clickItemProtocol
    }
    
    func click(sender:UITapGestureRecognizer){
        print("click")
        self.clickItemProtocol?.onItemClick(sender.numberOfTapsRequired)
    }

}

protocol ClickItemProtocol{
    func onItemClick(position : Int)
}

uiTapGesture.numberOfTapsRequired = i

你确定需要连续点 N 下才响应?

【热门文章】
【热门文章】