同时捏,拖动和平移 [英] Pinch, drag and pan at the same time
问题描述
我在UIImageView
上有一个标签,如下所示.
I have a label on the UIImageView
as below.
标签是可拖动的,可平移的和可捏的.但是我一次只能做一个手势.例如,我想像在Snapchat和Whatsapp中图像上的文本一样捏住标签时拖动标签.我的功能如下.当我搜索时,我认为我应该创建一个自定义手势识别器,但我不知道该怎么做.没有创建自定义识别器,有什么方法可以做到吗?
The label is draggable, pan-able and pinch-able. However I can only do one gesture at a time. For example, I want to drag the label while I'm pinching it like in texts on images in Snapchat and Whatsapp. My functions are as below. As I searched, I think I should create a custom gesture recognizer but I don't know how. Is there any way I can do it without creating a custom recognizer?
在执行此操作时,我从这篇文章中获得了帮助: 图像上类似Snapchat的文本
I got help from this post while doing this: Snapchat-like text on image
func handlePan(recognizer: UIPanGestureRecognizer) {
var translation = recognizer.translation(in: allview)
translation.x = max(translation.x, imageview.frame.minX - mylabel.frame.minX)
translation.x = min(translation.x, imageview.frame.maxX - mylabel.frame.maxX)
translation.y = max(translation.y, imageview.frame.minY - mylabel.frame.minY)
translation.y = min(translation.y, imageview.frame.maxY - mylabel.frame.maxY)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero , in: view)
}
func handlePinch(recognizer: UIPinchGestureRecognizer) {
if let view = recognizer.view as? UILabel {
let pinchScale: CGFloat = recognizer.scale
view.transform = view.transform.scaledBy(x: pinchScale, y: pinchScale)
recognizer.scale = 1.0
}
}
func handleRotate(recognizer: UIRotationGestureRecognizer) {
if let view = recognizer.view as? UILabel {
let rotation: CGFloat = recognizer.rotation
view.transform = view.transform.rotated(by: rotation)
recognizer.rotation = 0.0
}
}
推荐答案
我通过在ViewController中添加"UIGestureRecognizerDelegate"来解决.这允许同时使用手势.我敢肯定,创建自定义手势会更好,但是这样做也可以.在viewDidLoad函数上添加这三行代码
I solved by adding "UIGestureRecognizerDelegate" to my ViewController. This allows using gestures at the same time. I'm sure creating custom gesture will work better but this also do the work. Add this three line of codes on viewDidLoad function
pinchRecognizer.delegate = self
panRecognizer.delegate = self
rotateRecognizer.delegate = self
也不要忘记为代表添加functin;
Also do not forget to add functin for delegate which is;
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
这篇关于同时捏,拖动和平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!