Created
June 1, 2012 09:52
-
-
Save skyfe79/2850857 to your computer and use it in GitHub Desktop.
Using Pan, Pinch and Rotation gestures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)addGesture | |
{ | |
//: 드래그 | |
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; | |
pan.minimumNumberOfTouches = 1; | |
pan.maximumNumberOfTouches = 1; | |
[someView addGestureRecognizer:pan]; | |
//: 핀치 | |
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)]; | |
pinch.delegate = self; | |
[someView addGestureRecognizer:pinch]; | |
//: 회전 | |
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotateGesture:)]; | |
rotate.delegate = self; | |
[someView addGestureRecognizer:rotate]; | |
} | |
-(void)handlePanGesture:(UIGestureRecognizer *)recognizer | |
{ | |
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer*)recognizer; | |
CGPoint delta = [pan translationInView:pan.view]; | |
[someView setCenter:CGPointMake(someView.center.x + delta.x, someView.center.y + delta.y)]; | |
[pan setTranslation:CGPointZero inView:someView]; | |
} | |
-(void)handlePinchGesture:(UIGestureRecognizer *)recognizer | |
{ | |
static CGRect initialBounds; | |
if (recognizer.state == UIGestureRecognizerStateBegan) | |
{ | |
initialBounds = someView.bounds; | |
} | |
CGFloat factor = [(UIPinchGestureRecognizer *)recognizer scale]; | |
CGAffineTransform zoom = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor); | |
someView.bounds = CGRectApplyAffineTransform(initialBounds, zoom); | |
} | |
-(void)handleRotateGesture:(UIGestureRecognizer *)recognizer | |
{ | |
UIRotationGestureRecognizer *rotate = (UIRotationGestureRecognizer *)recognizer; | |
CGFloat rotation = [rotate rotation]; | |
CGAffineTransform transform = CGAffineTransformRotate(someView.transform, rotation); | |
someView.transform = transform; | |
[rotate setRotation:0]; | |
} | |
#pragma mark - | |
#pragma mark UIGestureDelegate | |
#pragma mark - | |
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer | |
{ | |
if (![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && ![otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { | |
return YES; | |
} | |
return NO; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment