我正在尝试使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
}
我正在努力做到这一点:
如果有人可以帮助更改或重写我的代码,那就太好了,在此先感谢您!
最佳答案
默认情况下, View 上的一个手势识别器开始处理该手势后,其他识别器将被忽略。可以通过重写方法(对于Swift)来控制此行为:
gestureRecognizer(_:应该同时识别:)
返回true。默认实现返回false。要覆盖此函数,只需将实现返回true的实现添加到ViewController源代码文件中。
这是一些示例Swift代码(请参阅https://developer.apple.com/documentation/uikit/uigesturerecognizerdelegate/1624208-gesturerecognizer?changes=_7):
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中,重载函数是这个函数(请参见https://developer.apple.com/documentation/uikit/uigesturerecognizerdelegate/1624208-gesturerecognizer?language=objc):
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)aR1 shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)aR2
{
return YES;
}
关于ios - 像Snapchat一样同时捏,平移和旋转文本[SWIFT 3],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45402639/