UIImageView上的UIGestureRecognizer

我有一个UIImageView ,我想要能够调整和旋转等

UIGestureRecognizer可以添加到UIImageView吗?

我想要添加一个旋转和捏认识到UIImageView将在运行时创build。

如何添加这些识别器?

检查UIImageView上的userInteractionEnabledYES 。 然后你可以添加一个手势识别器。

 imageView.userInteractionEnabled = YES; UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)]; pgr.delegate = self; [imageView addGestureRecognizer:pgr]; [pgr release]; : : - (void)handlePinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer { //handle pinch... } 

是的,可以将UIGestureRecognizer添加到UIImageView。 正如在另一个答案中所述,记住通过将其userInteractionEnabled属性设置为YES来在图像视图上启用用户交互是非常重要的。 UIImageViewinheritance自UIView,默认情况下,它的用户交互属性设置为YES ,但UIImageView的用户交互属性默认设置为NO

从UIImageView文档:

新的图像视图对象被configuration为默认忽略用户事件。 如果要处理UIImageView的自定义子类中的事件,则必须在初始化对象后明确将userInteractionEnabled属性的值更改为YES。

无论如何,在答案的大部分。 以下是如何使用UIPinchGestureRecognizerUIRotationGestureRecognizerUIPanGestureRecognizer创buildUIImageViewUIPanGestureRecognizer

首先,在viewDidLoad ,或者您select的其他方法,创build一个图像视图,给它一个图像,一个框架,并启用它的用户交互。 然后创build三个手势如下。 一定要利用他们的委托财产(最有可能设置为自己)。 这将需要同时使用多个手势。

 - (void)viewDidLoad { [super viewDidLoad]; // set up the image view UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage"]]; [imageView setBounds:CGRectMake(0.0, 0.0, 120.0, 120.0)]; [imageView setCenter:self.view.center]; [imageView setUserInteractionEnabled:YES]; // <--- This is very important // create and configure the pinch gesture UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureDetected:)]; [pinchGestureRecognizer setDelegate:self]; [imageView addGestureRecognizer:pinchGestureRecognizer]; // create and configure the rotation gesture UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureDetected:)]; [rotationGestureRecognizer setDelegate:self]; [imageView addGestureRecognizer:rotationGestureRecognizer]; // creat and configure the pan gesture UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)]; [panGestureRecognizer setDelegate:self]; [imageView addGestureRecognizer:panGestureRecognizer]; [self.view addSubview:imageView]; // add the image view as a subview of the view controllers view } 

以下是在检测到视图上的手势时将会调用的三种方法。 在它们内部,我们将检查手势的当前状态,如果它处于开始或更改的UIGestureRecognizerState我们将读取手势的缩放/旋转/平移属性,将该数据应用于仿射变换,将仿射变换应用于图像视图,然后重置手势缩放/旋转/平移。

 - (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer { UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { CGFloat scale = [recognizer scale]; [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)]; [recognizer setScale:1.0]; } } - (void)rotationGestureDetected:(UIRotationGestureRecognizer *)recognizer { UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { CGFloat rotation = [recognizer rotation]; [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, rotation)]; [recognizer setRotation:0]; } } - (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer { UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { CGPoint translation = [recognizer translationInView:recognizer.view]; [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)]; [recognizer setTranslation:CGPointZero inView:recognizer.view]; } } 

最后也是非常重要的一点,您需要使用UIGestureRecognizerDelegate方法gestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer允许手势同时工作。 如果这三个手势是将这个类指定为其委托的唯一三个手势,则可以简单地返回YES ,如下所示。 但是,如果您具有将此类指定为其委托的其他手势,则可能需要向此方法添加逻辑以确定在允许它们一起工作之前哪个手势是哪个手势。

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

不要忘记确保你的类符合UIGestureRecognizerDelegate协议。 为此,请确保您的界面如下所示:

 @interface MyClass : MySuperClass <UIGestureRecognizerDelegate> 

如果您更喜欢自己在工作示例项目中使用代码,那么可以在此处find包含此代码的示例项目。

Swift 2.0解决scheme

您可以在同一个庄园中创build水龙头,捏或滑动手势识别器。 下面我会通过4个步骤让你的识别器启动并运行。

4个步骤

1.)通过将其添加到您的类签名inheritanceUIGestureRecognizerDelegate

 class ViewController: UIViewController, UIGestureRecognizerDelegate {...} 

2.)控制从你的图像拖到你的viewController来创build一个IBOutlet:

 @IBOutlet weak var tapView: UIImageView! 

3.)在你的viewDidLoad中添加下面的代码:

 // create an instance of UITapGestureRecognizer and tell it to run // an action we'll call "handleTap:" let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:")) // we use our delegate tap.delegate = self // allow for user interaction tapView.userInteractionEnabled = true // add tap as a gestureRecognizer to tapView tapView.addGestureRecognizer(tap) 

