Skip to content

Instantly share code, notes, and snippets.

@mspensieri
Created May 25, 2014 15:14
Show Gist options
  • Save mspensieri/95f0d8c72a3779c77665 to your computer and use it in GitHub Desktop.
Save mspensieri/95f0d8c72a3779c77665 to your computer and use it in GitHub Desktop.
View controller with pan, rotate, and pinch
@interface MainViewController ()
@property CGAffineTransform scale;
@property CGAffineTransform rotate;
@property CGAffineTransform translate;
@end
@implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
view.backgroundColor = [UIColor blackColor];
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(onPinch:)];
pinch.delegate = self;
[view addGestureRecognizer:pinch];
UIRotationGestureRecognizer* rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(onRotate:)];
rotate.delegate = self;
[view addGestureRecognizer:rotate];
[self.view addSubview:view];
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPan:)];
pan.delegate = self;
pan.minimumNumberOfTouches = 1;
pan.maximumNumberOfTouches = 2;
[view addGestureRecognizer:pan];
self.rotate = CGAffineTransformMakeRotation(0);
self.translate = CGAffineTransformMakeTranslation(0, 0);
self.scale = CGAffineTransformMakeScale(1.0, 1.0);
}
-(void)onPinch:(UIPinchGestureRecognizer*)recognizer
{
self.scale = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
[self update:recognizer.view];
}
-(void)onRotate:(UIRotationGestureRecognizer*)recognizer
{
self.rotate = CGAffineTransformMakeRotation(recognizer.rotation);
[self update:recognizer.view];
}
-(void)onPan:(UIPanGestureRecognizer*)recognizer
{
self.translate = CGAffineTransformTranslate(self.translate,[recognizer translationInView:self.view].x , [recognizer translationInView:self.view].y);
[recognizer setTranslation:CGPointZero inView:self.view];
[self update:recognizer.view];
}
-(void)update:(UIView*)view
{
view.transform = CGAffineTransformConcat(self.rotate, CGAffineTransformConcat(self.scale, self.translate));
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment