通过点击iOS中弹出视图之外的任何位置来设置选取器值

时间:2017-05-17 14:40:55

标签: ios swift datepicker uipickerview

目前有一个弹出视图,通过点击SET按钮设置3个选择器值: Current SET button
但是,我想完全删除SET按钮,并在弹出窗口之外设置选择器值,这样就会隐藏弹出窗口。

以下是当前代码:

    // function for selecting picker values
func pickerDidSet() {
        let focusPeriodChoice = focusPeriodDataSource[pickerView.selectedRow(inComponent: 0)]
        let breakPeriodChoice = breakPeriodDataSource[pickerView.selectedRow(inComponent: 1)]
        let repeatCountChoice = repeatCountDataSource[pickerView.selectedRow(inComponent: 2)]

        persistPickerChoice(focusPeriodChoice, dataType: .focusPeriod)
        persistPickerChoice(breakPeriodChoice, dataType: .breakPeriod)
        persistPickerChoice(repeatCountChoice, dataType: .repeatCount)

        timerSummaryLabel.text = "\(focusPeriodChoice)m • \(breakPeriodChoice)m • \(repeatCountChoice)x"

        UIView.animate(withDuration: 0.2, animations: { self.pickerContainerView.alpha = 0.0 }, completion: { finished in
            self.pickerContainerView.isHidden = true
        })
    }

// Open popup, by tapping gear icon
    @IBAction func openSettings(_ sender: Any) {
        pickerView.selectRow(pickerChoiceIndex(forDataType: .focusPeriod), inComponent: 0, animated: false)
        pickerView.selectRow(pickerChoiceIndex(forDataType: .breakPeriod), inComponent: 1, animated: false)
        pickerView.selectRow(pickerChoiceIndex(forDataType: .repeatCount), inComponent: 2, animated: false)

        self.pickerContainerView.isHidden = false
        UIView.animate(withDuration: 0.2) {
            self.pickerContainerView.alpha = 1.0
        }
    }

// Once pickers have been set, display the summary
private func configureSummaryLabel() {
        let focusPeriodChoice = pickerChoice(forDataType: .focusPeriod)
        let breakPeriodChoice = pickerChoice(forDataType: .breakPeriod)
        let repeatCountChoice = pickerChoice(forDataType: .repeatCount)

        timerSummaryLabel.text = "\(focusPeriodChoice)m • \(breakPeriodChoice)m • \(repeatCountChoice)x"
    }

// Setting the picker “SET” button
private func addPickerSetButton(atX x: CGFloat, centerY: CGFloat) {
        pickerSetButton.frame = CGRect(x: x, y: 0, width: 40, height: 20)
        pickerSetButton.center = CGPoint(x: pickerSetButton.center.x, y: centerY)
        pickerSetButton.setTitle("SET", for: .normal)
        pickerSetButton.setTitleColor(UIColor.white, for: .normal)
        pickerSetButton.setTitleColor(UIColor.darkGray, for: .highlighted)
        pickerSetButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        pickerSetButton.addTarget(self, action: #selector(pickerDidSet), for: .touchUpInside)
        pickerHeaderView.addSubview(pickerSetButton)
    }

2 个答案:

答案 0 :(得分:1)

如果上一个黑色视图是ViewController的默认视图,那么您只需要在下面的方法中实现。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        // Check that the touched view is your background view
        if touches.first?.view == self.view {
            // Do What Every You want to do
        }
}

<强>详细 每个ViewController都有一个默认的view对象。在您的情况下,显示在弹出窗口后面的黑色叠加层似乎是该视图控制器的默认视图。如果该黑色叠加层不是您的默认视图,则创建该视图的IBOutlet,该视图的颜色为黑色不透明。然后在上面的方法中,您检查哪个视图是触摸检查,如果触摸的视图是您的黑色视图。 假设您的黑色视图IBOutletbackgroundView,则上述检查将是这样的。

if touches.first?.view == self.backgroundView {
    //It means you have touched outside the pop and out side the pop there is only your backgroundView.
    //Here you should do exactly the same which you were doing when `SET` button was clicked.

}

答案 1 :(得分:1)

如果触摸的对象是按钮,那么

touchesBegan方法无法正常工作。 您需要检查PickerView是否可见,然后禁用它,而不是触发该按钮的其他功能。

示例

在你的类中创建一个名为isPickerViewVisible的布尔变量,当选择器视图可见时使其成为真,当选择器视图获得隐藏时,只需将其设为false。该红色按钮可能有一个IBAction。

@IBAction didTapButton(_ sender: Any){
    //Here you need to check if pickerView is open then disable it. I don't know what logic you have implemented to show picker view.
    if isPickerViewVisible {
        self.pickerDidSet()
    }else {
        //Here you should do the task that you do on clicking this button.
    }
}