快速手势识别器longpress与捏和拖动

时间:2018-05-19 11:36:44

标签: ios swift uigesturerecognizer

嘿伙计我试图在父视图中显示长视图并使其可拖动和可捏合但是现在我只能在可拖动但是捏不起作用。这是我的代码

class MainVC: UIViewController, UIGestureRecognizerDelegate {

    // MARK: Intializer
    public init() {
        super.init(nibName: nil, bundle: nil)
    }

    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    // MARK: View controller lifecycle methods    
    public override func loadView() {
        self.view = MainView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let longpressed = UILongPressGestureRecognizer(target: self, action: #selector(longpressedAction(_:)))
        longpressed.minimumPressDuration = 0
        longpressed.numberOfTouchesRequired = 2
        self.rootView.addGestureRecognizer(longpressed)

//        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapAction(_:)))
//        tap.numberOfTouchesRequired = 2
//        self.rootView.addGestureRecognizer(tap)

    }

    // MARK: Stored Properties
    private var circleView: ShapeView!

}

// MARK: - Views
extension MainVC {
    public unowned var rootView: MainView { return self.view as! MainView } // swiftlint:disable:this force_cast
}

// MARK: - Action Functions
extension MainVC {

    @objc func longpressedAction(_ sender: UILongPressGestureRecognizer) {
        if sender.state == .began {
            self.addCircle(gesture: sender)

        } else if sender.state == .changed {

            let location = sender.location(in: self.view)
            self.circleView.center = CGPoint(
                x: view.center.x + (location.x - view.center.x),
                y: view.center.y + (location.y - view.center.y)
            )

        } else if sender.state == .ended{
            self.circleView.removeFromSuperview()
        }
    }


    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {

        if gestureRecognizer == self.circleView.pinchRecognizer {
            return true
        } else {
            return false
        }
    }

    @objc func pinchAction(_ sender: UIPinchGestureRecognizer){
        print("PINCHING")
        let scale = sender.scale
        self.circleView.transform = self.circleView.transform.scaledBy(x: scale, y: scale)
        sender.scale = 1.0
    }
}

// MARK: - Helper Function
extension MainVC {

    func addCircle(gesture: UIGestureRecognizer) {
        let tapPoint = gesture.location(in: self.rootView)

        let pinchRecog: UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(_:)))

        self.circleView = ShapeView(origin: tapPoint, pinchRecognizer: pinchRecog)
        self.circleView.pinchRecognizer.delegate = self
        self.rootView.addGestureRecognizer(pinchRecog)

        self.rootView.addSubview(self.circleView)
    }
} 

这里是两个手指长按时出现的形状的视图

import Foundation
import UIKit
import CoreGraphics

class ShapeView: UIView {
    let size: CGFloat = 150.0

    var circleLayer: CAShapeLayer!
    var circlePath: UIBezierPath!
    var roundRect: CGRect!
    var pinchRecognizer: UIPinchGestureRecognizer

    init(origin: CGPoint, pinchRecognizer: UIPinchGestureRecognizer) {

        self.roundRect = CGRect(x: 0.0, y: 0.0, width: self.size, height: self.size)        
        self.pinchRecognizer = pinchRecognizer
        super.init(frame: self.roundRect)
        self.center = origin

        self.circlePath = UIBezierPath(roundedRect: self.roundRect, cornerRadius: 10)

        self.circleLayer = CAShapeLayer()
        self.circleLayer.path = circlePath.cgPath
        self.circleLayer.fillColor = UIColor.clear.cgColor
        self.circleLayer.strokeColor = UIColor.red.cgColor
        self.circleLayer.lineWidth = 10.0
        self.circleLayer.strokeEnd = 1.0        

        // Add the circleLayer to the view's layer's sublayers
        layer.addSublayer(self.circleLayer)

//        self.initGestureRecognizers()
    }

    // We need to implement init(coder) to avoid compilation errors
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func initGestureRecognizers() {
        let pinchGR = UIPinchGestureRecognizer(target: self, action: #selector(didPinch(pinchGR:)))
        addGestureRecognizer(pinchGR)

        let panGR = UIPanGestureRecognizer(target: self, action: #selector(didPan(panGR:)))
        addGestureRecognizer(panGR)
    }

    @objc func didPinch(pinchGR: UIPinchGestureRecognizer) {
        print("hahahah")
        self.superview!.bringSubview(toFront: self)

        let scale = pinchGR.scale

        self.transform = self.transform.scaledBy(x: scale, y: scale)

        pinchGR.scale = 1.0
    }

    @objc func didPan(panGR: UIPanGestureRecognizer) {

        self.superview!.bringSubview(toFront: self)

        var translation = panGR.translation(in: self)

        self.center.x += translation.x
        self.center.y += translation.y

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

} 

我目前正在使用协调器模式,这里是代码https://github.com/aoiserizawa/kurayami/tree/master/kurayami

的存储库

1 个答案:

答案 0 :(得分:0)

我添加后现在正在工作:

public function categories() {
    return $this->belongsToMany(Category::class, 'questions_categories')
}

可以长按多重手势。请在我的github帐户上查看更新的代码