UIImageView_手势

//
//  MainViewController.m
//  UI04_UIImageView_手势
//
//  Created by 王继魁 on 15/11/30.
//  Copyright © 2015年 jlnu_cst. All rights reserved.
//


#import "MainViewController.h"


typedef NS_ENUM(NSInteger, SegIndex) {
    SegIndexTap,    // 点击
    SegIndexPan,    // 拖拽
    SegIndexSwipe,  // 轻扫
    SegIndexLongPress,  // 长按
    SegIndexPinch,      // 捏合
    SegIndexRotate,     // 旋转
    SegIndexScreenEdge  // 屏幕边缘
};




@interface MainViewController ()


@end






@implementation MainViewController


- (void)dealloc
{
    [_imageView release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self createView];
    [self createSegment];
}


- (void)createView
{
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, self.view.frame.size.height / 3)];
    // 1. 加载图片的第一种方法 +imageNamed:@"" 存入程序缓存
//    imageView.image = [UIImage imageNamed:@"11.png"];
    
    // 2. 加载图片的第二种方法 imageWithContentsOfFile (推荐使用)
    //a. 文件 绝对路径
//    NSString *path = @"/Users/jlnucst/Desktop/UI课程/UI04_UIImageView_手势/UI04_UIImageView_手势/11@2x.png";
    //b. 文件 相对路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"11@3x" ofType:@"png"];
    _imageView.image = [UIImage imageWithContentsOfFile:path];
    //UIImageView 的 userInteractionEnabled 默认是 NO, 不响应用户点击
    _imageView.userInteractionEnabled = YES;
    [self.view addSubview:_imageView];
    [_imageView release];
    
}


#pragma mark 知识点2 手势


- (void)createSegment
{
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[ @"点击", @"拖拽", @"轻扫", @"长按", @"捏合", @"旋转", @"边缘"]];
    [segment setFrame:CGRectMake(20, 300, 300, 40)];
    [self.view addSubview:segment];
    [segment release];
    
//    segment.selectedSegmentIndex = 0;
    [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
}


- (void)segmentAction:(UISegmentedControl *)segment
{
    NSLog(@"segment index = %ld", segment.selectedSegmentIndex);
    // 删除原有手势
    // 1. 获得原来的手势
    NSArray *array = self.imageView.gestureRecognizers;
    // 2. 移除一个一个的手势
    for (UIGestureRecognizer *ges in array) {
        [self.imageView removeGestureRecognizer:ges];
    }
    
    switch (segment.selectedSegmentIndex) {
        case SegIndexTap: // 点击
        {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            tap.numberOfTapsRequired = 1;
            tap.numberOfTouchesRequired = 1;
            [self.imageView addGestureRecognizer:tap];
            [tap release];
            break;
        }
        case SegIndexPan: // 拖拽
        {
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
            [self.imageView addGestureRecognizer:pan];
            [pan release];
        }
        case SegIndexSwipe: // 轻扫
        {
            UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
            [self.imageView addGestureRecognizer:swipe];
            swipe.direction = UISwipeGestureRecognizerDirectionDown;
            [swipe release];
        }
        case SegIndexLongPress: // 长按
        {
            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
            [self.imageView addGestureRecognizer:longPress];
            longPress.minimumPressDuration = 1;
            [longPress release];
        }
        case SegIndexPinch: // 捏合
        {
            UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
            [self.imageView addGestureRecognizer:pinch];
            [pinch release];
        }
        case SegIndexRotate: // 旋转
        {
            UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];
            [self.imageView addGestureRecognizer:rotate];
            [rotate release];
        }
        case SegIndexScreenEdge: // 屏幕边缘
        {
            UIScreenEdgePanGestureRecognizer *edge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgeAction:)];
            [self.view addGestureRecognizer:edge];
            edge.edges = UIRectEdgeLeft;
            [edge release];
        }
        default:
            break;
    }
}


- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"点击");
}


- (void)panAction:(UIPanGestureRecognizer *)pan
{
    NSLog(@"拖拽");
    CGPoint point = [pan translationInView:self.imageView];
    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);
    [pan setTranslation:CGPointZero inView:self.imageView];
}


- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"轻扫");
}


- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始长按");
    }
}


- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    NSLog(@"捏合");
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
    pinch.scale = 1.0f;
}


- (void)rotateAction:(UIRotationGestureRecognizer *)rotate
{
    NSLog(@"旋转");
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotate.rotation);
    rotate.rotation = 0;
}


- (void)edgeAction:(UIScreenEdgePanGestureRecognizer *)edge
{
    NSLog(@"屏幕边缘");
}


@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值