gpt4 book ai didi

ios - 像 Snapchat 一样同时捏合、平移和旋转文本 [SWIFT 3]

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:27 27 4
gpt4 key购买 nike

我正在尝试制作一个可以像 snapchat 一样四处移动的 TextView。我做了一些类似的东西,虽然当你尝试在旋转的同时缩放时,它往往会无限地水平拉伸(stretch)并且拖动有时会有点问题。

我有这个:

func panGesture(pan: UIPanGestureRecognizer) {
print("Being Dragged")
if pan.state == .began {
textViewOrigin = pan.location(in: textView)
}else {
let location = pan.location(in: view) // get pan location
textView.frame.origin = CGPoint(x: location.x - textViewOrigin.x, y: location.y - textViewOrigin.y)
}
}
func scaleGesture(_ gesture: UIPinchGestureRecognizer){
print("Being Scaled")
switch gesture.state{
case.began:
identity = textView.transform
case.changed,.ended:
textView.transform = identity.scaledBy(x: gesture.scale, y: gesture.scale)
default:
break
}
}
func rotationGesture(sender: UIRotationGestureRecognizer){
print("Being Rotated")
textView.transform = textView.transform.rotated(by: sender.rotation)
sender.rotation = 0
}

我正努力做到这一点: enter image description here

如果有人可以帮助更改或重写我的代码,那就太好了,在此先感谢!

最佳答案

默认情况下,在 View 上的一个手势识别器开始处理手势后,其他识别器将被忽略。在 Swift 中,可以通过覆盖方法来控制此行为,

gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) ^1 ,

返回true。默认实现返回 false。

要覆盖该函数,只需将您的实现添加到您的 ViewController 源代码文件中,返回 true。下面是一些示例 Swift 代码:

class ViewController: UIViewController,UIGestureRecognizerDelegate {

@IBOutlet var textField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

//add pan gesture
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
gestureRecognizer.delegate = self
textField.addGestureRecognizer(gestureRecognizer)

//Enable multiple touch and user interaction for textfield
textField.isUserInteractionEnabled = true
textField.isMultipleTouchEnabled = true

//add pinch gesture
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchRecognized(pinch:)))
pinchGesture.delegate = self
textField.addGestureRecognizer(pinchGesture)

//add rotate gesture.
let rotate = UIRotationGestureRecognizer.init(target: self, action: #selector(handleRotate(recognizer:)))
rotate.delegate = self
textField.addGestureRecognizer(rotate)


}

func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {

let translation = gestureRecognizer.translation(in: self.view)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}

}

func pinchRecognized(pinch: UIPinchGestureRecognizer) {

if let view = pinch.view {
view.transform = view.transform.scaledBy(x: pinch.scale, y: pinch.scale)
pinch.scale = 1
}
}

func handleRotate(recognizer : UIRotationGestureRecognizer) {
if let view = recognizer.view {
view.transform = view.transform.rotated(by: recognizer.rotation)
recognizer.rotation = 0
}
}

//MARK:- UIGestureRecognizerDelegate Methods
func gestureRecognizer(_: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool {
return true
}
}

这是 Objective-C 中的关键重写 ^2 :

-(BOOL)gestureRecognizer:(UIGestureRecognizer*)aR1 shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)aR2
{
return YES;
}

关于ios - 像 Snapchat 一样同时捏合、平移和旋转文本 [SWIFT 3],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45402639/

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