2017-11-11 81 views
0

我有一些图像PanTap手势识别器,它很好。捏手势不会工作

我尝试也增加缩放手势,但代表甚至不会触发:

func addPinchTo(image:UIImageView) 
    { 
     let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.Pscale(gesture:))) 
     let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(self.ProtateArt(gesture:))) 
     image.addGestureRecognizer(pinchGesture) 
     image.addGestureRecognizer(rotationGesture) 
     image.isUserInteractionEnabled = true 


    } 


func Pscale(gesture: UIPinchGestureRecognizer) { 

     print("D") //not print 
    } 

    func ProtateArt(gesture: UIRotationGestureRecognizer) { 

     print("aD") //not print 
} 
+0

你的代码看起来很好。你是否检查过该方法所在的类是否在调用该方法后未获得deinitalized? – carbonr

回答

0

代码伟大的工作,只是没有在Mac上,甚至可以让你掐你不能做它在Mac触控板,只在iPhone上。

+1

是的。没有必要从该方法返回任何东西。请注意,在您的问题中,您正在使用UIImageView和UIPinchGestureRecognizer,它们仅适用于UIKit(iOS)而非MacOS –

+1

如果您想要监视MacOS触控板中的夹点,则需要覆盖视图控制器magnify方法'override func magnify(with event :NSEvent){print(String(format:“%.2f %%”,event.magnification)) }'。监视旋转,你需要覆盖旋转方法'覆盖func旋转(事件:NSEvent){ print(event.rotation) } –

2
func addPinchTo(image:UIImageView) -> UIImageView 
    { 
     let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.Pscale(gesture:))) 
     let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(self.ProtateArt(gesture:))) 
     image.addGestureRecognizer(pinchGesture) 
     image.addGestureRecognizer(rotationGesture) 
     image.isUserInteractionEnabled = true 

     return image 
    } 


func Pscale(gesture: UIPinchGestureRecognizer) { 

     print("D") //not print 
    } 

func ProtateArt(gesture: UIRotationGestureRecognizer) { 

     print("aD") //not print 
} 

或由于@Leo Dabus,使用@objc方法将正常工作也。

@objc func pinch(gesture: UIPinchGestureRecognizer) { 
    print("hit from pinch function") 
} 

@objc func rotation(gesture: UIRotationGestureRecognizer) { 
    print("hit from rotation function") 
} 

,并使其工作,你的代码应该是这个样子

func addPinchTo(image: UIImageView) { 
    let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinch)) 
    let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(rotation)) 
    image.addGestureRecognizer(pinchGesture) 
    image.addGestureRecognizer(rotationGesture) 
    image.isUserInteractionEnabled = true 
} 

试试这个,它应该工作

祝你好运!

+0

这是错误的。你不需要返回任何东西。顺便说一句,只要添加'inout'关键字就可以了,但在这里绝对不需要。 –

+0

嗨,leo,它已经使用了它几个月,它工作正常,只是分享我的代码,ps:对不起,如果你想我删除它,我会做 –

+0

这取决于你。我不会投票,但其他人可能会这样做。 –

1

问题在于,您可能从未调用将手势识别器添加到图像视图的方法。如果您在Swift 4中编写代码,请确保将@objc添加到您的方法中。顺便说一下它的命名是你的方法开始为小写字母,斯威夫特约定:

@IBOutlet weak var imageView: UIImageView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    addPinchTo(image: imageView) 
} 

func addPinchTo(image: UIImageView) { 
    let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinch)) 
    let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(rotation)) 
    image.addGestureRecognizer(pinchGesture) 
    image.addGestureRecognizer(rotationGesture) 
    image.isUserInteractionEnabled = true 
} 

@objc func pinch(gesture: UIPinchGestureRecognizer) { 
    print(#function) 
} 

@objc func rotation(gesture: UIRotationGestureRecognizer) { 
    print(#function) 
}