#import "RootViewController.h"

@interface RootViewController ()
{
    UIImageView *imageView;
    NSInteger number;//用于存放图片的名字
    BOOL flag;//用于标记当前是哪张图片

}

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    //默认值是yes;用于点击事件更改照片
    flag = YES;
    
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"美女1.png"]];
    imageView.frame = [UIScreen mainScreen].bounds;
    
    //contentMode内容模式
    //UIViewContentModeScaleToFill规模填充
    //UIViewContentModeScaleAspectFit内容符合固定方面。剩余部分是透明的
    //UIViewContentModeScaleAspectFill内容扩展与固定方面填补。可能是剪的一部分内容。
    //UIViewContentModeRedraw重划边界变化(调用-setNeedsDisplay)
    //UIViewContentModeCenter内容保持相同大小。定位调整。
    imageView.contentMode = UIViewContentModeScaleToFill;
    
    
    //UIIamgeView 和 UILabel一样,默认的交互是关闭的NO
    //所以用userInteractionEnabled = YES
    imageView.userInteractionEnabled = YES;
    [self.view addSubview:imageView];
    [imageView release];
    
    
    
    //UISegmentedControl,分段控制器,继承于UIControl
    //参数:参数类型,数组内存放每个分段控制按钮的标题
    NSArray *items = @[@"点击", @"捏合", @"旋转", @"清扫", @"平移", @"边缘移动", @"长按"];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
    segmentedControl.frame = CGRectMake(0, 20, self.view.frame.size.width, 40);
    
    //关联方法
    //分段控制器,触发事件是UIControlEventValueChanged//和button的不一样UIControEventTouchUpInside
    [segmentedControl addTarget:self action:@selector(changeGesture:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentedControl];
    [segmentedControl release];
    
    
    
    // Do any additional setup after loading the view.
}

- (void)changeGesture:(UISegmentedControl *)aControl
{
    //选中的是第几个区域
    //selectedSegmentIndex, 从0开始依次增加
    NSLog(@"%ld", aControl.selectedSegmentIndex);
    
    //抽象基类的特点:不能够直接使用,必须通过子类化的形式才能使用,换言之,只能使用子类
    //UIGestureRecognizer,手势识别器,其实是对触摸的封装,是抽象基类
    
    //移除之前的手势
    for (UIGestureRecognizer *gesture in imageView.gestureRecognizers) {
        [imageView removeGestureRecognizer:gesture];
    }
    
    
    switch (aControl.selectedSegmentIndex) {
        case 0://单击
        {
            //单击手势
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesture)];
            //单击次数
            tap.numberOfTapsRequired = 2;
            //需要几个手指同时点击
            tap.numberOfTouchesRequired = 1;
            
            //把手势添加到某个视图上
            [imageView addGestureRecognizer:tap];
            //释放
            [tap release];
            
        
        
        }
            break;
        
        case 1://捏合
        {
            //捏合手势
            UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
            [imageView addGestureRecognizer:pinch];
            [pinch release];
        }
            break;
            
        case 2://旋转
        {   //旋转手势
            UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
            [imageView addGestureRecognizer:rotation];
            [rotation release];
        }
            break;
            
        case 3://清扫
            
        {//清扫手势
//            imageView.image = [UIImage imageNamed:@"美女1.png"];
//            imageView.contentMode = UIViewContentModeScaleToFill;
            
            //向左滑动
            UISwipeGestureRecognizer *leftWipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swape:)];
            //设置初始值1
            number = 1;
            //设置方向, 左右(只支持一个方向)
            leftWipe.direction = UISwipeGestureRecognizerDirectionLeft;
            [imageView addGestureRecognizer:leftWipe];
            [leftWipe release];
            
            
            //向右滑动
            UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swape:)];
            //设置初始值1
            number = 1;
            //设置方向, 左右(只支持一个方向)
            rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
            [imageView addGestureRecognizer:rightSwipe];
            [rightSwipe release];
         }
            break;
            
        case 4://平移
        {
            //平移手势
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
            [imageView addGestureRecognizer:pan];
            [pan release];
        }
            break;
            
        case 5://边缘移动//扁平化之后才有的
        {
            //注意:边缘移动手势触摸的机制,必须保证视图紧贴屏幕的边缘
            UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePan)];
            //边缘(支持一个方向)
            edgePan.edges = UIRectEdgeLeft;
            edgePan.edges = UIRectEdgeRight;
            
            [imageView addGestureRecognizer:edgePan];
            [edgePan release];
        
        }
            break;
            
        case 6://长按
        {
            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
            //时间相应(按多长时间)
            longPress.minimumPressDuration = 1.0;
            [imageView addGestureRecognizer:longPress];
            [longPress release];
        
        }
            break;
        default:
            break;
    }

}

