我有一些带有Pan
和Tap
手势识别器的图片效果很好。
我也尝试添加捏合手势,但代表甚至不会解雇:
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 :(得分: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
}
试试这个,它应该可行
祝你好运!答案 1 :(得分:1)
问题是你可能从未调用过将手势识别器添加到图像视图的方法。如果您使用Swift 4进行编码,请确保将@objc添加到您的方法中。顺便说一句,Swift惯例是用小写字母命名你的方法:
@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)
}
答案 2 :(得分:0)
代码工作得很好,只是没有在Mac上,即使你的触控板让你无法在Mac上进行操作,只能在iPhone上使用。
答案 3 :(得分:0)
如果您有多个手势识别器,则可以实现UIGestureRecognizerDelegate
并为此方法返回true
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return Bool
}
此方法询问委托人是否应使用两个手势识别器 允许同时识别手势。
例如:
let gifImageView = UIImageView()
gifImageView.contentMode = .scaleToFill
gifImageView.layer.cornerRadius = 5
gifImageView.layer.masksToBounds = true
gifImageView.translatesAutoresizingMaskIntoConstraints = false
gifImageView.isUserInteractionEnabled = true
NSLayoutConstraint.activate([
gifImageView.topAnchor.constraint(equalTo: navigationBarView.bottomAnchor, constant: 16),
gifImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
gifImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
gifImageView.bottomAnchor.constraint(equalTo: collectionView.topAnchor, constant: -16)
])
let rotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(_:)))
rotationGestureRecognizer.delegate = self
gifImageView.addGestureRecognizer(rotationGestureRecognizer)
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
panGestureRecognizer.delegate = self
gifImageView.addGestureRecognizer(panGestureRecognizer)
let pincGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(_:)))
pincGestureRecognizer.delegate = self
gifImageView.addGestureRecognizer(pincGestureRecognizer)
view.addSubview(gifImageView)
还有您的处理功能:
@objc func handleRotate(_ gestureRecognizer : UIRotationGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
gifImageView.transform = gifImageView.transform.rotated(by: gestureRecognizer.rotation)
gestureRecognizer.rotation = 0
}
}
@objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
gifImageView.center = CGPoint(x: gifImageView.center.x + translation.x, y: gifImageView.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
@objc func handlePinch(_ gestureRecognizer: UIPinchGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
gifImageView.transform = gifImageView.transform.scaledBy(x: gestureRecognizer.scale, y: gestureRecognizer.scale)
gestureRecognizer.scale = 1.0
}
}
//Finally implement gesture recognizer delegate method
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}