Created
October 30, 2010 16:06
-
-
Save okonomi/655456 to your computer and use it in GitHub Desktop.
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
#import <UIKit/UIKit.h> | |
@interface GestureImageView : UIImageView <UIGestureRecognizerDelegate> { | |
} | |
@end |
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
#import "GestureImageView.h" | |
#import <QuartzCore/QuartzCore.h> | |
@implementation GestureImageView | |
- (id)initWithImage:(UIImage *)image { | |
if ((self = [super initWithImage:image])) { | |
self.userInteractionEnabled = YES; | |
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] | |
initWithTarget:self action:@selector(handlePanGesture:)]; | |
[panGestureRecognizer setDelegate:self]; | |
[self addGestureRecognizer:panGestureRecognizer]; | |
[panGestureRecognizer release]; | |
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] | |
initWithTarget:self action:@selector(handleRotationGesture:)]; | |
[rotationGestureRecognizer setDelegate:self]; | |
[self addGestureRecognizer:rotationGestureRecognizer]; | |
[rotationGestureRecognizer release]; | |
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] | |
initWithTarget:self action:@selector(handlePinchGesture:)]; | |
[self addGestureRecognizer:pinchGestureRecognizer]; | |
[pinchGestureRecognizer release]; | |
CALayer *layer = [self layer]; | |
// 青い線で枠を囲む処理 | |
[layer setBorderWidth:1.0f]; | |
[layer setBorderColor:[[UIColor blueColor] CGColor]]; | |
} | |
return self; | |
} | |
- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer { | |
UIView *view = [gestureRecognizer view]; | |
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || | |
[gestureRecognizer state] == UIGestureRecognizerStateChanged) { | |
CGPoint translation = [gestureRecognizer translationInView:[view superview]]; | |
[view setCenter:CGPointMake([view center].x + translation.x, [view center].y + translation.y)]; | |
[gestureRecognizer setTranslation:CGPointZero inView:[view superview]]; | |
} | |
} | |
- (void)handleRotationGesture:(UIRotationGestureRecognizer *)gestureRecognizer { | |
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || | |
[gestureRecognizer state] == UIGestureRecognizerStateChanged) { | |
[gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]); | |
[gestureRecognizer setRotation:0]; | |
} | |
} | |
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer { | |
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || | |
[gestureRecognizer state] == UIGestureRecognizerStateChanged) { | |
[gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]); | |
[gestureRecognizer setScale:1]; | |
} | |
} | |
// ensure that the pinch, pan and rotate gesture recognizers on a particular view can all recognize simultaneously | |
// prevent other gesture recognizers from recognizing simultaneously | |
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer | |
{ | |
/* | |
// if the gesture recognizers's view isn't one of our pieces, don't allow simultaneous recognition | |
if (gestureRecognizer.view != firstPieceView && gestureRecognizer.view != secondPieceView && gestureRecognizer.view != thirdPieceView) | |
return NO; | |
*/ | |
// if the gesture recognizers are on different views, don't allow simultaneous recognition | |
if (gestureRecognizer.view != otherGestureRecognizer.view) | |
return NO; | |
return YES; | |
} | |
/* | |
// Only override drawRect: if you perform custom drawing. | |
// An empty implementation adversely affects performance during animation. | |
- (void)drawRect:(CGRect)rect { | |
// Drawing code | |
} | |
*/ | |
- (void)dealloc { | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment