屏幕

2011-05-17 53 views
0

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

回答

2

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

'drag' move a uibutton so it acts like uiScrollView

+0

好答。它可以帮助我also..Thanks Jhaliya – 2012-11-28 09:44:41

2

检查已拍摄this

,则应该从点使移动框架和相应的移动框架,以便在触摸

+0

但它会不会看起来像弹跳效果... – 2012-11-28 09:46:09

1

的地方你的按钮动作如果你编写脚本的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; 
}