【问题标题】:Swift - Hide view if touch outside of itSwift - 如果在其外部触摸则隐藏视图
【发布时间】:2019-03-13 23:12:57
【问题描述】:

我目前正在我的应用程序上编写日历系统。 我已经使用我创建的 toogleCalendar() 函数显示/隐藏带有 UIButton 的日历。

我想在显示日历时隐藏日历并且 如果用户触摸它的外部。

类似的东西:

如果我在日历之外点击,日历就会消失。

我尝试插入一个子视图并在其上添加一个 GestureRecognizer,但它不起作用。

你知道我该怎么做吗?

这是切换功能:

func toggleCalendar(){
      if !calendarIsHidden {
         calendarIsHidden = true

         UIView.animate(withDuration: 0.2, animations: {
            self.shadowCalendarView.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
            self.shadowCalendarView.alpha = 0
         }) { (finish) in
            if finish {
               self.shadowCalendarView.isHidden = self.calendarIsHidden
            }
         }

      } else {

         calendarIsHidden = false
         self.shadowCalendarView.isHidden = self.calendarIsHidden

         UIView.animate(withDuration: 0.2, animations: {
            self.shadowCalendarView.transform = .identity


            self.shadowCalendarView.alpha = 1

         }) { (finish) in
            // Nothing for now
         }

      }

}

【问题讨论】:

  • 你试过调试你的代码吗?发生了什么? (您遇到问题的部分是什么?)
  • 在 shadowCalendarView 上添加一个按钮并隐藏日历。 Flow 应该是 Btn、Calendar 中的 shadowCalendarView。如果您添加点击手势它也适用于日历,那么它不会是与日历的正确用户交互
  • 向 self.view 添加轻击手势。
  • 你是如何实现日历下方视图的暗淡效果的?向该视图添加点击手势。

标签: ios swift swift3 touch uigesturerecognizer


【解决方案1】:

当子视图打开时,您看到屏幕如何模糊为较深的颜色?您可以在日历下方添加一个带有 0.5(例如)alpha 的黑色视图,然后像这样添加一个 UIGestureTapRecognizer (我假设这就是 shadowCalendarView 的用途?):

// Somewhere in the code, for example on viewDidLoad if its a UIViewController, or anywhere in your dismissal function:
shadowCalendarView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(yourClassName.handleDismiss)))

@objc func handleDismiss() {
    // Dismisses the calendar with fade animation
    UIView.animate(withDuration: 0.3, animations: {
        self.calendarView.alpha = 0.0
        self.shadowCalendarView.alpha = 0.0
    }) { ( finished ) in
        self.calendarView.isHidden = true
        self.shadowCalendarView.isHidden = true
        // If you are implementing calendar as a child view, remove it in the completion block of the animation like so:
        self.willMove(toParentViewController: nil)
        self.view.removeFromSuperview()
        self.removeFromParentViewController()
    }
}

如果这个答案不够清楚,请尝试提供更多信息,我会尝试进行相应的编辑。比如实现,是不是一个新的ViewController作为childView?它只是一个视图吗?

【讨论】:

    【解决方案2】:

    将UITapGestureRecognizer 添加到您的self.view 并在UITapGestureRecognizer 的构造函数的action 部分内,您将输入一个函数,您将在其中显示用于检查您的日历 呈现/启用,并且可以进一步隐藏它。

    let mytapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
    self.view.addGestureRecognizer(mytapGestureRecognizer)
    self.isUserInteractionEnabled = true
    
    func handleTap(_ sender:UITapGestureRecognizer){
    
      if !calendarIsHidden {
       calendarIsHidden = true
    
       //Hide the calendar here
    
      }
      else {
       calendarIsHidden = false
    
       // Show the Calendar Here
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 2017-02-18
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 2020-11-11
      • 2017-07-12
      相关资源
      最近更新 更多