简体   繁体   English

在屏幕上移动按钮

[英]Move a button in the screen

I have an UIButton and I want to move that button by touching and swiping it in the screen. 我有一个UIButton ,我想通过在屏幕上触摸和滑动来移动该按钮。 When I release the touch it will be in the current position. 当我释放触摸时,它将处于当前位置。 Explain clearly, please. 请清楚解释。

You can move a view by using touch moved event. 您可以使用触摸移动事件来移动视图。 There is a sample tutorial MoveMe by Apple which drags a view and after releasing the touch animate the view. Apple提供了一个示例教程MoveMe ,可拖动视图并在释放触摸后为视图添加动画。 Check specially the touch events (touchesBegan, touchesMoved, touchesEnded) in MoveMeView.m to get the idea how they have moved placardView. 专门检查MoveMeView.m中的触摸事件(touchesBegan,touchesMoved,touchesEnded),以了解它们如何移动placardView。 You can move your button just like the placardView. 您可以像placardView一样移动按钮。

Taken from 'drag' move a uibutton so it acts like uiScrollView “拖动”中移出一个uibutton,使其像uiScrollView

check this 检查这个

you should make move frame from the points and move frame accordingly so that your button moves in places of touches 您应该从这些点开始移动框架并相应地移动框架,以便您的按钮在触摸位置移动

If you are scripting for iOS 3.2 and above, consider using UIPanGestureRecognizer . 如果您要为iOS 3.2及更高版本编写脚本,请考虑使用UIPanGestureRecognizer

Simply attach an instance of it like this, 只需附加一个这样的实例,

...
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;

[self.button addGestureRecognizer:panGesture];
[panGesture release];
...

and define handlePan: like this, 并定义handlePan:这样,

- (void)handlePan:(UIPanGestureRecognizer *)panGesture {
    CGRect buttonFrame = self.button.frame;
    CGPoint translation = [panGesture translationInView:panGesture.view];

    buttonFrame.origin.x += translation.x;
    buttonFrame.origin.y += translation.y;

    [panGesture setTranslation:CGPointMake(0, 0) inView:panGesture.view];
    self.button.frame = buttonFrame;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM