UI2_UIGesture

//
//  ViewController.h
//  UI2_UIGesture
//
//  Created by zhangxueming on 15/7/9.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>


@end

 

//
//  ViewController.m
//  UI2_UIGesture
//
//  Created by zhangxueming on 15/7/9.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *names = @[@"blue",@"red",@"yellow"];
    for (int i=0; i<3; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100*i, 180+100*i, 100, 100)];
        imageView.image = [UIImage imageNamed:names[i]];
        [self.view addSubview:imageView];
        //打开用户交互使能
        imageView.userInteractionEnabled = YES;
        
        //添加点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        //设置点击次数
        tap.numberOfTapsRequired = 1;
        //设置手指个数
        tap.numberOfTouchesRequired = 2;
        //给imageView 添加手势
        [imageView addGestureRecognizer:tap];
        
        //添加长按手势
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
        longPress.numberOfTapsRequired = 0;
        //longPress.numberOfTouchesRequired = 2;
        [imageView addGestureRecognizer:longPress];
        
        //添加移动手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer  alloc] initWithTarget:self action:@selector(panGesture:)];
        [imageView addGestureRecognizer:pan];
        
        //捏合手势
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
        [imageView addGestureRecognizer:pinch];
        //旋转手势
        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
        [imageView addGestureRecognizer:rotation];
        //轻扫手势
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
        [imageView addGestureRecognizer:swipe];
    }
}

- (void)tapGesture:(UITapGestureRecognizer *)tap
{
    NSLog(@"图片被点击");
}

- (void)longPressGesture:(UILongPressGestureRecognizer *)longPress
{
    NSLog(@"长按手势被触发");
}

- (void)panGesture:(UIPanGestureRecognizer *)pan
{
    NSLog(@"移动手势被触发");//5 10 15   5 5 5
    UIView *view = pan.view;//5+5+5
    //获取手势的偏移量
    CGPoint px = [pan translationInView:self.view];
    if(pan.state == UIGestureRecognizerStateBegan || pan.state == UIGestureRecognizerStateChanged)
    {
    //改变手势对应的view中心点坐标
        pan.view.center = CGPointMake(view.center.x+px.x, view.center.y+px.y);
    }
    //设置偏移量为0;
    [pan setTranslation:CGPointZero inView:self.view];
}

- (void)pinchGesture:(UIPinchGestureRecognizer *)pinch
{
    NSLog(@"捏合手势被触发");
    if(pinch.scale == UIGestureRecognizerStateBegan || pinch.scale ==UIGestureRecognizerStateChanged)
    {
        pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    }
    //设置系数为1
    pinch.scale = 1.0;
}

- (void)rotationGesture:(UIRotationGestureRecognizer *)rotation
{
    NSLog(@"旋转手势被触发");
    if (rotation.state == UIGestureRecognizerStateBegan || rotation.state == UIGestureRecognizerStateChanged) {
        rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    }
    
    rotation.rotation = 0.0;
}

- (void)swipeGesture:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"轻扫手势被触发");
}

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

@end

 

转载于:https://www.cnblogs.com/0515offer/p/4638914.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值