//转载他处
#import "MainViewController.h" @interface MainViewController () @property (strong, nonatomic) UIImageView *imageView; @property (strong, nonatomic) UIImageView *imageView2; @property CGFloat lastRotation; @property CGRect frame1; @property CGRect frame2; @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; CGFloat pointY = self.view.center.y; self.imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"png-0838"]]; self.imageView2.frame = CGRectMake(0, 0, 220, 220); self.imageView2.center = CGPointMake(self.view.center.x, pointY*2/3); [self.view addSubview:self.imageView2]; self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"music"]]; self.imageView.frame = CGRectMake(0, 0, 220, 220); self.imageView.center = CGPointMake(self.view.center.x, pointY*4/3); [self.view addSubview:self.imageView]; self.view.multipleTouchEnabled = YES;//多个触摸启用 UIRotationGestureRecognizer *rotationG = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationImage:)]; rotationG.delegate = self; UIPinchGestureRecognizer *pinchGestureRecongnizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeImage:)]; pinchGestureRecongnizer.delegate = self; [self.imageView setUserInteractionEnabled:YES]; [self.imageView addGestureRecognizer:pinchGestureRecongnizer]; [self.imageView addGestureRecognizer:rotationG]; UIRotationGestureRecognizer *rotationG2 = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationImage2:)]; rotationG2.delegate = self; UIPinchGestureRecognizer *pinchGestureRecongnizer2 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeImage2:)]; pinchGestureRecongnizer2.delegate = self; [self.imageView2 setUserInteractionEnabled:YES]; [self.imageView2 addGestureRecognizer:pinchGestureRecongnizer2]; [self.imageView2 addGestureRecognizer:rotationG2];
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panpan:)]; [self.imageView addGestureRecognizer:panGesture]; UIPanGestureRecognizer *panGesture2=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panpan2:)]; [self.imageView2 addGestureRecognizer:panGesture2]; self.frame1 = self.imageView.frame; self.frame2 = self.imageView2.frame; } //移动 - (void)panpan:(UIPanGestureRecognizer *)sender { // NSLog(@"pan"); [self.view bringSubviewToFront:self.imageView];//放置在前面 CGPoint location = [sender locationInView:self.view];//获取在view中的位置 NSLog(@"(%f,%f)",location.x,location.y); sender.view.center = CGPointMake(location.x, location.y);//调换frame,固定位置 } - (void)panpan2:(UIPanGestureRecognizer *)sender { [self.view bringSubviewToFront:self.imageView2]; CGPoint location = [sender locationInView:self.view]; sender.view.center = CGPointMake(location.x, location.y); } //旋转 - (void)rotationImage:(UIRotationGestureRecognizer*)gesture { NSLog(@"rotation"); [self.view bringSubviewToFront:gesture.view]; CGPoint location = [gesture locationInView:self.view]; gesture.view.center = CGPointMake(location.x, location.y); if ([gesture state] == UIGestureRecognizerStateEnded) { self.lastRotation = 0; return; } CGAffineTransform currentTransform = self.imageView.transform; CGFloat rotation = 0.0 - (self.lastRotation - gesture.rotation); CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotation); self.imageView.transform = newTransform; self.lastRotation = gesture.rotation; } //放大,缩小 - (void)changeImage:(UIPinchGestureRecognizer*)pinchGestureRecognizer { NSLog(@"change"); [self.view bringSubviewToFront:pinchGestureRecognizer.view]; CGPoint location = [pinchGestureRecognizer locationInView:self.view]; pinchGestureRecognizer.view.center = CGPointMake(location.x, location.y); pinchGestureRecognizer.view.transform = CGAffineTransformScale(pinchGestureRecognizer.view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale); pinchGestureRecognizer.scale = 1; } - (void)changeImage2:(UIPinchGestureRecognizer*)pinchGestureRecognizer { [self.view bringSubviewToFront:pinchGestureRecognizer.view]; CGPoint location = [pinchGestureRecognizer locationInView:self.view]; pinchGestureRecognizer.view.center = CGPointMake(location.x, location.y); pinchGestureRecognizer.view.transform = CGAffineTransformScale(pinchGestureRecognizer.view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale); pinchGestureRecognizer.scale = 1; } - (void)rotationImage2:(UIRotationGestureRecognizer*)gesture { [self.view bringSubviewToFront:gesture.view]; CGPoint location = [gesture locationInView:self.view]; gesture.view.center = CGPointMake(location.x, location.y); if ([gesture state] == UIGestureRecognizerStateEnded) { self.lastRotation = 0; return; } CGAffineTransform currentTransform = self.imageView2.transform; CGFloat rotation = 0.0 - (self.lastRotation - gesture.rotation); CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotation); self.imageView2.transform = newTransform; self.lastRotation = gesture.rotation; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } @end
16:31:392016-05-30