手势图片:拖拽捏合旋转放大

 //转载他处

#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

转载于:https://www.cnblogs.com/OIMM/p/5542928.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值