PanGesture with multiple subviews

The following code snippet is based on the iPhone documentation for handling the pan gesture, although theres still used the translate method which has been replaced. The method also caters for panning all of the views subviews so that everything moves.

- (IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender {
	static CGPoint prevTranslate;

    CGPoint translate = [sender translationInView:self.OfficeImage];
		UIView *subView;
	for(subView in self.view.subviews)
	{

		CGRect newFrame = subView.frame;
		newFrame.origin.x += (translate.x - prevTranslate.x);
		newFrame.origin.y += (translate.y - prevTranslate.y);
		subView.frame = newFrame;
	}

	prevTranslate = translate;

    if (sender.state == UIGestureRecognizerStateEnded)
        prevTranslate = CGPointMake(0, 0);
}


One Response to “PanGesture with multiple subviews”

  1. Thank you, this was quite helpful.

Leave a Reply