簡體   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