iOS拖动UIImageView
好吧,所以我一直想知道如何实际拾取图像视图并拖动它。我正在计划当我拖动图像视图时,当用户将其放置到正确的位置时,它会锁定在那里。我真的不知道该怎么做,这已经困扰我一段时间了。
非常感谢!
Ok, so I have always wondered how do I actually pick up an image view and drag it. I was planning when I drag an image view and when user places it to correct location it locks there. I really don't have idea how to do this and it has bothered me for sometime.
Thanks so much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者,如果您不想子类化视图并自己处理触摸,则可以使用
UIPanGestureRecognizer
。在任何类(例如视图控制器)中创建一个平移识别器并将其添加到您的视图中:
然后简单地说:
如果您像我一样喜欢 Interface Builder,您也可以将平移手势识别器拖到图像视图上,然后连接
handlePanGesture:
的识别器应声明为IBAction
而不是void
。Or you can use a
UIPanGestureRecognizer
if you don't want to subclass the view and handle touches yourself.Create a pan recognizer in any class (e.g. view controller) and add it to your view:
And then simply:
If you like Interface Builder as much as me you can also just drag a pan gesture recognizer over your image view, and then connect the recognizer to the
handlePanGesture:
that should be declared as anIBAction
instead ofvoid
.如果您喜欢阅读,请查看
UIResponder
类参考,特别是touchesBegan
和touchesMoved
。If you're one for reading, then check out the
UIResponder
Class Reference, and, in particular,touchesBegan
andtouchesMoved
.我已经使用过这个,但是用户可以自由地将 imageview 拖动到他们想要的位置。这是我的 swift 自定义类
只需将这个自定义类与您的 UIImageView 一起使用
I already worked with this, but the user can drag imageview freely with position that they want. and this is my custom class with swift
Just user this custom class with your UIImageView