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

发布于 2024-11-18 04:03:12 字数 198 浏览 4 评论 0原文

我正在尝试用我的应用程序做一些新的事情。

我有许多按钮,我希望用户能够通过拖动它们在屏幕上重新排列它们。该视图当前是通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水溶 2024-11-25 04:03:12

您可以通过使用操作方法来完成此操作 -

// 在拖动事件期间在每个实例上调用

- (IBAction)draggedOut: (id)sender withEvent: (UIEvent *) event {
    UIButton *selected = (UIButton *)sender;
    selected.center = [[[event allTouches] anyObject] locationInView:self.view];
} 

You can have it done by using the action method -

//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];
} 
幽蝶幻影 2024-11-25 04:03:12

由于 UIControlUIView,因此您可以使用 addGestureRecognizer 在对象上安装 UIPanGestureRecognizer

UIGestureRecognizer 允许每次在附加手势识别器的视图上执行特定手势时调用您编写的函数。您可以在视图控制器 viewDidLoad 方法中创建这样的一个:

-(void)viewDidLoad {
     ...
     gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDragOnButton)];
     [button addGestureRecognizer:gestureRecognizer];
     ...
     [super viewDidLoad];
}

此代码将实例化一个手势识别器并将其附加到您的按钮。因此,在按钮上执行的每个“平移”(拖动)手势都会导致调用 handleDragOnButton
在上面的代码中,我假设您的视图控制器包含如下声明:

@interface MyViewController : UIViewController {
    ...
    IBOutlet UIButton* button;   //-- this is created through IB
    UIPanGestureRecognizer* gestureRecognizer;   //-- this will be added by you
    ...
}    

现在,您需要在控制器中定义一个handleDragOnButton。在此函数中,您可以获取当前触摸并通过更改其框架来相应地移动按钮。

 - (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>

        }
  }

不要忘记在控制器的dealloc 中释放手势识别器。

另请参阅 此文档来自 Apple

另一种方法是使用 UIResponder,

 – touchesBegan:withEvent:
 – touchesMoved:withEvent:
 – touchesEnded:withEvent:
 – touchesCancelled:withEvent:

但我仅建议您针对 iOS 3.1.3 及更早版本使用。

Since UIControl is a UIView, you can use addGestureRecognizer to install on you object a 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. You create one like this in your view controller viewDidLoad method:

-(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.
In the code above, I assume that you your view controller contains declarations like:

@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. In this function, you can get the current touch and move the button accordingly by changing its 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.

Look also at this doc from Apple.

An alternative could be using UIResponder's

 – touchesBegan:withEvent:
 – touchesMoved:withEvent:
 – touchesEnded:withEvent:
 – touchesCancelled:withEvent:

but I would only suggest that if you are targeting iOS 3.1.3 and older.

生生漫 2024-11-25 04:03:12

有一些开源代码可以做到这一点。

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文