2013-03-11 3 views

ответ

3

вы можете добиться этого в следующем виде:

вы можете использовать touchesMoved жест или использовать UIPanGestureRecognizer

1) с использованием touchesMoved:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:self]; 
    placardView.center = location; 
    return; 
} 

2) с использованием UIPanGestureRecognizer

добавить распознавателя жестов к кнопке на 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 для справки.

Смежные вопросы