我想使用拖動在我的應用程序中移動UIButton。 UIButton是使用代碼創建的。 我該怎麼做?使用手指拖動UIButton
1
A
回答
3
您可以按以下方式實現這一目標:
可以使用touchesMoved
手勢或使用使用UIPanGestureRecognizer
1)touchesMoved:使用UIPanGestureRecognizer
添加
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
placardView.center = location;
return;
}
2)手勢識別器viewDidLoad
上的按鈕如下:
UIPanGestureRecognizer *objGesture= [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveButton:)];
[myButton addGestureRecognizer:objGesture];
[objGesture release];
寫被稱爲目標方法
-(void)moveButton:(UIPanGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateChanged ||
recognizer.state == UIGestureRecognizerStateEnded) {
UIView *draggedButton = recognizer.view;
CGPoint translation = [recognizer translationInView:self.view];
CGRect newButtonFrame = draggedButton.frame;
newButtonFrame.origin.x += translation.x;
newButtonFrame.origin.y += translation.y;
draggedButton.frame = newButtonFrame;
[recognizer setTranslation:CGPointZero inView:self.view];
}
}
0
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self];
button.center = pt;
}
0
嘗試使用panGestureRecognizer
或使用touches
方法。 點擊here以獲得更多參考。
相關問題
- 1. UIButton將手指拖入界限
- 2. 在拖動UIButton時移動拖動手柄
- 3. 在手指下拖動UIView
- 4. 手指觸摸/按/拖動
- 5. 在觸摸手機上使用Javascript檢測手指拖動?
- 6. UIButton長按手指靜止
- 7. 手指拖動時觸發按鈕它
- 8. 如何使UIbutton在UIView內拖動?
- 9. '拖'移動一個uibutton,使它像uiScrollView
- 10. 的UIButton接受1個手指2的手指敲擊事件
- 11. UIButton和滑動手勢
- 12. iPhone Dev - 手動創建UIButton
- 13. 通過視圖拖動手指並使其工作
- 14. 實現觸摸用手指拖動各種圖像
- 15. Android - 用手指拖動imageview不會更新位置
- 16. 如何用兩個手指拖動視圖
- 17. 用手指拖動Android圖像視圖表現奇怪
- 18. 幫助觸摸移動時,拖動手指
- 19. 樹葉下降動畫和手指拖動
- 20. 團結遊戲拖動對象與手指運動
- 21. Doubletap和滑動/拖動手指放大/縮小
- 22. UIButton網格激活同時拖動
- 23. 如何更好地拖動UIButton? UIButton的UIControlEventTouchUpInside事件不叫
- 24. 使用SendMessage拖動拖放
- 25. 使用手動設置一個UIButton狀態
- 26. 使用滑動手勢交換兩個UIButton
- 27. 陣營母語:創建動畫認爲,用手指直接拖拽滑動
- 28. 使uibutton脈動。
- 29. 用兩個手指手勢滾動
- 30. Unity3d拖動手指有問題的對象