【问题标题】:Set picker values by tapping anywhere outside of a popup view in iOS通过点击 iOS 中弹出视图之外的任何位置来设置选择器值
【发布时间】:2017-05-17 14:40:55
【问题描述】:

目前有一个弹出视图,通过点击 SET 按钮设置 3 个选择器值:
但是,我想完全删除 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)
    }

【问题讨论】:

  • 您的 Popup 只是您呈现的视图还是 viewController?
  • @SyedQamarAbbas 我相信它是 viewController 中的一个视图:class FocusHomeViewController: UIViewController { let pickerTitles = ["FOCUS", "BREAK", "CYCLES"] ...

标签: ios swift datepicker uipickerview


【解决方案1】:

如果 Previous Black View 是 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,该视图的颜色为黑色不透明。然后在上面检查哪个视图是触摸的方法中,检查是否触摸的视图是你的黑色视图。 假设你黑色视图的IBOutlet 是backgroundView 那么上面的检查会是这样的。

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.

}

【讨论】:

  • 您是否有机会详细说明...例如// Check that the touched view is your background view 是什么样的?以及如何将其与我在帖子中提供的代码集成。我拿起了这个项目,只是 iOS 的初学者。
  • 我正在执行您的建议。请参阅我上面的编辑。我收到 addTarget 函数的错误。
  • 另外,澄清一下,黑色背景是默认viewController,而pickerContainerView是@IBOutlet。
  • 那个黑色的视图覆盖了整个屏幕,黑色过度。容器可能是具有相同大小的选择器视图的视图
  • 谢谢,解决了!我只需要将 pickerDidSet 函数添加到您的代码中。但是,有一个问题......你看到那个红色圆圈,它是一个按钮。如果用户在 IBOutlet 位于顶部时点击它,它会触发,而不触发 touchesBegan 代码。那么我怎样才能让它首先运行 touchesBegan 然后触发按钮。
【解决方案2】:

touchesBegan 如果触摸的对象是按钮,则方法不起作用,因此按照您的逻辑。 您需要检查 PickerView 是否可见,然后禁用它而不是触发该按钮的其他功能。

示例。

Create a boolean variable named isPickerViewVisible in your class and when picker view is going to visible make it true and when picker view is getting hide just make it 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.
    }
}

【讨论】:

    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    相关资源
    最近更新 更多