这是我尝试过的代码-它限制从特定位置集移动视图
import UIKit
class AlertWithPan: UIViewController
{
@IBOutlet weak var alertView: UIView!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
self.alertView.isHidden = true
self.addPanGesture()
}
func addPanGesture()
{
let gesture : UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action:#selector(AlertWithPan.handlePanGesture(panGesture:)))
self.alertView.addGestureRecognizer(gesture)
}
@objc func handlePanGesture(panGesture: UIPanGestureRecognizer)
{
///Get my Location
let touchPosition = panGesture.location(in: self.alertView)
print("currentTouch: \(touchPosition.y), ViewOrigin_Y: \(String(describing: panGesture.view?.frame.origin.y))")
//Let us limit the frame Movement on specific location
//In your case you had initialised a header View supposingly it is having height 32
//So we will set TouchPosition not to move Ahead of that View
//I am using here touch Location in AlertView
//Setting it to limit upto `self.alertView.frame.size.height*0.2`
//self.alertView.frame.size.height*0.2 - This refers to Height of your Header View
//if a view having total height of Hundred then If I write self.alertView.frame.size.height*0.2
//This means just 20 percent of height - 20 out of hundred
//we had added pan in AlertView , I.e So we need to set a proper height in which Pan will be allowed to set translation
//self.alertView.frame.size.height*0.2 - using this I made the view to move only in case if touch location is with in the header size I.e 20
// if you had provided static height of header
//Then Can get this by using like pan gesture.origin.y + header view height
// So check will be touchPosition.y > Total of both
if touchPosition.y > self.alertView.frame.size.height*0.2 {
print("High")
}
else{
print("Low")
///Get the changes
let translation = panGesture.translation(in: self.view)
///Move view to required Position
panGesture.view!.center = CGPoint(x: panGesture.view!.center.x, y: panGesture.view!.center.y + translation.y)
panGesture.setTranslation(CGPoint.zero, in: self.view)
}
}
@IBAction func showMyAlertView(_ sender: Any)
{
self.alertView.isHidden = false
}
}
我的故事板:
工作视频:
https://drive.google.com/open?id=1ZZuqxc34BUEZQ7WGFJiA2lU6ydIwqEjn