Last active
August 29, 2015 14:04
-
-
Save STAR-ZERO/db5aa411ac51daa4dbd9 to your computer and use it in GitHub Desktop.
GestureRecognizerで移動、拡大・縮小、回転
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 GestureView : UIView <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 "GestureView.h" | |
@interface GestureView () | |
@property (nonatomic, strong) UIImageView *imageView; | |
@end | |
@implementation GestureView | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self initializeView]; | |
} | |
return self; | |
} | |
- (void)initializeView | |
{ | |
self.imageView = [[UIImageView alloc] initWithFrame:self.frame]; | |
self.imageView.image = [UIImage imageNamed:@"image"]; | |
self.imageView.contentMode = UIViewContentModeScaleAspectFill; | |
[self addSubview:self.imageView]; | |
self.userInteractionEnabled = YES; | |
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; | |
pan.delegate = self; | |
[self addGestureRecognizer:pan]; | |
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)]; | |
rotation.delegate = self; | |
[self addGestureRecognizer:rotation]; | |
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)]; | |
pinch.delegate = self; | |
[self addGestureRecognizer:pinch]; | |
} | |
// 複数のジェスチャを認識させるために必要 | |
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer | |
{ | |
return YES; | |
} | |
// 移動 | |
- (void)panGesture:(UIPanGestureRecognizer *)gesture | |
{ | |
CGPoint translation = [gesture translationInView:self]; | |
self.imageView.center = CGPointMake(self.imageView.center.x + translation.x, self.imageView.center.y + translation.y); | |
[gesture setTranslation:CGPointZero inView:self]; | |
} | |
// 回転 | |
- (void)rotationGesture:(UIRotationGestureRecognizer *)gesture | |
{ | |
[self.imageView setTransform:CGAffineTransformRotate(self.imageView.transform, gesture.rotation)]; | |
gesture.rotation = 0; | |
} | |
// 拡大・縮小 | |
- (void)pinchGesture: (UIPinchGestureRecognizer *)gesture | |
{ | |
[self.imageView setTransform:CGAffineTransformScale(self.imageView.transform, gesture.scale, gesture.scale)]; | |
gesture.scale = 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment