UIImageView 上的 UIGestureRecognizer
我有一个 UIImageView
,我希望能够调整大小和旋转等。
可以将 UIGestureRecognizer
添加到 UIImageView
中吗?
我想将旋转和捏合识别器添加到将在运行时创建的 UIImageView 中。
如何添加这些识别器?
I have a UIImageView
, which I want to be able to resize and rotate etc.
Can a UIGestureRecognizer
be added to the UIImageView
?
I would want to add a rotate and pinch recognizer to a UIImageView
which would be created at runtime.
How does one add these recognizers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
SWIFT 3 示例
无需手势识别器委托或其他必要的实现。
SWIFT 3 Example
No gesture recongnizer delegates or other implementations where necessary.
您还可以将点击手势识别器拖动到
Storyboard
中的图像视图。然后通过Ctrl + 拖动
到代码中创建一个操作。You can also drag a tap gesture recogniser to the image view in
Storyboard
. Then create an action byctrl + drag
to the code.对于块爱好者,您可以使用 ALActionBlocks 在块中添加手势操作
For Blocks lover you can use ALActionBlocks to add action of gestures in block
检查
UIImageView
上的userInteractionEnabled
是否为YES
。然后您可以添加手势识别器。Check that
userInteractionEnabled
isYES
on theUIImageView
. Then you can add a gesture recognizer.是的,可以将 UIGestureRecognizer 添加到 UIImageView 中。正如另一个答案中所述,记住通过将其
userInteractionEnabled
属性设置为YES
来启用图像视图上的用户交互非常重要。 UIImageView继承自UIView,UIView的用户交互属性默认设置为YES
,但UIImageView的用户交互属性默认设置为NO
。来自 UIImageView 文档:
无论如何,关于大部分答案。以下示例展示了如何使用
UIPinchGestureRecognizer
、UIRotationGestureRecognizer
和UIPanGestureRecognizer
创建UIImageView
。首先,在 viewDidLoad 或您选择的其他方法中,创建一个图像视图,为其提供图像、框架,并启用其用户交互。然后创建三个手势,如下所示。请务必使用他们的委托属性(最有可能设置为 self)。这需要同时使用多个手势。
以下是检测到视图上的手势时将调用的三个方法。在它们内部,我们将检查手势的当前状态,如果它处于开始或更改的
UIGestureRecognizerState
状态,我们将读取手势的缩放/旋转/平移属性,将该数据应用于仿射变换,将仿射变换应用于图像视图,然后重置手势缩放/旋转/平移。最后也是非常重要的是,您需要利用 UIGestureRecognizerDelegate 方法
gestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer
允许手势同时工作。如果这三个手势是仅有的三个将此类指定为其委托的手势,那么您只需返回YES
即可,如下所示。但是,如果您有其他手势将此类指定为其委托,则您可能需要向此方法添加逻辑,以确定哪个手势是哪个手势,然后才能允许它们一起工作。不要忘记确保您的类符合 UIGestureRecognizerDelegate 协议。为此,请确保您的界面如下所示:
如果您更喜欢自己使用工作示例项目中的代码,我创建的示例项目包含此代码com/NSPostWhenIdle/GestureImageView">可以在这里找到。
Yes, a UIGestureRecognizer can be added to a UIImageView. As stated in the other answer, it is very important to remember to enable user interaction on the image view by setting its
userInteractionEnabled
property toYES
. UIImageView inherits from UIView, whose user interaction property is set toYES
by default, however, UIImageView's user interaction property is set toNO
by default.From the UIImageView docs:
Anyway, on the the bulk of the answer. Here's an example of how to create a
UIImageView
with aUIPinchGestureRecognizer
, aUIRotationGestureRecognizer
, and aUIPanGestureRecognizer
.First, in
viewDidLoad
, or another method of your choice, create an image view, give it an image, a frame, and enable its user interaction. Then create the three gestures as follows. Be sure to utilize their delegate property (most likely set to self). This will be required to use multiple gestures at the same time.Here are the three methods that will be called when the gestures on your view are detected. Inside them, we will check the current state of the gesture, and if it is in either the began or changed
UIGestureRecognizerState
we will read the gesture's scale/rotation/translation property, apply that data to an affine transform, apply the affine transform to the image view, and then reset the gestures scale/rotation/translation.Finally and very importantly, you'll need to utilize the UIGestureRecognizerDelegate method
gestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer
to allow the gestures to work at the same time. If these three gestures are the only three gestures that have this class assigned as their delegate, then you can simply returnYES
as shown below. However, if you have additional gestures that have this class assigned as their delegate, you may need to add logic to this method to determine which gesture is which before allowing them to all work together.Don't forget to make sure that your class conforms to the UIGestureRecognizerDelegate protocol. To do so, make sure that your interface looks something like this:
If you prefer to play with the code in a working sample project yourself, the sample project I've created containing this code can be found here.
Swift 4.2
以及点击时:
Swift 4.2
and when tapped:
Swift 2.0 解决方案
您可以在同一庄园中创建点击、捏合或滑动手势识别器。下面我将引导您完成 4 个步骤来启动并运行识别器。
4 个步骤
1.) 通过将
UIGestureRecognizerDelegate
添加到您的类签名来继承它。2.) 控制从图像拖动到 viewController 以创建 IBOutlet:
3.) 在 viewDidLoad 中添加以下代码:
4.) 创建点击手势识别器时将调用的函数。 (如果您选择,可以排除
= nil
)。您的最终代码应该如下所示:
Swift 2.0 Solution
You create a tap, pinch or swipe gesture recognizer in the same manor. Below I'll walk you through 4 steps to getting your recognizer up and running.
4 Steps
1.) Inherit from
UIGestureRecognizerDelegate
by adding it to your class signature.2.) Control drag from your image to your viewController to create an IBOutlet:
3.) In your viewDidLoad add the following code:
4.) Create the function that will be called when your gesture recognizer is tapped. (You can exclude the
= nil
if you choose).Your final code should look something like this:
我刚刚使用 swift4 通过在单个视图 UIPinchGestureRecognizer 中添加 3 个手势来完成此操作
这是我的示例代码
如有任何问题,只需输入评论即可。谢谢
I just done this with swift4 by adding 3 gestures together in single view
Here my sample code
Any question, just type to comment. thank you