如何允许用户通过 iOS sdk 移动按钮
我正在尝试用我的应用程序做一些新的事情。
我有许多按钮,我希望用户能够通过拖动它们在屏幕上重新排列它们。该视图当前是通过 Interface Builder 创建的。理想的实现将检查 NSUserDefaults 中的标志,如果存在该标志,则允许用户移动对象,放下它,然后删除该标志,该标志将保存设置以供下次用户加载。
有人知道这样的事情是否可能吗?
I'm trying to do something new with my app.
I have a number of buttons that I'd like the user to be able to rearrange on the screen by dragging them. The view is currently created via Interface Builder. The ideal implementation would check for a flag in NSUserDefaults which if present would allow the user to move the object, drop it,then remove the flag which would save the setup for next time the user loads.
Anyone know if anything like this would be possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过使用操作方法来完成此操作 -
// 在拖动事件期间在每个实例上调用
You can have it done by using the action method -
//called on each instance during a drag event
由于
UIControl
是UIView
,因此您可以使用addGestureRecognizer
在对象上安装UIPanGestureRecognizer
。UIGestureRecognizer
允许每次在附加手势识别器的视图上执行特定手势时调用您编写的函数。您可以在视图控制器viewDidLoad
方法中创建这样的一个:此代码将实例化一个手势识别器并将其附加到您的按钮。因此,在按钮上执行的每个“平移”(拖动)手势都会导致调用
handleDragOnButton
。在上面的代码中,我假设您的视图控制器包含如下声明:
现在,您需要在控制器中定义一个
handleDragOnButton
。在此函数中,您可以获取当前触摸并通过更改其框架
来相应地移动按钮。不要忘记在控制器的dealloc 中
释放
手势识别器。另请参阅 此文档来自 Apple。
另一种方法是使用 UIResponder,
但我仅建议您针对 iOS 3.1.3 及更早版本使用。
Since
UIControl
is aUIView
, you can useaddGestureRecognizer
to install on you object aUIPanGestureRecognizer
.A
UIGestureRecognizer
allows a function you wrote to be called each time the specific gesture is executed on the view you attach the gesture recognizer to. You create one like this in your view controllerviewDidLoad
method:This code will instantiate a gesture recognizer and attach it to your button. So, every gesture of kind "pan" (dragging) done on the button will cause a call to
handleDragOnButton
.In the code above, I assume that you your view controller contains declarations like:
Now, you need define a
handleDragOnButton
in your controller. In this function, you can get the current touch and move the button accordingly by changing itsframe
.Don't forget to
release
the gesture recognizer in the controller'sdealloc
.Look also at this doc from Apple.
An alternative could be using
UIResponder
'sbut I would only suggest that if you are targeting iOS 3.1.3 and older.
有一些开源代码可以做到这一点。
https://github.com/gmoledina/GMGridView
“适用于 iOS (iPhone/iPad) 的高性能网格视图)允许使用手势对视图进行排序(用户可以用手指移动项目来对它们进行排序),并且捏合/旋转/平移手势允许用户玩视图并从单元视图切换到全尺寸显示。”
There is some open sourced code that does exactly that.
https://github.com/gmoledina/GMGridView
"A performant Grid-View for iOS (iPhone/iPad) that allows sorting of views with gestures (the user can move the items with his finger to sort them) and pinching/rotating/panning gestures allow the user to play with the view and toggle from the cellview to a fullsize display."