UIImageView 上的用户交互

User Interaction on a UIImageView(UIImageView 上的用户交互)

本文介绍了UIImageView 上的用户交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多 UIImageView,上面有按钮.

I have a number of UIImageView which have buttons on top of them.

我想在这些按钮后面的 UIImageView 上启用用户交互.

I would like to enable user interaction on the UIImageView behind these buttons.

我在 IB 中看到了该选项,但想知道在实际触摸 UIImageView 时如何触发一些代码.

I see the option in IB, but would like to know how to trigger some code when the UIImageView is actually touched.

如何做到这一点以及如何在代码而不是 IB 中将其设置为启用和禁用?

How does one do this and how is it set to enabled and disabled in the code rather than IB?

谢谢

推荐答案

当 UIImageView 实际被触摸时如何触发一些代码.

how to trigger some code when the UIImageView is actually touched.

你有两个选择:

  1. 创建 UITapGestureRecognizer(或其他手势识别器)的实例,指定目标和操作方法.然后使用 -[UIView addGestureRecognizer:] 将手势识别器添加到图像视图中.适用于 OS 3.2+.

  1. Create an instance of UITapGestureRecognizer (or another gesture recognizer), specifying a target and an action method. Then add the gesture recognizer to the image view with -[UIView addGestureRecognizer:]. Works in OS 3.2+.

子类 UIImageView 并覆盖 -touches... 方法.确保您创建的图像视图是您的自定义子类的实例.

Subclass UIImageView and override the -touches... methods. Make sure the image views you create are instances of your custom subclass.

有关详细信息,请参阅文档.

See the documentation for details.

如何在代码而不是IB中设置为启用和禁用

how is it set to enabled and disabled in the code rather than IB

简单:imageView.userInteractionEnabled = YES;

这篇关于UIImageView 上的用户交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIImageView 上的用户交互

基础教程推荐