通过手势识别实现图片的缩放与旋转
#import <UIKit/UIKit.h>
@interface BIDViewController : UIViewController <UIGestureRecognizerDelegate>
@property (strong, nonatomic) UIImageView *imageView;
@end
#import "BIDViewController.h"
@interface BIDViewController ()
@end
@implementation BIDViewController {
CGFloat scale, previousScale;
CGFloat rotation, previousRotation;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
previousScale = 1;
UIImage *image = [UIImage imageNamed:@"test"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.userInteractionEnabled = NO;
self.imageView.center = CGPointMake(160, 160);
[self.view addSubview:self.imageView];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doPinch:)];
pinchGesture.delegate = self;
[self.view addGestureRecognizer:pinchGesture];
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(doRotate:)];
rotationGesture.delegate = self;
[self.view addGestureRecognizer:rotationGesture];
}
//- (void)didReceiveMemoryWarning
//{
// [super didReceiveMemoryWarning];
// // Dispose of any resources that can be recreated.
//}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (void)transfromImageView
{
CGAffineTransform t = CGAffineTransformMakeScale(scale * previousScale, scale * previousScale);
t = CGAffineTransformRotate(t, rotation + previousRotation);
self.imageView.transform = t;
}
- (void)doPinch:(UIPinchGestureRecognizer *)gesture
{
scale = gesture.scale;
[self transfromImageView];
if (gesture.state == UIGestureRecognizerStateEnded) {
previousScale = scale * previousScale;
scale = 1;
}
}
- (void)doRotate:(UIRotationGestureRecognizer *)gesture
{
rotation = gesture.rotation;
[self transfromImageView];
if (gesture.state == UIGestureRecognizerStateEnded) {
previousRotation = rotation + previousRotation;
rotation = 0;
}
}
@end