仅从 1 个子视图平移 UIView
Panning a UIView only from 1 subview
我有一个用作自定义警报的 UIView (alertView)。在这个 UIView 中,我有几个交互部分。我只希望能够平移此自定义警报 (alertHeaderView) 的顶部以移动 alertView,同时仍保持 alertView 内容的功能。
我目前有这个 half-working。在故事板中,我将平移手势识别器添加到整个 alertView。我的平移功能运行完美,但它阻止了视图中发生的所有交互。就是这个问题。
@IBAction func panAlert(_ sender: UIPanGestureRecognizer) {
// ... All my pan code.
}
我已经为 header 创建了一个出口:
@IBOutlet weak var alertHeaderView: UIView!
有没有一种方法可以平移整个 alertView,仅通过来自 alertHeaderView 的触摸?
这是我试过的代码 - 它限制了从特定位置集移动的视图
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
我有一个用作自定义警报的 UIView (alertView)。在这个 UIView 中,我有几个交互部分。我只希望能够平移此自定义警报 (alertHeaderView) 的顶部以移动 alertView,同时仍保持 alertView 内容的功能。
我目前有这个 half-working。在故事板中,我将平移手势识别器添加到整个 alertView。我的平移功能运行完美,但它阻止了视图中发生的所有交互。就是这个问题。
@IBAction func panAlert(_ sender: UIPanGestureRecognizer) {
// ... All my pan code.
}
我已经为 header 创建了一个出口:
@IBOutlet weak var alertHeaderView: UIView!
有没有一种方法可以平移整个 alertView,仅通过来自 alertHeaderView 的触摸?
这是我试过的代码 - 它限制了从特定位置集移动的视图
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