I';我想制作一张在swift中刷卡时滑动的卡片
我很难使用动画和PangestureRecognitors,我想要一个通过向上拖动或用手指拖动来滑动的视图。它有四个部分 1-2需要更改高度、宽度和底部约束 2-3高度是最大高度,您可以将其滑入或滑出 3-4卡处于最大高度,您可以向下滑动I';我想制作一张在swift中刷卡时滑动的卡片,swift,xcode,animation,uipangesturerecognizer,Swift,Xcode,Animation,Uipangesturerecognizer,我很难使用动画和PangestureRecognitors,我想要一个通过向上拖动或用手指拖动来滑动的视图。它有四个部分 1-2需要更改高度、宽度和底部约束 2-3高度是最大高度,您可以将其滑入或滑出 3-4卡处于最大高度,您可以向下滑动 谢谢 我就是这样做的 var theView = UIView() override func viewDidLoad() { super.viewDidLoad() // First create your
谢谢 我就是这样做的
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
}
}
var theView=UIView()
重写func viewDidLoad(){
super.viewDidLoad()
//首先创建您的视图。
theView=ui视图(帧:CGRect(x:40,y:self.view.bounds.height-120,width:self.view.frame.width-80,height:100))
//设置其背景色
theView.backgroundColor=UIColor.black
//创建圆角
view.layer.cornerRadius=20
//并将其添加到视图中
self.view.addSubview(theView)
//创建UIPangestureRecognitor并将函数分配给选择器
让gS=UIPangestureRecognitor(目标:self,操作:#选择器(growShink(:))
//在视图上启用用户交互
theView.isUserInteractionEnabled=true
//将手势识别器添加到视图中
theView.AddGestureRecognitor(gS)
}
//控制值用于确定用户是向上还是向下刷卡
变量控制值:CGFloat=0
var动画设置=false
//用户刷卡时调用的函数
@objc func growShink(发送方:UIPANGESTURE识别器){
让translation=sender.location(在:视图中)
//检查控制值是否已设置
如果controlValue!=0&&!设置动画{
//如果已设置,请检查控制值是否大于平移手势接收到的新值
if translation.xcontrolValue,则为else{
设置动画=真
//否则,将帧的值更改回。
UIView.animate(持续时间:0.5,动画:{
self.theView.frame=CGRect(x:0,y:self.view.bounds.height-300,width:self.view.frame.width,height:300)
},完成:{在中完成
UIView.animate(持续时间:1.0,动画:{
self.theView.frame=CGRect(x:40,y:self.view.bounds.height-120,width:self.view.frame.width-80,height:100)
},完成:{finished in self.animating=false;self.controlValue=0})
}
}
//将控制值设置为从平移手势接收的值
controlValue=translation.x
}
}
您可以随意调整宽度和高度值。希望这能满足您的要求:@SchaheerSaleem-问题是我需要从第1部分移动到第2部分,这意味着增加宽度和高度,然后像普通的卡片视图一样使用它,你可以上下滑动,这部分有点困难。谢谢你的回复,但我想增加宽度,直到高度达到200,然后上下滑动,检查照片是什么导致初始动画将宽度和高度增加到某一点,在您期待向上和向下滑动之前?同样,向上或向下滑动使其增加到200的高度和宽度。然后第二次滑动将高度增加到全屏?向下滑动时的相反方向?或者直接,在一次滑动中,首先将高度增加到ex 200,宽度增加到100%,然后将高度增加到全屏