gpt4 book ai didi

ios - 快速手势识别器 longpress with pinch and drag

转载 作者:行者123 更新时间:2023-11-28 07:48:05 26 4
gpt4 key购买 nike

嘿伙计们,我试图让一个 View 出现在父 View 中的长按上,并使其可拖动和可收缩,但就像现在一样,我只能使它成为可拖动的,但收缩不起作用。这是我的代码

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

这里是长按两根手指会出现的形状的 View

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

最佳答案

在我添加之后它现在可以工作了:

extension MainVC: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}

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

关于ios - 快速手势识别器 longpress with pinch and drag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50424857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com