//

//  GestureViewController.m

//  LesssonGesture

//

//  Created by lanouhn on 15/3/24.

//  Copyright (c) 2015年 lanouhn. All rights reserved.

//

 

#import "GestureViewController.h"

 

@interface GestureViewController () {

    UIImageView *imageView;

    BOOL flag;//用于标记当前是哪张图片

    NSInteger number;

}

 

@end

 

@implementation GestureViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor yellowColor];

    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"哭"]];

    //默认值是YES;

    flag = YES;

    imageView.frame = [UIScreen mainScreen].bounds;

//    imageView.frame = 

    //UIImageView和UILabel一样, 默认的交互是关闭的

    imageView.userInteractionEnabled = YES;

    imageView.contentMode = UIViewContentModeScaleAspectFill;

    [self.view addSubview:imageView];

    [imageView release];

    

    //抽象基类的特点: 不能够直接使用, 必须通过子类化的形式才能使用, 换而言之, 只能使用其子类

    //UIGestureRecognizer, 手势识别器, 其实是对触摸的封装, 是抽象基类

    /*

    //点击手势

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:(self) action:@selector(print)];

    //点击次数

    tap.numberOfTapsRequired = 2;

    //需要几个手指,同时点击

    tap.numberOfTouchesRequired = 2;

    //把手势添加到某个视图上

    [imageView addGestureRecognizer:tap];

    //释放

    [tap release];

    

    //捏合

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];

    [imageView addGestureRecognizer:pinch];

    [pinch release];

    

    //手势集合

    //imageView.gestureRecognizers

//    for (UIGestureRecognizer *gesture in imageView.gestureRecognizers) {

//        [imageView removeGestureRecognizer:gesture];

//    }

    

    //旋转

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];

    [imageView addGestureRecognizer:rotation];

    [rotation release];

    

    //去掉手势

//    [imageView removeGestureRecognizer:tap];

//    [imageView removeGestureRecognizer:pinch];

    */

    //UISegmentedControl, 分段控制器, 继承于UIControl

    //参数: 数组类型, 数组内存放每个分段控制按钮的标题

    NSArray *items = @[@"点击", @"捏合", @"旋转", @"清扫", @"平移", @"边缘移动", @"长按"];

    UISegmentedControl * segmentedControl = [[UISegmentedControl alloc] initWithItems:items];

    segmentedControl.frame = CGRectMake(0, 20, self.view.frame.size.width, 40);

    

    //关联方法

    [segmentedControl addTarget:self action:@selector(changeGesture:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:segmentedControl];

    [segmentedControl release];

}

                                       

 

 

- (void)changeGesture:(UISegmentedControl *)aControl {

    //选中的时第几个区域

    //selectedSegmentIndex, 从0开始

    NSLog(@"%ld", aControl.selectedSegmentIndex);

    //移除之前的手势

    for (UIGestureRecognizer *gesture in imageView.gestureRecognizers) {

        [imageView removeGestureRecognizer:gesture];

        

    }

    //添加手势

    switch (aControl.selectedSegmentIndex) {

        case 0://单击

        {

            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:(self) action:@selector(print)];

            //点击次数

            tap.numberOfTapsRequired = 1;

            //需要几个手指,同时点击

            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"];

 

            //填充模式

            imageView.contentMode = UIViewContentModeScaleAspectFit;

            //初始值为1

            number = 1;

            

            UISwipeGestureRecognizer *rightswipe =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swape:)];

            //设置方向, 只支持一个方向

            rightswipe.direction = UISwipeGestureRecognizerDirectionRight;

        

        [imageView addGestureRecognizer:rightswipe];

            [rightswipe release];

            

            UISwipeGestureRecognizer *leftswipe =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swape:)];

            //设置方向, 只支持一个方向

            leftswipe.direction = UISwipeGestureRecognizerDirectionLeft;

            

            [imageView addGestureRecognizer:leftswipe];

            [leftswipe 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;

            [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;

    }

}

 

- (void)longPress:(UILongPressGestureRecognizer *)aLongPress {

    NSLog(@"%s", __FUNCTION__);

    //手势状态

    //aLongPress.state

    if (aLongPress.state == UIGestureRecognizerStateBegan) {

        NSLog(@"开始");

        //屏幕截图

        UIGraphicsBeginImageContext([UIScreen mainScreen].bounds.size);

        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    }

    

}

 

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

    UIAlertView *alView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"图片已保存" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];

    [alView show];

    [alView release];

}

 

- (void)edgePan {

    NSLog(@"%s", __FUNCTION__);

}

 

- (void)pan:(UIPanGestureRecognizer *)aPan {

    //手指移动的位置(x轴和y周的偏移量)

    CGPoint point = [aPan translationInView:imageView];

    NSLog(@"%@", NSStringFromCGPoint(point));

//    CGPoint temp = imageView.center;

//    point = temp;

    

    //平移

    //参数1:图片原有detransform

    //参数2:x轴位移

    //参数3:y轴位移

    imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y);

    //重置point

    [aPan setTranslation:CGPointZero inView:imageView];

    

}

 

 

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

    NSLog(@"%s", __FUNCTION__);

    if (aSwape.direction == UISwipeGestureRecognizerDirectionLeft) {

        number++;

        if (number == 15) {

            number = 1;

        }

        

    }

    if (aSwape.direction == UISwipeGestureRecognizerDirectionRight) {

        number--;

        if (number == 0) {

            number = 14;

        }

 

    }

    NSString *name = [NSString stringWithFormat:@"%ld",number];

    imageView.image = [UIImage imageNamed:name];

}

 

- (void)print {

//    //1.

//    if (flag) {

//        imageView.image = [UIImage imageNamed:@"哭"];

//        flag = NO;

//    } else {

//        imageView.image = [UIImage imageNamed:@"笑"];

//        flag = YES;

//    }

    //2.

    imageView.image = flag ? [UIImage imageNamed:@"哭"] :[UIImage imageNamed:@"笑"];

    flag = !flag;

    

}

 

- (void)rotation:(UIRotationGestureRecognizer *)aRotation {

    NSLog(@"%.2lf", aRotation.rotation);

    //改变图片旋转角度

    //参数1, 改变原先的transform

    //参数2, 变化的角度

    imageView.transform = CGAffineTransformRotate(imageView.transform, aRotation.rotation);

    //重置角度

    aRotation.rotation = 0;

}

 

- (void)pinch:(UIPinchGestureRecognizer *)aPinch {

    //缩放比率

    NSLog(@"%.2lf", aPinch.scale);

    //transfrom是UIView的属性

    //图片的变形

    //参数1 :原先视图的trandform

    //参数2 :x轴改变的比率

    //参数3 :y轴改变的比率

    imageView.transform = CGAffineTransformScale(imageView.transform, aPinch.scale, aPinch.scale);

    //重置比率

    aPinch.scale = 1;

    

   

    

    

}

 

 

- (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

 

posted on 2015-03-24 22:44  小雪童鞋  阅读(194)  评论(0编辑  收藏  举报