#pragma mark ---单击响应方法的实现
/**
 *  单击响应方法的实现
    通过点击实现两个图片的交换显示
 */
- (void)gesture
{
    //1.第一种方法
//    if (flag) {
//        imageView.image = [UIImage imageNamed:@"美女1.png"];
//        imageView.contentMode = UIViewContentModeScaleToFill;
//        flag = NO;
//    } else {
//    
//        imageView.image = [UIImage imageNamed:@"美女2.png"];
//        imageView.contentMode = UIViewContentModeScaleToFill;//图片填充
//        flag = YES;
//        
//    }

//2.第二种方法,三目运算
    imageView.image = flag ? [UIImage imageNamed:@"美女2.png"] : [UIImage imageNamed:@"美女1.png"];
    flag = !flag;

}

#pragma mark ---捏合方法的实现
- (void)pinch:(UIPinchGestureRecognizer *)aPinch
{
    //aPinch.scale缩放比率
    NSLog(@"%.2f", aPinch.scale);
    //图像的变形 transform 是UIView的属性
    
    //图像的变形
    //CGAffineTransformScale(缩放比例, X轴, Y轴)
    //参数1:原始图的transform
    //参数2:X轴上改变的比率
    //参数3:Y轴上改变的比率
    imageView.transform = CGAffineTransformScale(imageView.transform, aPinch.scale, aPinch.scale);
    //重置比率(是为了让图像的比率正常)
    aPinch.scale = 1;

}

#pragma mark ---旋转方法的实现
- (void)rotation:(UIRotationGestureRecognizer *)aRotation
{
    //旋转角度
    NSLog(@"%.2f", aRotation.rotation);
    //改变图片旋转角度
    //参数1:视图原先的transform
    //参数2:变化的角度
    imageView.transform = CGAffineTransformRotate(imageView.transform, aRotation.rotation);
    //重置角度
    aRotation.rotation = 0;

}

#pragma mark ---清扫方法的实现

- (void)swape:(UISwipeGestureRecognizer *)aSwape
{

if (aSwape.direction == UISwipeGestureRecognizerDirectionLeft) {
        number++;
        if (number == 9) {//当向左滑动9次时,第9次变为从1张图片开始
            number = 1;
        }
    }
    if (aSwape.direction == UISwipeGestureRecognizerDirectionRight) {
        number--;
        if (number == 0) {//本身是1当向右滑动1次时变为0,变为从8张图片开始
            number = 8;
        }
    }
    NSString *name = [NSString stringWithFormat:@"美女%ld.png", number];
    imageView.image = [UIImage imageNamed:name];
}

#pragma mark ---平移方法的实现
- (void)pan:(UIPanGestureRecognizer *)aPan
{
    //手指移动的位置(X轴和Y轴偏移量)
    CGPoint point = [aPan translationInView:imageView];
    NSLog(@"%@", NSStringFromCGPoint(point));
    
//    //更改center偏移量
//    //方法1.
//    CGPoint temp = imageView.center;
//    temp.x += point.x;
//    temp.y += point.y;
//    imageView.center = temp;
//    //重置point
//    [aPan setTranslation:CGPointZero inView:imageView];
//    
    //方法2
    //参数1:图片原有的transform
    //参数2:X轴位移
    //参数3:Y轴位移
    imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y);
    [aPan setTranslation:CGPointZero inView:imageView];
    
}

#pragma mark ---边缘移动方法的实现
- (void)edgePan
{
    NSLog(@"边缘");

}

#pragma mark ---长按方法的实现

