zoukankan      html  css  js  c++  java
  • 【转】使用手势对UIImageView进行缩放、旋转和移动

    原文地址http://blog.csdn.net/crazy_frog/article/details/8664108/

    // 添加所有的手势
    - (void) addGestureRecognizerToView:(UIView *)view
    {
        // 旋转手势
        UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
        [view addGestureRecognizer:rotationGestureRecognizer];
        
        // 缩放手势
        UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
        [view addGestureRecognizer:pinchGestureRecognizer];
        
        // 移动手势
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
        [view addGestureRecognizer:panGestureRecognizer];
    }
    
    // 处理旋转手势
    - (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer
    {
        UIView *view = rotationGestureRecognizer.view;
        if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {
            view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);
            [rotationGestureRecognizer setRotation:0];
        }
    }
    
    // 处理缩放手势
    - (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
    {
        UIView *view = pinchGestureRecognizer.view;
        if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
            view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
            pinchGestureRecognizer.scale = 1;
        }
    }
    
    // 处理拖拉手势
    - (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer
    {
        UIView *view = panGestureRecognizer.view;
        if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
            CGPoint translation = [panGestureRecognizer translationInView:view.superview];
            [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
            [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
        }
    }

    [self addGestureRecognizerToView:view];

    //如果处理的是图片,别忘了
    [imageView setUserInteractionEnabled:YES];
    [imageView setMultipleTouchEnabled:YES];

     

    在.h文件里边定义变量

    @interface YourViewController : UIViewController<UIGestureRecognizerDelegate>  
    {  
        CGFloat lastScale;  
        CGRect oldFrame;    //保存图片原来的大小  
        CGRect largeFrame;  //确定图片放大最大的程度  
    }  

    viewDidLoad里面加上

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        showImgView = [[UIImageView alloc] initWithFrame:<span class="s1">CGRectMake</span>(<span class="s2">0</span>, <span class="s2">0</span>, 320, 480)];
        [showImgView setMultipleTouchEnabled:YES];
        [showImgView setUserInteractionEnabled:YES];
        [showImgView setImage:[UIImage imageNamed:@"1.jpg"]];
        
        oldFrame = showImgView.frame;
        largeFrame = CGRectMake(0 - screenSize.width, 0 - screenSize.height, 3 * oldFrame.size.width, 3 * oldFrame.size.height);
        
        [self addGestureRecognizerToView:showImgView];
        [self.view addSubview:showImgView];

    我修改了缩放的代码,增加了限制,其他的类似

    // 处理缩放手势  
    - (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer  
    {  
        UIView *view = pinchGestureRecognizer.view;  
        if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
            view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);  
            if (showImgView.frame.size.width < oldFrame.size.width) {  
                showImgView.frame = oldFrame;  
                //让图片无法缩得比原图小  
            }  
            if (showImgView.frame.size.width > 3 * oldFrame.size.width) {  
                showImgView.frame = largeFrame;  
            }  
            pinchGestureRecognizer.scale = 1;  
        }  
    }  
  • 相关阅读:
    随机生成字符串,可用来当id
    自己总结的关于uniapp项目用androidstuido打包成apk
    css3 滑动效果 门
    在数组里面随机获取随机的几个内容
    把一个数组分割成两个(不管奇数还是偶数)
    Mysql常用命令
    怎样才能彻底地删除多余输入法软件
    启动MySQL服务
    linux监控命令全覆盖
    浅谈MVC模式与SSH框架
  • 原文地址:https://www.cnblogs.com/qianyindichang/p/4000187.html
Copyright © 2011-2022 走看看