zoukankan      html  css  js  c++  java
  • 手势图片:拖拽捏合旋转放大

     //转载他处

    #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

  • 相关阅读:
    02-27 朴素贝叶斯
    JQuery UI datepicker 使用方法(转)
    纯CSS兑现侧边栏/分栏高度自动相等(转)
    在触屏设备上面利用html5裁剪图片(转)
    简单几步让CentOS系统时间同步(转)
    百度与谷歌地图坐标转换代码(转)
    jQuery插件开发全解析(转)
    Centos 安装ImageMagick 与 imagick for php步骤详解
    将windows目录共享到linux
    Event事件的兼容性(转)
  • 原文地址:https://www.cnblogs.com/OIMM/p/5542928.html
Copyright © 2011-2022 走看看