[英]How to allow a user to move a button via iOS sdk
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.
该视图当前是通过 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.
理想的实现将检查 NSUserDefaults 中的标志,如果存在,则允许用户移动 object,放下它,然后删除将保存设置以供用户下次加载的标志。
Anyone know if anything like this would be possible?任何人都知道这样的事情是否可能?
You can have it done by using the action method -您可以使用 action 方法完成它 -
//called on each instance during a drag event //在拖动事件期间在每个实例上调用
- (IBAction)draggedOut: (id)sender withEvent: (UIEvent *) event {
UIButton *selected = (UIButton *)sender;
selected.center = [[[event allTouches] anyObject] locationInView:self.view];
}
Since UIControl
is a UIView
, you can use addGestureRecognizer
to install on you object a UIPanGestureRecognizer
.由于
UIControl
是UIView
,您可以使用addGestureRecognizer
在您的 object 上安装UIPanGestureRecognizer
。
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. UIGestureRecognizer
允许每次在您附加手势识别器的视图上执行特定手势时调用您编写的 function。 You create one like this in your view controller viewDidLoad
method:您在视图 controller
viewDidLoad
方法中创建一个这样的:
-(void)viewDidLoad {
...
gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDragOnButton)];
[button addGestureRecognizer:gestureRecognizer];
...
[super viewDidLoad];
}
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
.因此,在按钮上完成的每个“平移”(拖动)手势都会导致对
handleDragOnButton
的调用。 In the code above, I assume that you your view controller contains declarations like:在上面的代码中,我假设您的视图 controller 包含如下声明:
@interface MyViewController : UIViewController {
...
IBOutlet UIButton* button; //-- this is created through IB
UIPanGestureRecognizer* gestureRecognizer; //-- this will be added by you
...
}
Now, you need define a handleDragOnButton
in your controller.现在,您需要在 controller 中定义一个
handleDragOnButton
。 In this function, you can get the current touch and move the button accordingly by changing its frame
.在此 function 中,您可以通过更改其
frame
来获取当前触摸并相应地移动按钮。
- (void)handleDragOnButton:(UIPanGestureRecognizer*)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
CGPoint touchLocation = [recognizer locationInView:recognizer.view];
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
<your logic here>
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
<your logic here>
}
}
Don't forget to release
the gesture recognizer in the controller's dealloc
.不要忘记在控制器的
dealloc
中release
手势识别器。
Look also at this doc from Apple .另请查看Apple 的此文档。
An alternative could be using UIResponder
's另一种可能是使用
UIResponder
的
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:
but I would only suggest that if you are targeting iOS 3.1.3 and older.但我只建议如果您的目标是 iOS 3.1.3 及更早版本。
There is some open sourced code that does exactly that.有一些开源代码可以做到这一点。
https://github.com/gmoledina/GMGridView 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." “用于 iOS (iPhone/iPad) 的高性能 Grid-View 允许使用手势对视图进行排序(用户可以用手指移动项目以对其进行排序)和捏合/旋转/平移手势允许用户玩视图和从单元格视图切换到全尺寸显示。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.