【问题标题】:floating button which can be dragged to anywhere in the view controller?可以拖动到视图控制器中的任何位置的浮动按钮?
【发布时间】:2016-01-30 14:17:52
【问题描述】:

看到很多问题来创建一个浮动按钮,效果很好。但是是否可以拖动按钮以便用户可以将其保留在任何他想要的地方?

如果有人使用过“flipkart”应用程序,您可以看到“ping”按钮。这就是我要找的。任何人有什么想法吗?

提前致谢

【问题讨论】:

  • 你尝试了什么?您的代码在哪里崩溃?
  • 发布您的代码。但是这里有个思路:添加平移手势,在平移上应用仿射平移变换,将视图中平移手势平移设置回零
  • cocoacontrols.com/controls/vcfloatingactionbutton。我使用了这个控件。但是这里我们不能移动或拖动按钮。用户可以通过拖动来移动按钮吗?

标签: ios objective-c iphone


【解决方案1】:

是的,这是可能的,您可以在 iOS 中拖动 UIButtons 和 UIViews。以下是我在谷歌搜索“拖动 uibutton ios”时发现的一些解释方法的帖子:

Basic Drag and Drop in iOS

how to make UIbutton draggable within UIView?

Touch and drag a UIButton around, but don't trigger it when releasing the finger

如果您想在重新启动应用程序时保存用户拖动到的位置,请使用 NSUserDefaults。

【讨论】:

    【解决方案2】:

    您可以像这样使用 UIPanGestureRecognizer:

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property (strong, nonatomic) UIView *snapshotView;
    
    @property (weak, nonatomic) IBOutlet UIButton *moveMeButton;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        [self.moveMeButton addGestureRecognizer:recognizer];
    }
    
    - (void)handlePan:(UIPanGestureRecognizer*)recognizer {
    
        CGPoint translation = [recognizer translationInView:self.view];
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
        [recognizer setTranslation:CGPointZero inView:self.view];
    }
    
    @end
    


    干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 2016-04-02
      相关资源
      最近更新 更多