gpt4 book ai didi

ios - 控制中心 View

转载 作者:行者123 更新时间:2023-11-28 15:02:56 26 4
gpt4 key购买 nike

我正在尝试在 iPhone 中创建类似控制中心的 View 。根据我的要求,当用户向上切换时, View 必须位于底部, View 需要从下到上出现,而关闭 View 需要从底部切换。就像控制中心一样,我需要一个 View ,有人可以帮助我吗?

最佳答案

您可以通过以下代码实现:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

var viewControlCenter : UIView!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.
createControlCenterView()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

// MARK: - Gesture Methods

@objc func respondToSwipeGesture(sender: UIGestureRecognizer? = nil) {
if let swipeGesture = sender as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.up:
if self.viewControlCenter.frame.origin.y != 0 {
UIView.animate(withDuration: 0.5, animations: {
self.viewControlCenter.frame.origin.y = 0
}, completion: nil)
}
break
case UISwipeGestureRecognizerDirection.down:
if self.viewControlCenter.frame.origin.y == 0 {
UIView.animate(withDuration: 0.5, animations: {
self.viewControlCenter.frame.origin.y = self.view.frame.size.height
}, completion: nil)
}
break
default:
break
}
}
}

@objc func onViewTapped(sender: UITapGestureRecognizer? = nil) {
if self.viewControlCenter.frame.origin.y == 0 {
UIView.animate(withDuration: 0.5, animations: {
self.viewControlCenter.frame.origin.y = self.view.frame.size.height
}, completion: nil)
}
}

// MARK: - Custom Methods

func createControlCenterView() {
viewControlCenter = UIView.init(frame: self.view.bounds)
self.viewControlCenter.frame.origin.y = self.view.frame.size.height

let viewBG : UIView = UIView.init(frame: self.view.bounds)
viewBG.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.6)
viewControlCenter.addSubview(viewBG)

self.view.addSubview(viewControlCenter)

let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(sender:)))
swipeUp.direction = .up
self.view.addGestureRecognizer(swipeUp)

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(sender:)))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)

let tap = UITapGestureRecognizer(target: self, action: #selector(onViewTapped(sender:)))
tap.delegate = self
viewControlCenter.addGestureRecognizer(tap)
}
}

关于ios - 控制中心 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48740644/

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