将手势识别器添加到添加到 UIWindow 上的视图
Adding a gesture recogniser to a view which added on UIWindow
我在 UIWindow
上添加了一个子视图作为 toast view
,现在我在 2 秒后自动删除它(toast 视图)。但是我需要添加一个 swipe/tap gesture recogniser
以在用户 swipes/touches 时删除它。我尝试了很多但没有结果。
请问有什么办法可以实现吗,有解决办法请告知。谢谢
class func showToast(withDuration duration: TimeInterval, afterDelay delay: TimeInterval, withMessage message: String, toastType type: UINotificationFeedbackGenerator.FeedbackType, hideToastAfterCompletion: Bool) {
let notificationFeedback = UINotificationFeedbackGenerator()
let window = UIApplication.shared.keyWindow
let toastView = UIView()
toastView.tag = 999
toastView.accessibilityHint = "toastView"
toastView.backgroundColor = UIColor.clear
toastView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 80)
toastView.isUserInteractionEnabled = true
let toastLabelWidth = screenWidth*0.75
let xPosition = (screenWidth - toastLabelWidth)/2
let size = message.height(withConstrainedWidth: toastLabelWidth, font: UIFont.LatoRegular(16))
var topPadding: CGFloat = 0.0
if #available(iOS 11.0, *) {
topPadding = window?.safeAreaInsets.top ?? 0.0
}
topPadding = (topPadding == 0.0 ? 20.0 : topPadding)
let toastLabel = UILabel(frame: CGRect(x: xPosition, y: topPadding, width: toastLabelWidth, height: size))
toastLabel.text = message
toastLabel.numberOfLines = 0
toastLabel.textAlignment = .center
toastLabel.textColor = type.TextColor
toastLabel.font = UIFont.LatoRegular(16)
toastLabel.backgroundColor = UIColor.clear
toastView.addSubview(toastLabel)
toastView.frame.size.height = toastLabel.frame.origin.y + size + 32
removeExistedToast()
self.drawWave(forToastView: toastView, fillColor: type.ToastColor)
toastView.transform = CGAffineTransform(translationX: 0, y: -toastView.frame.height)
window?.addSubview(toastView)
notificationFeedback.notificationOccurred(type)
Toast.animateLayer(toastView: toastView)
let swipeGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(toastViewSwiped))
toastView.addGestureRecognizer(swipeGestureRecognizer)
animate(toast: toastView, withDelay: delay, duration: 0.5, transform: CGAffineTransform.identity, {
if [=10=] && hideToastAfterCompletion {
animate(toast: toastView, withDelay: delay + duration, duration: 0.25, transform: CGAffineTransform(translationX: 0, y: -toastView.frame.height), { _ in
toastView.removeFromSuperview()
})
}
})
}
@objc private func toastViewSwiped(_ gesture: UIGestureRecognizer) {
Toast.removeExistedToast()
}
class func removeExistedToast(){
let window = UIApplication.shared.keyWindow
window?.subviews.filter({ [=10=].tag == 999 && [=10=].accessibilityHint == "toastView" }).forEach({ (existedToast) in
UIView.animate(withDuration: 0.25, animations: {
existedToast.alpha = 0
}, completion: { (_) in
existedToast.removeFromSuperview()
})
})
}
我的代码在这里可以检查,当为手势识别器分配目标或无法添加目标时是否有任何错误。
将 'toastView' 和 'toastLabel' 设为全局 'Toast' 并举个例子,
private var window: UIWindow!
public var toastView: UIView!
private var toastLabel: UILabel!
private static let sharedInstance = Toast()
class func shared() -> Toast {
return sharedInstance
}
我替换了下面的代码,
animate(toast: toastView, withDelay: delay + duration, duration: 0.25, transform: CGAffineTransform(translationX: 0, y: -toastView.frame.height), { _ in
toastView.removeFromSuperview()
})
和
self.perform(#selector(self.removeExistedToast), with: nil, afterDelay: delay + duration)
通过
func removeExistedToast()
为
@objc func removeExistedToast()
我在 UIWindow
上添加了一个子视图作为 toast view
,现在我在 2 秒后自动删除它(toast 视图)。但是我需要添加一个 swipe/tap gesture recogniser
以在用户 swipes/touches 时删除它。我尝试了很多但没有结果。
请问有什么办法可以实现吗,有解决办法请告知。谢谢
class func showToast(withDuration duration: TimeInterval, afterDelay delay: TimeInterval, withMessage message: String, toastType type: UINotificationFeedbackGenerator.FeedbackType, hideToastAfterCompletion: Bool) {
let notificationFeedback = UINotificationFeedbackGenerator()
let window = UIApplication.shared.keyWindow
let toastView = UIView()
toastView.tag = 999
toastView.accessibilityHint = "toastView"
toastView.backgroundColor = UIColor.clear
toastView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 80)
toastView.isUserInteractionEnabled = true
let toastLabelWidth = screenWidth*0.75
let xPosition = (screenWidth - toastLabelWidth)/2
let size = message.height(withConstrainedWidth: toastLabelWidth, font: UIFont.LatoRegular(16))
var topPadding: CGFloat = 0.0
if #available(iOS 11.0, *) {
topPadding = window?.safeAreaInsets.top ?? 0.0
}
topPadding = (topPadding == 0.0 ? 20.0 : topPadding)
let toastLabel = UILabel(frame: CGRect(x: xPosition, y: topPadding, width: toastLabelWidth, height: size))
toastLabel.text = message
toastLabel.numberOfLines = 0
toastLabel.textAlignment = .center
toastLabel.textColor = type.TextColor
toastLabel.font = UIFont.LatoRegular(16)
toastLabel.backgroundColor = UIColor.clear
toastView.addSubview(toastLabel)
toastView.frame.size.height = toastLabel.frame.origin.y + size + 32
removeExistedToast()
self.drawWave(forToastView: toastView, fillColor: type.ToastColor)
toastView.transform = CGAffineTransform(translationX: 0, y: -toastView.frame.height)
window?.addSubview(toastView)
notificationFeedback.notificationOccurred(type)
Toast.animateLayer(toastView: toastView)
let swipeGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(toastViewSwiped))
toastView.addGestureRecognizer(swipeGestureRecognizer)
animate(toast: toastView, withDelay: delay, duration: 0.5, transform: CGAffineTransform.identity, {
if [=10=] && hideToastAfterCompletion {
animate(toast: toastView, withDelay: delay + duration, duration: 0.25, transform: CGAffineTransform(translationX: 0, y: -toastView.frame.height), { _ in
toastView.removeFromSuperview()
})
}
})
}
@objc private func toastViewSwiped(_ gesture: UIGestureRecognizer) {
Toast.removeExistedToast()
}
class func removeExistedToast(){
let window = UIApplication.shared.keyWindow
window?.subviews.filter({ [=10=].tag == 999 && [=10=].accessibilityHint == "toastView" }).forEach({ (existedToast) in
UIView.animate(withDuration: 0.25, animations: {
existedToast.alpha = 0
}, completion: { (_) in
existedToast.removeFromSuperview()
})
})
}
我的代码在这里可以检查,当为手势识别器分配目标或无法添加目标时是否有任何错误。
将 'toastView' 和 'toastLabel' 设为全局 'Toast' 并举个例子,
private var window: UIWindow!
public var toastView: UIView!
private var toastLabel: UILabel!
private static let sharedInstance = Toast()
class func shared() -> Toast {
return sharedInstance
}
我替换了下面的代码,
animate(toast: toastView, withDelay: delay + duration, duration: 0.25, transform: CGAffineTransform(translationX: 0, y: -toastView.frame.height), { _ in
toastView.removeFromSuperview()
})
和
self.perform(#selector(self.removeExistedToast), with: nil, afterDelay: delay + duration)
通过
func removeExistedToast()
为
@objc func removeExistedToast()