4.)创build当您的手势识别器被点击时将被调用的function。 (如果您select,您可以排除= nil )。

 func handleTap(sender: UITapGestureRecognizer? = nil) { // just creating an alert to prove our tap worked! let tapAlert = UIAlertController(title: "hmmm...", message: "this actually worked?", preferredStyle: UIAlertControllerStyle.Alert) tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil)) self.presentViewController(tapAlert, animated: true, completion: nil) } 

你的最终代码应该是这样的:

 class ViewController: UIViewController, UIGestureRecognizerDelegate { @IBOutlet weak var tapView: UIImageView! override func viewDidLoad() { super.viewDidLoad() let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:")) tap.delegate = self tapView.userInteractionEnabled = true tapView.addGestureRecognizer(tap) } func handleTap(sender: UITapGestureRecognizer? = nil) { let tapAlert = UIAlertController(title: "hmmm...", message: "this actually worked?", preferredStyle: UIAlertControllerStyle.Alert) tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil)) self.presentViewController(tapAlert, animated: true, completion: nil) } } 

斯威夫特4

 myImageView.isUserInteractionEnabled = true let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("imageTapped:"))) tapGestureRecognizer.numberOfTapsRequired = 1 myImageView.addGestureRecognizer(tapGestureRecognizer) 

当点击时:

 func imageTapped(sender: UITapGestureRecognizer) { // do something when image tapped println("image tapped") } 

对于Swift爱好者:

  myImageView.userInteractionEnabled = true var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageTapped:") tapGestureRecognizer.numberOfTapsRequired = 1 myImageView.addGestureRecognizer(tapGestureRecognizer) 

然后当图像点击:

 func imageTapped(sender: UITapGestureRecognizer) { // do something when image tapped println("image tapped") } 

SWIFT 3示例

 override func viewDidLoad() { self.backgroundImageView.addGestureRecognizer( UITapGestureRecognizer.init(target: self, action: #selector(didTapImageview(_:)) ) ) self.backgroundImageView.isUserInteractionEnabled = true } func didTapImageview(_ sender: Any) { // do something } } 

没有手势recongnizer代表或其他必要的实现。

我只是通过在单视图中添加3个手势来完成swift4

  1. UIPinchGestureRecognizer :放大和缩小视图。
  2. UIRotationGestureRecognizer :旋转视图。
  3. UIPanGestureRecognizer :拖动视图。

这里我的示例代码

 class ViewController: UIViewController: UIGestureRecognizerDelegate{ //your image view that outlet from storyboard or xibs file. @IBOutlet weak var imgView: UIImageView! // declare gesture recognizer var panRecognizer: UIPanGestureRecognizer? var pinchRecognizer: UIPinchGestureRecognizer? var rotateRecognizer: UIRotationGestureRecognizer? override func viewDidLoad() { super.viewDidLoad() // Create gesture with target self(viewcontroller) and handler function. self.panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:))) self.pinchRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch(recognizer:))) self.rotateRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(self.handleRotate(recognizer:))) //delegate gesture with UIGestureRecognizerDelegate pinchRecognizer?.delegate = self rotateRecognizer?.delegate = self panRecognizer?.delegate = self // than add gesture to imgView self.imgView.addGestureRecognizer(panRecognizer!) self.imgView.addGestureRecognizer(pinchRecognizer!) self.imgView.addGestureRecognizer(rotateRecognizer!) } // handle UIPanGestureRecognizer @objc func handlePan(recognizer: UIPanGestureRecognizer) { let gview = recognizer.view if recognizer.state == .began || recognizer.state == .changed { let translation = recognizer.translation(in: gview?.superview) gview?.center = CGPoint(x: (gview?.center.x)! + translation.x, y: (gview?.center.y)! + translation.y) recognizer.setTranslation(CGPoint.zero, in: gview?.superview) } } // handle UIPinchGestureRecognizer @objc func handlePinch(recognizer: UIPinchGestureRecognizer) { if recognizer.state == .began || recognizer.state == .changed { recognizer.view?.transform = (recognizer.view?.transform.scaledBy(x: recognizer.scale, y: recognizer.scale))! recognizer.scale = 1.0 } } // handle UIRotationGestureRecognizer @objc func handleRotate(recognizer: UIRotationGestureRecognizer) { if recognizer.state == .began || recognizer.state == .changed { recognizer.view?.transform = (recognizer.view?.transform.rotated(by: recognizer.rotation))! recognizer.rotation = 0.0 } } // mark sure you override this function to make gestures work together func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } } 

任何问题,只需键入评论。 谢谢

您还可以将轻击手势识别器拖动到情节提要中的图像视图中。 然后通过Ctrl +拖动到代码创build一个动作…

对于爱好者,你可以使用ALActionBlocks来添加手势的动作

 __weak ALViewController *wSelf = self; imageView.userInteractionEnabled = YES; UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) { NSLog(@"pan %@", NSStringFromCGPoint([weakGR locationInView:wSelf.view])); }]; [self.imageView addGestureRecognizer:gr];