gpt4 book ai didi

ios - Swift - 如果在 View 外部触摸则隐藏 View

转载 作者:行者123 更新时间:2023-11-28 10:06:23 25 4
gpt4 key购买 nike

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

I would like to hide the Calendar when the calendar is displayed and if the user touch outside of it.

类似的东西:

enter image description here

如果我点击日历以外的地方,日历就会消失。

我试图插入一个 subview 并在其上添加一个 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
}

}

}

最佳答案

UITapGestureRecognizer 添加到您的 self.viewUITapGestureRecognizer 构造函数的 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
}

}

关于ios - Swift - 如果在 View 外部触摸则隐藏 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52716543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com