UIButton不响应第一次单击

时间:2018-06-22 04:06:27

标签: xcode uibutton swift4

我在那里有3个按钮,这些按钮是我使用自定义子类DropdownBtn进行编程编写的。我已将目标添加到所有按钮,以便它们具有功能。现在出现了问题,当您按下三个按钮之一时,将打开一个下拉菜单,而当您按下另一个按钮时,应该打开该下拉菜单。但是,您必须再次单击它才能打开下拉菜单。为什么要点击两次才能打开下拉菜单?如果您需要其他任何信息,请发表评论,我会尽快与您联系。

这是DropdownBtn类

NaN

这是DropdownProtocol

const has = select => array => {
  const set = new Set(array.map(select))
  return (object, index, array) => set.has(select(object, index, array))
}

const hasId = has(({ id }) => id)
const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }]
const array2 = [{ id: 1 }, { id: 2 }]

const result1 = array1.every(hasId(array2))
const result2 = array2.every(hasId(array1))

console.log(result1)
console.log(result2)

这是DropdownView类

import Foundation
import UIKit

class DropdownBtn : UIButton, DropdownProtocol {

func dropDownPressed(string: String) {
    self.setTitle(string, for: .normal)
    self.dismissDropDown()
}

var dropView = DropdownView()
var height = NSLayoutConstraint()

override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = UIColor.darkGray

    dropView = DropdownView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
    dropView.delegate = self
    dropView.translatesAutoresizingMaskIntoConstraints = false
}

override func didMoveToSuperview() {
    self.superview?.addSubview(dropView)
    self.superview?.bringSubview(toFront: dropView)
    dropView.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    dropView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
    height = dropView.heightAnchor.constraint(equalToConstant: 0)
}

func activateDropDown() {
    NSLayoutConstraint.deactivate([self.height])

    if self.dropView.tableView.contentSize.height > 135 {
        self.height.constant = 135
    } else {
        self.height.constant = self.dropView.tableView.contentSize.height
    }

    NSLayoutConstraint.activate([self.height])

    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
        self.dropView.layoutIfNeeded()
        self.dropView.center.y += self.dropView.frame.height / 2
    }, completion: nil)
}

func dismissDropDown() {
    NSLayoutConstraint.deactivate([self.height])
    self.height.constant = 0
    NSLayoutConstraint.activate([self.height])

    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
        self.dropView.center.y -= self.dropView.frame.height / 2
        self.dropView.layoutIfNeeded()
    }, completion: nil)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

最后是MenuViewController类

import Foundation
import UIKit

protocol DropdownProtocol {
    func dropDownPressed(string: String)
}

需要明确的是,每个类和/或协议都在各自独立的swift文件中。

谢谢:)

0 个答案:

没有答案