代码之家  ›  专栏  ›  技术社区  ›  Joe

仅从1个子视图平移UIView

  •  0
  • Joe  · 技术社区  · 6 年前

    我有一个用作自定义警报的UIView(alertView)。在这个UIView中,我有几个交互片段。我希望能够平移此自定义警报(alertHeaderView)的顶部以移动alertView,同时仍保持alertView内容的功能。

    我现在有一半在工作。在情节提要中,我将平移手势识别器添加到整个alertView中。我的平移功能工作得很好,但它阻止了视图中发生的所有交互。这就是问题所在。

    @IBAction func panAlert(_ sender: UIPanGestureRecognizer) {
        // ... All my pan code.
    }
    

    我为收割台创建了一个出口:

    @IBOutlet weak var alertHeaderView: UIView!
    

    有没有一种方法可以仅通过alertHeaderView的触摸来平移整个alertView?

    1 回复  |  直到 6 年前
        1
  •  1
  •   iOS Geek    6 年前

    这是我尝试过的代码-它限制从特定位置集移动视图

    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
        }
    }
    

    我的故事板:

    enter image description here

    工作视频:

    https://drive.google.com/open?id=1ZZuqxc34BUEZQ7WGFJiA2lU6ydIwqEjn