仅从1个子视图平移UIView

时间:2018-02-05 04:04:59

标签: ios swift uitapgesturerecognizer uipangesturerecognizer

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

我目前正在进行这项半工作。在故事板中,我将pan手势识别器添加到整个alertView。我的平移功能非常完美,但它阻止了视图中发生的所有交互。这就是问题所在。

<p>Type math here: <span id="math-field"></span></p>
<p>LaTeX of what you typed: <span id="latex"></span></p>

<script>
var mathFieldSpan = document.getElementById('math-field');
var latexSpan = document.getElementById('latex');

var MQ = MathQuill.getInterface(2); // for backcompat
var mathField = MQ.MathField(mathFieldSpan, {
  spaceBehavesLikeTab: true, // configurable
  handlers: {
    edit: function() { // useful event handlers
      latexSpan.textContent = mathField.latex(); // simple API
    }
  }
});
</script>

我为标题创建了一个插座:

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

有没有办法可以平移整个alertView,只能触及来自alertHeaderView的触摸?

1 个答案:

答案 0 :(得分:1)

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

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