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

  • 相关阅读:
    《高质量C/C++》读书笔记
    刚刚从网易搬过来了 ,以后就在这安家落户喽
    别让男孩为你哭泣
    EXT综合训练
    liferay中使用自己的数据库
    liferay环境搭建
    第七课,Extjs中常用表单介绍与应用二
    配电管理地理信息系统解决方案
    liferay学习第一站
    第九课,Extjs数据处理
  • 原文地址:https://www.cnblogs.com/OIMM/p/5542928.html
Copyright © 2011-2022 走看看