- (void)longPress:(UILongPressGestureRecognizer *)aLongPress
{
    //手势状态
    //aLongPress.state
    if (aLongPress.state == UIGestureRecognizerStateBegan) {
        //屏幕截图
        //绘制一张图片,屏幕大小
        UIGraphicsBeginImageContext([UIScreen mainScreen].bounds.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        //绘画结束
        UIGraphicsEndImageContext();
        //存到相册中
        UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

//    结束触发
    //    if (aLongpress.state == UIGestureRecognizerStateEnded) {
    //        <#statements#>
    //    }
}

#pragma mark ---屏幕截图提示方法的实现

- (void)               image: (UIImage *) image
    didFinishSavingWithError: (NSError *) error
                 contextInfo: (void *) contextInfo {
    
    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"图片已保存" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alterView show];
    [alterView release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 
 

UI中各种手势的使用点击,捏合,清扫,旋转,平移,边缘移动,长按的更多相关文章

  1. iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)

    1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...

  2. iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义

    1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...

  3. UI中的七种手势

    // // GestureRecognizerViewController.m #import "GestureRecognizerViewController.h" #impor ...

  4. iOS中CollectionView由于多次点击造成错误的解决方案

    iOS中CollectionCiew由于多次点击,会给程序造成错误. 这个时候,我们可以用过手势类来进行判断和过滤. 但是,有一个快捷的解决方法,那就是给用户响应增加延时操作. 具体代码如下: [co ...

  5. [转] 「指尖上的魔法」 - 谈谈 React Native 中的手势

    http://gold.xitu.io/entry/55fa202960b28497519db23f React-Native是一款由Facebook开发并开源的框架,主要卖点是使用JavaScrip ...

  6. Android中的手势

    Android对两种手势行为提供了支持:1.对于第一种手势行为而言,Android提供了手势检测,并为手势检测提供了相应的监听器.2.对于第二种手势行为,Android允许开发者添加手势,并提供了相应 ...

  7. jquery ui中 accordion的问题及我的解决方法

    原文:jquery ui中 accordion的问题及我的解决方法 jquery有一套所谓的ui组件,很不错的.如果有兴趣的朋友,可以参考http://jqueryui.com/ 但其中的accord ...

  8. 关于Element UI中页面样式小问题

    一,修改组件dialog窗口的大小 二,在我使用upload组件上传一张美女图片时,发现当预览图片时,图片是灰色的,点击一下才会变亮,这种效果使我很不舒服,于是我通过添加下面的一条样式,问题解决了(可 ...

  9. jquery ui中的dialog,官网上经典的例子

    jquery ui中的dialog,官网上经典的例子   jquery ui中dialog和easy ui中的dialog很像,但是最近用到的时候全然没有印象,一段时间不用就忘记了,这篇随笔介绍一下这 ...

随机推荐

  1. C语言学习笔记(四) 流程控制

    流程控制 流程控制,说通俗一点就是程序代码执行的顺序.不管对于哪门语言来说,流程控制都是很重要的一部分内容: 流程控制的分类,可以分为三大类: 1.顺序 这个很好理解,顺序执行就是代码从上往下一行行的 ...

  2. Error: [vuex] vuex requires a Promise polyfill in this browser. 与 babel-polyfill 的问题

    Error: [vuex] vuex requires a Promise polyfill in this browser. 与 babel-polyfill 的问题 采用最笨重的解决方案就是npm ...

  3. Netty(一):初识Netty

    Netty是什么? Netty是由JBOSS提供的一个java开源框架. Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. 封装了JDK底 ...

  4. Atitit.js this错误指向window的解决方案

    Atitit.js this错误指向window的解决方案 1.1. 出现地点and解决之道1 1.2. call,apply和bind这三个方法2 1.2.1. Function.prototype ...

  5. ModelSim高级使用进阶_1_do文件和批处理文件使用_Camp

    https://wenku.baidu.com/view/50fb251914791711cc7917fd.html https://wenku.baidu.com/view/73187dcefe47 ...

  6. 【转载】表单验证<AngularJs>

    原文地址:http://www.cnblogs.com/rohelm/archive/2014/10/19/4033513.html 常用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,只要 ...

  7. 【算法拾遗(java描写叙述)】--- 插入排序(直接插入排序、希尔排序)

    插入排序基本思想 每次将一个待排序的记录按其keyword大小插入到前面已经拍好序的子文件的适当位置,直到全部记录插入完毕为止. 直接插入排序 基本思想 直接插入排序的基本操作是将一个记录插入到已排好 ...

  8. cf #363 b

    B. One Bomb time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  9. jquery中绑定点击事件

    $("body").on("click",".tab-contentBox td",function(){}; $(".tab-c ...

  10. linux跨主机复制文件

    scp -r billing@10.200.171.111:/billdata2/user/yanhm/redis/* /newboss/billing/user/aabb 其中: 10.200.17 ...