简体   繁体   English

如何允许用户通过 iOS sdk 移动按钮

[英]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 .由于UIControlUIView ,您可以使用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 .不要忘记在控制器的deallocrelease手势识别器。

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.

相关问题 如何移动按钮动作iOS - How to move button action ios iOS Card.io SDK会允许用户手动输入名称吗? - iOS Card.io SDK will allow user enter the name manually? 无法使用测试用户通过Facebook iOS SDK 3登录 - Can't login via Facebook iOS SDK 3 with a test user iOS - 如何在单击按钮时移动到其他页面 - iOS - how to move to a different page on click of a button 如何允许用户通过iOS浏览器将图像上传到我们的网站? - How to allow users to upload images to our site via iOS browser? 如何在 iOS 中允许用户一次只为一台设备使用该应用程序? - How to allow the user to use the application only for one device at a time in iOS? 如何在iOS中使用自定义按钮对Tableview单元进行重新排序,但不使用Move Edit图标进行重新排序 - How to Reorder tableview cell with a custom button, but not with move edit icon in ios 如何在 XCode(iOS SDK) 中将按钮添加到注释视图 - How to add button to annotation view in XCode(iOS SDK) 如何使用iOS Pin It SDK在Pinterest中实现Followboard按钮功能? - How to achieve Followboard button functionality in Pinterest using iOS Pin It SDK? 如何在Google Maps iOS SDK上显示用户位置 - How to show user location on Google Maps iOS SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM