UITapGestureRecognizer无法正常工作

时间:2018-04-10 10:03:34

标签: ios swift uitapgesturerecognizer

我创建了函数updateItems(),它从一个数组中创建UIView中的许多UIScrollView

以下是此功能的文件:

class MainViewController: UIViewController {

    @IBOutlet weak var body: UIScrollView!
    @IBOutlet weak var edit: UIButton!

    var _title: String = "Title"
    var _isEditing: Bool = false
    var firstItems: [UISectionView] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationController?.navigationBar.topItem?.title = self._title
        navigationController?.navigationItem.largeTitleDisplayMode = .automatic

        body.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height + 100)

        self.updateItems(self.firstItems)

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    public func updateItems(_ s: [UISectionView]) {
        let topMargin = 10
        let rightMargin = 10
        let leftMargin = 10
        let space = 5
        let heightItem = 60
        var b = topMargin

        for i in body.subviews {
            i.removeFromSuperview()
        }

        for t in s {
            if t.isHidden == true {
                continue
            }
            if t.title != nil {
                let f = UIFont(name: "Helvetica", size: 20)
                let l = UILabel(frame: CGRect(x: rightMargin, y : b, width: Int(UIScreen.main.bounds.width) - (rightMargin + leftMargin), height: Int(f!.lineHeight)))
                l.font = f
                l.text = t.title
                body.addSubview(l)
                b = b + Int(f!.lineHeight) + space
            }
            for i in t.items{
                body.addSubview(i.getView(frame: CGRect(x: rightMargin, y: b, width: Int(UIScreen.main.bounds.width) - (rightMargin + leftMargin), height: heightItem), view: self))
                b = b + heightItem + space
            }
        }
    }

}

提示: UISectionView是一个包含UIItemView

数组的对象

对象UIItemView看起来像:

class UIItemView {

    var icon: UIImage = UIImage();
    var line1: rString = rString("")!;
    var line2: rString = rString("")!;
    var leftline: Any = String();
    var background: String = "white"
    var onItemTap: (_ sender: UITapGestureRecognizer?) -> () = {sender in }
    var onItemLongPress: (_ sender: UILongPressGestureRecognizer?) -> () = {sender in }

    var id: String

    init?(_ id: String) {
        self.id = id
    }

    public func getView(frame: CGRect, view: UIViewController) -> UIView {
        let width = Int(frame.width)
        let height = Int(frame.height)
        let rightMargin = 20
        let leftMargin = 10
        let topMargin = 10
        let bottomMargin = 10
        let iconSide = height - (topMargin + bottomMargin)
        let marginLine = leftMargin + iconSide + 10

        let v = UIView(frame: frame)

        //Background & shape
        if self.background == "white" {
            v.backgroundColor = UIColor.white;
        } else if self.background == "blur" {
            let bEV = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.extraLight))
            bEV.frame = v.bounds
            bEV.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            v.addSubview(bEV)
        }
        v.layer.cornerRadius = 10.0

        //Icon
        let i = UIImageView()
        i.image = self.icon;
        i.frame = CGRect(x: leftMargin, y: topMargin, width: iconSide, height: iconSide)
        v.addSubview(i)

        //First Line
        let l1 = self.line1.getLabel()
        l1.frame = CGRect(x: marginLine, y: topMargin, width: width - (marginLine + leftMargin), height: Int(self.line1.getFont().lineHeight))
        v.addSubview(l1)

        //Seconde Line
        let l2 = self.line2.getLabel()
        l2.frame = CGRect(x: marginLine, y: height - (bottomMargin + Int(self.line1.getFont().lineHeight)), width: width - (marginLine + leftMargin), height: Int(self.line1.getFont().lineHeight))
        v.addSubview(l2)

        //Left Line
        if type(of: self.leftline) == type(of: SpinnerView()) {
            let sv = (self.leftline as! SpinnerView)
            sv.frame = CGRect(x: width - (rightMargin + iconSide), y: height/2 - iconSide/2, width: iconSide, height: iconSide)
            v.addSubview(sv)
        } else if type(of: self.leftline) == type(of: rString("")) {
            let rS = (self.leftline as! rString)
            if rS.text != "" {
                rS.fontName = "HelveticaNeue-Bold"
                rS.size = 15
                rS.color = UIColor(red:0.01, green:0.48, blue:1.00, alpha:1.0)
                let l3 = rS.getLabel()
                l3.frame = CGRect(x: width - (rightMargin + Int(rS.getFont().lineWidth(rS.text)) + 15), y: height/2 - (Int(rS.getFont().lineHeight) + 10)/2, width: Int(rS.getFont().lineWidth(rS.text)) + 15, height: Int(rS.getFont().lineHeight) + 10)
                l3.backgroundColor = UIColor(red:0.94, green:0.94, blue:0.97, alpha:1.0)
                l3.layer.masksToBounds = true
                l3.layer.borderWidth = 2
                l3.layer.borderColor = UIColor(red:0.94, green:0.94, blue:0.97, alpha:1.0).cgColor
                l3.layer.cornerRadius = rS.getFont().lineHeight/1.2
                l3.textAlignment = .center
                v.addSubview(l3)
            }
        }

        //GestureRecognizer
        v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.oIT(_:))))
        v.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(self.oILP(_:))))
        v.restorationIdentifier = self.id
        return v;
    }

    @objc func oIT(_ sender: UITapGestureRecognizer) {
        print("Tap")
        self.onItemTap(sender)
    }
    @objc func oILP(_ sender: UILongPressGestureRecognizer) {
        print("LongPress")
        self.onItemLongPress(sender)
    }

    static func ==(lhs: UIItemView, rhs: UIItemView) -> Bool {
        return lhs === rhs
    }

}

提示: UIItemView包含函数getView(),该函数返回特定的UIView

问题:

一切正常,当我加载ViewController(其中有UIScrollView)时,每个UIView都按照我想要的方式构建,我可以与{{1 } UIViewUITapGestureRecognizer(函数按预期调用)

BUT

当我第二次调用函数UILongPressGestureRecognizer而未重新加载updateItems()时,项目会按预期更改,但ViewControllerUITapGestureRecognizer不起作用更多。

我希望你能帮助我:D 如果您缺少信息以便了解问题,请告诉我们;)

0 个答案:

没有答案