UIImageView 上的 UIGesture识别器

我有一个 UIImageView,我想能够调整大小和旋转等。

UIGestureRecognizer可以添加到 UIImageView吗?

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

如何添加这些识别器?

108699 次浏览

检查 userInteractionEnabled是否是 UIImageView上的 YES,然后可以添加一个手势识别器。

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...
}

是的,可以向 UIImageView 添加 UIGesture识别器。正如在另一个答案中所述,记住通过将其 userInteractionEnabled属性设置为 YES来启用图像视图上的用户交互是非常重要的。UIImageView 继承自 UIView,其用户交互属性默认设置为 YES,但 UIImageView 的用户交互属性默认设置为 NO

来自 UIImageView 文档:

将新的图像视图对象配置为忽略用户事件 类的自定义子类中处理事件 属性的值,则必须显式更改 初始化对象后,将 userInteractionEnable 属性设置为 YES。

不管怎样,大部分的答案。下面是一个如何创建具有 UIPinchGestureRecognizerUIRotationGestureRecognizerUIPanGestureRecognizerUIImageView的示例。

首先,在 viewDidLoad或您选择的其他方法中,创建一个图像视图,给它一个图像、一个框架,并启用其用户交互。然后创建如下三个手势。一定要利用他们的委托属性(很可能设置为 self)。这将需要同时使用多个手势。

- (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];
}
}

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

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

不要忘记确保您的类符合 委托协议。要做到这一点,请确保您的界面看起来像下面这样:

@interface MyClass : MySuperClass <UIGestureRecognizerDelegate>

如果您喜欢自己使用工作示例项目中的代码,那么可以使用我创建的包含此代码 可以在这里找到。的示例项目

Swift 4.2

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

当被点击的时候:

@objc func imageTapped(_ sender: UITapGestureRecognizer) {
// do something when image tapped
print("image tapped")
}

Swift 2.0解决方案

您创建一个轻击,捏或滑动手势识别在同一庄园。下面我将带你通过4个步骤,让你的识别器启动和运行。

4个步骤

通过将其添加到类签名中,从 UIGestureRecognizerDelegate继承。

class ViewController: UIViewController, UIGestureRecognizerDelegate {...}

控制从图像拖动到 viewController 来创建 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)创建手势识别器被点击时将被调用的函数。(如果您选择,可以排除 = 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)
}
}

您还可以将一个轻击手势识别器拖动到 Storyboard中的图像视图。然后通过 ctrl + drag对代码创建一个操作。

SWIFT3示例

override func viewDidLoad() {


self.backgroundImageView.addGestureRecognizer(
UITapGestureRecognizer.init(target: self, action:#selector(didTapImageview(_:)))
)


self.backgroundImageView.isUserInteractionEnabled = true
}


func didTapImageview(_ sender: Any) {
// do something
}

必要时没有手势识别器委托或其他实现。

对于块爱好者,你可以使用 ALActionBlock添加动作的手势在块

__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];

我只是通过在单一视图中添加3个手势来做到这一点

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

这是我的示例代码

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
}


}

有任何问题,请打评论,谢谢