2013-03-11 77 views

回答

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以獲得更多參考。