2

I want to move some UIView by button inside this view. I can it, in that way:

 - (void)viewDidLoad
    {
[button addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
        [button addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents: UIControlEventTouchDragInside];
        [button addTarget:self action:@selector(dragEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
}

.

    - (void)dragBegan:(UIControl *)c withEvent:ev {

    UITouch *touch = [[ev allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

}

- (void)dragMoving:(UIControl *)c withEvent:ev {
    UITouch *touch = [[ev allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
 //This is moving view to touchPoint
SimpleView.center = touchPoint;


}

- (void)dragEnded:(UIControl *)c withEvent:ev {

}

How can i move it only then, if I do longPress on that button?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Black Pearl
  • 93
  • 1
  • 9
  • Do you mean [this](http://stackoverflow.com/questions/9272333/combine-longpress-gesture-and-drag-gesture-together)? – Coder404 Oct 10 '13 at 18:35
  • Yes, but how can I recorganize touch event in this? – Black Pearl Oct 10 '13 at 18:58
  • Wait, are asking for the x and y values of `touch` in your dragMoving and dragBegan methods? If so, look at [this](http://stackoverflow.com/questions/2733868/can-ipad-iphone-touch-points-be-wrong-due-to-calibration). – Coder404 Oct 10 '13 at 19:00
  • That I know, but how can I use it in UILongPressGestureRecognizer? – Black Pearl Oct 10 '13 at 19:06

2 Answers2

8

Try using this code. I've used this in a card game i developed. moving cards around using long press gestures. Hope i helps.

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addLongpressGesture:)];
 [longPress setDelegate:self];
 [YOUR_VIEW addGestureRecognizer:longPress];

- (void)addLongpressGesture:(UILongPressGestureRecognizer *)sender {

UIView *view = sender.view;

CGPoint point = [sender locationInView:view.superview];

if (sender.state == UIGestureRecognizerStateBegan){ 

  // GESTURE STATE BEGAN

}
else if (sender.state == UIGestureRecognizerStateChanged){

 //GESTURE STATE CHANGED/ MOVED

CGPoint center = view.center;
center.x += point.x - _priorPoint.x;
center.y += point.y - _priorPoint.y;
view.center = center;

// This is how i drag my views
}

else if (sender.state == UIGestureRecognizerStateEnded){

  //GESTURE ENDED
 }
ReyJenald
  • 220
  • 1
  • 7
  • You didn't understand! I need to longpress on button and drag the MY_VIEW, in same touch! – Black Pearl Oct 11 '13 at 07:51
  • well you add the gesture recognizer on your UIButton and in the UIGestureRecognizerStateChanged you manipulate the points of your MY_VIEW – ReyJenald Oct 14 '13 at 01:36
1

I would use this link provided by @Coder404 to detect whether or not the player used a long touch. Then, add an @property BOOL performedLongTouch and set that to YES in the selector passed into the UILongPressGestureRecognizer

Then, in your dragBegan and dragMoving functions, add a check for performedLongTouch and in your dragEnded function, set its value to NO

I know that seem pretty straight forward, but is that what you were looking for?

Community
  • 1
  • 1
doctorBroctor
  • 2,018
  • 1
  • 14
  • 17