#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. 解决安装Ubuntu之后找不到无线网卡驱动的问题

    为了不浇灭大家尝试ubuntu的冲动,昨天我安装了ubuntu 14.04 LTS版本号,从安装到又一次开机都非常顺利.PS:不会安装的请找教程区把,网上非常多,CSDN论坛都有. 安装之后当你奇妙的 ...

  2. iOS开发之实现半透明蒙层背景效果[用于下拉菜单页和分享页]

    郝萌主倾心贡献.尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠.支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  3. 更改 easyUI 的皮肤样式

    我的版本是:jquery-easyui-1.3.2.根据官方提供的皮肤样式,——在theme 里面: 只需要在引入的 页面中 link样式的地址改变即可: <link rel="sty ...

  4. Sql Server分页分段查询百万级数据四种项目实例

    实际项目中需要实现自定义分页,最关键第一步就是写分页SQL语句,要求语句效率要高. 那么本文的一个查询示例是查询第100000-100050条记录,即每页50条的结果集.查询的表名为infoTab,且 ...

  5. objective-c的观察者模式

    addObserver即添加消息响应函数.postNotificationName即发消息.

  6. URL浅谈

    URL中的锚 URL中的锚就是#,语法: #foo 其中定位锚的方式有2种,id和name属性都可以定位锚. 例子: <div name='top'>top</div>或者&l ...

  7. mysql之select into outfile

    参见:http://dev.mysql.com/doc/refman/5.1/en/select-into.html 例子: SELECT a,b,a+b INTO OUTFILE '/tmp/res ...

  8. 推荐个强大的任务管理器-Process Hacker

    软件主页及下载: http://processhacker.sourceforge.net/index.php 之前一直用process explorer 功能一样强大,但是process hacke ...

  9. wp8 各种启动器

    PhoneCallTask  打电话 需要 ID_CAP_PHONEDIALER Windows Phone 8, Windows Phone OS 7.1

  10. int和Integer差别

    种原始数据类型之中的一个. Java为每一个原始类型提供了封装类.Integer是java为int提供的封装类. 原始数据类型包含byte.int.char.long.float.double.boo ...