1

i have an UIView subclass for drag and drop object, it's work all perfectly but after i apply this transformation

    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
    self.transform = transform;

When go to drag and drop, object start an infinite spiral :-/

Touch Began

if (!moveObject){

    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
    self.transform = transform;

}else{

    // When a touch starts, get the current location in the view
    currentPoint = [[touches anyObject] locationInView:self];
}

Touch moved code is here

if (moveObject){

    //Current point is locationInView get in touchBegin
    // Get active location upon move
    CGPoint activePoint = [[touches anyObject] locationInView:self];

    // Determine new point based on where the touch is now located
    CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
                                   self.center.y + (activePoint.y - currentPoint.y));
    // Set new center location
    self.center = newPoint;
}

How i can resolve this?

P.S. moveObject is declared elsewhere, and is correctly set YES/NO.


I don't want move and rotate object at same time, only 1 thing per time. When i rotate UIView i want to maintain a new orientation and i must be able to move object with new orientation.

ADDED Here my xcode project example. https://www.dropbox.com/s/lza1n9kjnxa4zun/xcode.zip

  • Where do you apply this transformation? – Ossir Jan 18 '14 at 19:32
  • @Ossir in touchesBegan if a boolean flag is true i made the transformation else i move the object –  Jan 18 '14 at 19:35
  • The code you posted is ok. You should show us code with this check before applying transformation and the code where you're changing this boolean. The error is there, I guess=) – Ossir Jan 18 '14 at 19:38
  • @Ossir check the code, i have added my touchesBegan –  Jan 18 '14 at 19:42
  • Where you change `moveObject` to `YES` ? Try to do it right after `self.transform = transform;` – Ossir Jan 18 '14 at 19:44
  • @Ossir i change moveObject by UIButton action, when button is OFF can move, when button is ON i set moveObject to YES and can rotate uiview, you cannot rotate and move at same time. –  Jan 18 '14 at 19:52
  • Oh, now I get it. So you want to different actions, I will change my answer then – Ossir Jan 18 '14 at 19:53

2 Answers2

1

I tested this, and it works like charm:

- (void)viewDragged:(UIPanGestureRecognizer *)gesture
{

    CGPoint translation = [gesture translationInView:self.view];

    gesture.view.center = CGPointMake(gesture.view.center.x + translation.x, gesture.view.center.y + translation.y);


    [gesture setTranslation:CGPointZero inView:gesture.view];

}
Albara
  • 1,306
  • 8
  • 14
  • but now i use touches method for drag and drop my object not gesture recognizer –  Jan 18 '14 at 19:41
1

EDITED Now when we know more info...

If you want two different action with some sort of switcher as moveObject you need to move all your code into else block:

if (!moveObject){
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
    self.transform = transform;
} 
else {
    // When a touch starts, get the current location in the view
    currentPoint = [[touches anyObject] locationInView:self];
    //Current point is locationInView get in touchBegin
    // Get active location upon move
    CGPoint activePoint = [[touches anyObject] locationInView:self];

    // Determine new point based on where the touch is now located
    CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
                                   self.center.y + (activePoint.y - currentPoint.y));
    // Set new center location
    self.center = newPoint;
}

Or even better, If you want to rotate your view on 90 degrees every touch, then change transformation code to this:

if (!moveObject){
    self.transform = CGAffineTransformRotate(self.transform, M_PI_2);
}
else {
    ….
} 

You can set lesser value instead of M_PI_2 to achieve slow rotation while panning with your finger=)

Ossir
  • 3,109
  • 1
  • 34
  • 52
  • I insert currentPoint = [[touches anyObject] locationInView:self]; outside but not work, also moveObject is correctly set to YES or NO, i not forgot. –  Jan 18 '14 at 19:56
  • 1
    Also check out answer from @Unheilig it will be definitely your next problem=) – Ossir Jan 18 '14 at 20:01