【转】使用手势对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;
    }
}  
时间: 2024-10-07 09:16:34

【转】使用手势对UIImageView进行缩放、旋转和移动的相关文章

使用手势对UIImageView进行缩放、旋转和移动

[cpp] view plain copy // 添加所有的手势 - (void) addGestureRecognizerToView:(UIView *)view { // 旋转手势 UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)]; [view addG

iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

其余的请见:http://blog.csdn.net/totogo2010/article/details/8615940 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类.手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别. UITapGes

iOS手势UIGestureRecognizer识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) (转)

1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类.手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别. UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecog

IOS开发UI篇—手势识别器(拖拽+旋转+缩放)

IOS开发UI篇—手势识别器(拖拽+旋转+缩放) 一.拖拽 示例代码: 1 // 2 // YYViewController.m 3 // 06-拖拽事件 4 // 5 // Created by apple on 14-6-19. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController ()

UI-target...action设计模式,手势识别器.UIimageview

target-action设计模式 iOS设计模式之Target-Action主要是为了降低代码的耦合性.顾名思义      Target-Action模式就是指  目标-动作模式,它贯穿于iOS开发始终. 提到Target-Action,先说2个词     “高内聚,低耦合”      这主要是评价一个软件的好坏 它评判软件的好坏主要靠模板之间内聚是否高,模块间耦合度是否低. 其实Target-action模式很简单,就是当某个事件发生时,调用那个对象中的那个方法.比如:点击按钮时,调用Con

UI基础:target...action设计模式,手势识别器.UIimageview

使用target..action和delegate设计模式可以实现解耦.使代码更加优化. 手势识别器: 手势识别器:是对触摸事件做了封装,无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用,所在我们可以把重心放在识别之后该去做什么操作上面.很方便. 手势识别器是iOS中比较抽象的一个类,用于识别一个手势,所谓的手势:有规律的触摸. 手势识别器有7个子类: 分别是:轻怕手势,轻移手势,清扫手势,缩放手势,旋转手势,长按手势,以及屏幕边缘平移手势. 一旦指定的手势别识别了,就可以执行自定义

WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示

原文:WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示 为方便描述, 这里仅以正方形来做演示, 其他图形从略. 运行时效果图: XAML代码:// Transform.XAML <Canvas Width="700" Height="700" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  xmlns:x="http://sc

【转】iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) -- 不错不错

原文网址:http://blog.csdn.net/totogo2010/article/details/8615940 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类.手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别. UITapGest

[C#] Graphics平移缩放旋转

[平移] private void btnTranslate_Click(object sender, EventArgs e) { Graphics graphics = this.CreateGraphics(); // 红色笔 Pen pen = new Pen(Color.Red, 5); Rectangle rect = new Rectangle(0, 0, 200, 50); // 用红色笔画矩形 graphics.DrawRectangle(pen, rect); // 向左平移