swift - 我正在尝试制作一张在快速滑动时滑动的卡片
问题描述
我在动画和 PanGestureRecognizers 方面遇到了困难,我想要一个通过用手指向上拖动或黎明滑动的视图。它有 4 个部分 1 - 2 需要更改高度、宽度和底部约束 2 - 3 高度是最大高度,您可以将其滑入或滑出 3 - 4 卡片处于最大高度,您可以刷卡下
谢谢!
解决方案
我就是这样做的。
var theView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// First create your view.
theView = UIView(frame: CGRect(x: 40, y: self.view.bounds.height - 120, width: self.view.frame.width - 80, height: 100))
// set its background color
theView.backgroundColor = UIColor.black
// Create the round corners
theView.layer.cornerRadius = 20
// And add it to the view
self.view.addSubview(theView)
// Create the UIPanGestureRecognizer and assign the function to the selector
let gS = UIPanGestureRecognizer(target: self, action: #selector(growShink(_:)))
// Enable user interactions on the view
theView.isUserInteractionEnabled = true
// Add the gesture recognizer to the view
theView.addGestureRecognizer(gS)
}
// The control value is used to determine wether the user swiped up or down
var controlValue:CGFloat = 0
var animating = false
// The function called when the user swipes
@objc func growShink(_ sender:UIPanGestureRecognizer){
let translation = sender.location(in: theView)
// Check the control value to see if it has been set
if controlValue != 0 && !animating{
// if it has been set check that the control value is greater than the new value recieved by the pan gesture
if translation.x < controlValue{
animating = true
// If it is, change the values of the frame using animate
UIView.animate(withDuration: 0.5, animations: {
self.theView.frame = CGRect(x: 0, y: self.view.bounds.height - 300, width: self.view.frame.width, height: 300)
}, completion: {finished in
UIView.animate(withDuration: 1.0, animations: {
self.theView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
}, completion: {finished in self.animating = false; self.controlValue = 0})})
}else if translation.x > controlValue{
animating = true
// else, change the values of the frame back.
UIView.animate(withDuration: 0.5, animations: {
self.theView.frame = CGRect(x: 0, y: self.view.bounds.height - 300, width: self.view.frame.width, height: 300)
}, completion: {finished in
UIView.animate(withDuration: 1.0, animations: {
self.theView.frame = CGRect(x: 40, y: self.view.bounds.height - 120, width: self.view.frame.width - 80, height: 100)
}, completion: {finished in self.animating = false; self.controlValue = 0})})
}
}
// set the control value to the value received from the pan gesture
controlValue = translation.x
}
}
您可以根据需要调整宽度和高度值。
推荐阅读
- java - 在提交表单的同一 jsp 中显示成功消息
- javascript - 处理类构造函数中的 promise 错误
- redirect - 将子域重定向到 Odoo 数据库
- elasticsearch - 无法更改 LXC 上的 vm.max_map_count
- javascript - React useRef 或模块范围以存储与 UI 无关的状态
- dart - 从 Dart 中的 HttpClientResponse 类中检索字符串
- react-native - React Native 从 Zomato API 获取集合
- java - 如何在 Ubuntu 18.04.1 LTS 上的 VS Code 中导入 javafx?
- kubernetes - 使用 Helm 更改默认 StorageClass
- swiftui - SwiftUI:单击 NavigationLink 时后退按钮消失