UIPanGestureRecognizer不会触发动作

时间:2018-09-05 21:09:50

标签: ios swift uiview uipangesturerecognizer

我正在尝试创建UIView的子类,以使视图在其上平移。它应该以这种方式工作:如果用户将锅平移到顶部,则视图的高度会减小,而如果锅平移到底部,则应该增加。为了实现该功能,我试图将UIPanGestureRecognizer添加到视图中,但是它似乎不起作用。我这样做是这样的:

第一个代码段是uiView子类声明

class ExpandibleView: UIView {
    //Here I create the reference to the recognizer
    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))

    //And here I set its minimumNumberOfTouches and maximumNumberOfTouches properties and add it to the view
    func initialize() {
        panGestureRecognizer.minimumNumberOfTouches = 1
        panGestureRecognizer.maximumNumberOfTouches = 1
        self.addGestureRecognizer(panGestureRecognizer)
    }

    //here's the function that should handle the pan but who instead doesn't seem to been called at all
    @objc func handlePan(_ sender:UIPanGestureRecognizer) {
        //Here's I handle the Pan
    }
}

第二个是视图控制器内部的实现。

class MapViewController: UIViewController {
    @IBOutlet weak var profileView: ExpandibleView!

    //MARK: - ViewController Delegate Methods

    override func viewDidLoad() {
        super.viewDidLoad()

        //Here I set the View
        profileView.isUserInteractionEnabled = true
        profileView.initialize()
        profileView.minHeight = 100
        profileView.maxHeight = 190
    }

}

我在情节提要板中将视图的类设置为我创建的子类,但识别器并未在所有处理程序上触发。

2 个答案:

答案 0 :(得分:1)

您遇到的问题是由于您在此处的类定义中定义了panGestureRecognizer变量:

//Here I create the reference to the recognizer
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))

您可以通过这种方式对其进行初始化,但是创建此变量时似乎未设置self。因此,您的操作永远不会被注册。

有几种方法可以使用代码解决此问题,可以继续将其初始化为实例变量,但是您需要在initialize()函数中设置目标/操作

let panGestureRecognizer = UIPanGestureRecognizer()

func initialize() {
    // add your target/action here
    panGestureRecognizer.addTarget(self, action: #selector(handlePan(_:)))
    panGestureRecognizer.minimumNumberOfTouches = 1
    panGestureRecognizer.maximumNumberOfTouches = 1
    addGestureRecognizer(panGestureRecognizer)
}

或者您可以只在初始化函数中初始化手势识别器,而不使用实例变量

func initialize() {
    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
    panGestureRecognizer.minimumNumberOfTouches = 1
    panGestureRecognizer.maximumNumberOfTouches = 1
    addGestureRecognizer(panGestureRecognizer)
}

我的原始答案

这是一个解决方案,可以使用情节提要中定义的视图或直接操纵框架来使用约束。

带有约束的示例

import UIKit

// Delegate protocol for managing constraint updates if needed
protocol MorphDelegate: class {

    // tells the delegate to change its size constraints
    func morph(x: CGFloat, y: CGFloat)
}

class ExpandableView: UIView {

    var delegate: MorphDelegate?

    init() {
        // frame is set later if needed by creator when using this init method
        super.init(frame: CGRect.zero)
        configureGestureRecognizers()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configureGestureRecognizers()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        configureGestureRecognizers()
    }

    // setup UIPanGestureRecognizer
    internal func configureGestureRecognizers() {
        let panGR = UIPanGestureRecognizer.init(target: self, action: #selector(didPan(_:)))
        addGestureRecognizer(panGR)
    }

    @objc func didPan(_ panGR: UIPanGestureRecognizer) {

        // get the translation
        let translation = panGR.translation(in: self).applying(transform)

        if let delegate = delegate {

            // tell delegate to change the constraints using this translation
            delegate.morph(x: translation.x, y: translation.y)

        } else {

            // If you want the view to expand/contract in opposite direction
            // of drag then swap the + and - in the 2 lines below
            let newOriginY = frame.origin.y + translation.y
            let newHeight = frame.size.height - translation.y

            // expand self via frame manipulation
            let newFrame = CGRect(x: frame.origin.x, y: newOriginY, width: frame.size.width, height: newHeight)
            frame = newFrame
        }

        // reset translation
        panGR.setTranslation(CGPoint.zero, in: self)
    }

}

如果您想通过在情节提要中定义该类并操纵其约束来使用此类,则可以像这样处理它。

首先在情节提要中定义您的视图,并限制其宽度和高度。对于演示,我将其限制在视图中心的x位置,将其限制在SafeArea.bottom的底部。

为视图创建一个IBOutlet,并将其设置为ViewController文件的高度限制。

在此示例中,我将背景色设置为蓝色。

storyboard setup

在视图控制器中,我定义了一个用于设置视图的函数(当前仅设置用于约束操作回调的委托),然后定义了扩展以处理用于更新约束的委托调用。

class ViewController: UIViewController {

    @IBOutlet weak var expandingView: ExpandableView!
    @IBOutlet weak var constraint_expViewHeight: NSLayoutConstraint!


    override func viewDidLoad() {
        super.viewDidLoad()

        // call to configure our expanding view
        configureExpandingView()
    }

    // this sets the delegate for an expanding view defined in the storyboard
    func configureExpandingView() {
        expandingView.delegate = self
    }
}

// setup the delegate callback to handle constraint manipulation
extension ViewController: MorphDelegate {

    func morph(x: CGFloat, y: CGFloat) {

        // this will update the view's height based on the amount
        // you drag your finger in the view. You can '+=' below if
        // you want to reverse the expanding behavior based on pan
        // movements.
        constraint_expViewHeight.constant -= y
    }
}

通过约束来执行此操作使我在运行项目时能够做到这一点:

animation with constraints

具有帧操作的示例

要使用此属性并仅使用frame属性来控制高度,则视图控制器可以实现类似以下的视图创建:

override func viewDidLoad() {
    super.viewDidLoad()

    setupExpandingView()
}

func setupExpandingView() {

    let newView = ExpandableView()
    newView.backgroundColor = .red
    newView.frame = CGRect(x: 20, y: 200, width: 100, height: 100)
    view.addSubview(newView)
}

仅使用帧操作,我得到以下信息:

Demo Gif

答案 1 :(得分:0)

尝试更改此内容

class ExpandibleView: UIView {

    func initialize() {


    }

    //here's the function that should handle the pan but who instead doesn't seem to been called at all
     func handlePan() {
        //your function
    }
}

class MapViewController: UIViewController {
    @IBOutlet weak var profileView: ExpandibleView!

    //MARK: - ViewController Delegate Methods

    override func viewDidLoad() {
        super.viewDidLoad()
     let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
     panGestureRecognizer.minimumNumberOfTouches = 1
        panGestureRecognizer.maximumNumberOfTouches = 1
     profileView.addGestureRecognizer(panGestureRecognizer)
        //Here I set the View
        profileView.isUserInteractionEnabled = true
        profileView.initialize()
        profileView.minHeight = 100
        profileView.maxHeight = 190
    }

   @objc func handlePan(_ sender:UIPanGestureRecognizer) {
       profileView.handlePan()
    }
}

或者您可以在其他视图中创建呼叫委托。

祝你好运!