繁体   English   中英

在屏幕上移动按钮

[英]Move a button in the screen

我有一个UIButton ,我想通过在屏幕上触摸和滑动来移动该按钮。 当我释放触摸时,它将处于当前位置。 请清楚解释。

您可以使用触摸移动事件来移动视图。 Apple提供了一个示例教程MoveMe ,可拖动视图并在释放触摸后为视图添加动画。 专门检查MoveMeView.m中的触摸事件(touchesBegan,touchesMoved,touchesEnded),以了解它们如何移动placardView。 您可以像placardView一样移动按钮。

“拖动”中移出一个uibutton,使其像uiScrollView

检查这个

您应该从这些点开始移动框架并相应地移动框架,以便您的按钮在触摸位置移动

如果您要为iOS 3.2及更高版本编写脚本,请考虑使用UIPanGestureRecognizer

只需附加一个这样的实例,

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

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

并定义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