gpt4 book ai didi

ios - 如何处理 UIPanGestureRecognizer 和 UISwipeGestureRecognizer(顶部和向下)

转载 作者:行者123 更新时间:2023-11-30 12:40:25 24 4
gpt4 key购买 nike

我需要制作一个类似于 iPhone 屏幕的 View (不完全是这样,而是向上/向下滑动的手势和 UIPanesture )下面的屏幕截图。

see screenshot

查看此处的代码。对我来说只工作Pangesture,我需要两者都需要工作吗?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let pan = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan(sender:)))
pan.maximumNumberOfTouches = 1
pan.minimumNumberOfTouches = 1
slideView.addGestureRecognizer(pan)

let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(gesture:)))
swipeUp.direction = UISwipeGestureRecognizerDirection.up
slideView.addGestureRecognizer(swipeUp)

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(gesture:)))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
slideView.addGestureRecognizer(swipeDown)
print(self.slideView.frame.origin.y)

}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
@IBAction func respondToSwipeGesture(gesture: UIGestureRecognizer) {

if let swipeGesture = gesture as? UISwipeGestureRecognizer {


switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.down:
print("Swiped down")
animateSlideView()
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
animateSlideView()
default:
break
}
}
}



@IBAction func slideView(_ sender: Any) {

animateSlideView()

}

func animateSlideView(){
UIView.animate(withDuration: 0.7, animations: {
if self.toggleTop == true{
self.toggleTop = false
self.slideView.frame = CGRect(x: 0, y: 670, width: self.slideView.frame.size.width, height: self.slideView.frame.size.height)
}else {
self.toggleTop = true
self.slideView.frame = CGRect(x: 0, y: 150, width: self.slideView.frame.size.width, height: self.slideView.frame.size.height) // CGRectMake(0, 670, self.slideView.frame.size.width, self.slideView.frame.size.height)
}
self.slideView.layoutIfNeeded()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func handlePan(sender: UIPanGestureRecognizer) {
var touchPointOffset:CGPoint = CGPoint(x: 0, y: 0)
let vel = sender.velocity(in: self.view)
//print(vel.y)
print(sender.location(in: slideView).y)
let actualPosition = sender.view!.center.y - 319.0
let translation = sender.translation(in: self.view)
// [gestureRecognizer locationInView:self.panelViewController.view].y <
if (sender.state == .began || sender.state == .changed) {
if actualPosition > 670 {

} else if actualPosition < 150 {

} else{
sender.view!.center = CGPoint(x: sender.view!.center.x , y: sender.view!.center.y + translation.y)
}
touchPointOffset = sender.location(in: slideView)
//self.touchPointOffset = sender.location(in: slideView) //[gestureRecognizer locationInView:self.panelViewController.view];
}else if sender.state == .cancelled{
print("canceled")
}else if sender.state == .ended{
print(sender.location(in: slideView).y)
//print(vel.y)
touchPointOffset = CGPoint(x: 0, y: 0)
if (vel.y > 0.0) {
if actualPosition >= 670 || actualPosition >= UIScreen.main.bounds.height / 2.0 {
UIView.animate(withDuration: 0.7, animations: {
self.toggleTop = false
sender.view!.center = CGPoint(x: sender.view!.center.x , y: 989.0)
self.slideView.layoutIfNeeded()
})

}else if actualPosition <= 150 || actualPosition <= UIScreen.main.bounds.height / 2.0{
UIView.animate(withDuration: 0.7, animations: {
self.toggleTop = true
sender.view!.center = CGPoint(x: sender.view!.center.x , y: 469.0)
self.slideView.layoutIfNeeded()
})

}
}

//print(vel.y)

}

sender.setTranslation( CGPoint(x: 0,y :0), in: self.view)
}

最佳答案

您正在使用 UIGestureRecognizerDelegate 中的 shouldRecognizeSimultaneouslyWithGestureRecognizer 委托(delegate)方法,但您似乎并未将 ViewController 设置为符合该委托(delegate)。第一:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

}

然后确保您需要同时识别的 UIGestureRecogniersdelegate 属性设置为 self (对于您的 ViewController):

override func viewDidLoad() {
super.viewDidLoad()

let pan = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan(sender:)))
pan.delegate = self // HERE - for the delegate
pan.maximumNumberOfTouches = 1
pan.minimumNumberOfTouches = 1
slideView.addGestureRecognizer(pan)

let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(gesture:)))
swipeUp.delegate = self // HERE - for the delegate
swipeUp.direction = UISwipeGestureRecognizerDirection.up
slideView.addGestureRecognizer(swipeUp)

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(gesture:)))
swipeDown.delegate = self // HERE - for the delegate
swipeDown.direction = UISwipeGestureRecognizerDirection.down
slideView.addGestureRecognizer(swipeDown)
print(self.slideView.frame.origin.y)

}

关于ios - 如何处理 UIPanGestureRecognizer 和 UISwipeGestureRecognizer(顶部和向下),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42304215/

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