iOS 手势

触摸

#import "ViewController1.h"

@interface ViewController1 ()
//开始坐标
@property(assign)CGPoint startPoint;

@end

@implementation ViewController1

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor magentaColor];
    UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(80, 320, 80, 40);
    btn.backgroundColor=[UIColor cyanColor];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
     [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    for (int i=0; i<3; i++) {
        UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(80, 50+70*i, 150, 50)];
        label.backgroundColor=[UIColor blueColor];
        label.textAlignment=NSTextAlignmentCenter;
        label.tag=10+i;
        [self.view addSubview:label];
    }
    //手指视图可以接受多线触摸,默认是不接受的
    self.view.multipleTouchEnabled=YES;
}
-(void)btnAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//以下的方法是UIResponder的方法,不管是视图还是视图控制器都可以实现这一组方法,因为他们都是继承自UIResponder这个类
//开始触摸会调用此方法
//改变第一个文本的内容为触摸开始
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UILabel * laber1=(UILabel *)[self.view viewWithTag:10];
    laber1.text=@"触摸开始";

    //取出集合中的某个手势(anyObject)集合中的任何对象
    UITouch * tonch=[touches anyObject];
    //获取开始的坐标  获取手势所在位置
    _startPoint=[tonch locationInView:self.view];
    //获取点击次数
    NSInteger tapCount=tonch.tapCount;
    //获取手指数量
    NSInteger fingerCount=touches.count;

    UILabel * label2=(UILabel *)[self.view viewWithTag:11];
    label2.text=[NSString stringWithFormat:@"%ld个手指点击了%ld次",(long)fingerCount,(long)tapCount];
}
//触摸手势在移动调用此方法
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UILabel * label1=(UILabel *)[self.view viewWithTag:10];
    label1.text=@"触摸移动";
}
//触摸结束调用此方法
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UILabel * label1=(UILabel *)[self.view viewWithTag:10];
    label1.text=@"触摸结束";
    UITouch * touch=[touches anyObject];
    CGPoint endPoint=[touch locationInView:self.view];
    //把CGPoint转换成字符串
    NSLog(@"%@   %@",NSStringFromCGPoint(_startPoint),NSStringFromCGPoint(endPoint));
    //fabsf   求绝对值
    CGFloat distanceX=fabs(endPoint.x-_startPoint.x);
    CGFloat distancey=fabs(endPoint.y-_startPoint.y);
    if (distanceX>20&&distancey<=10) {
         UILabel * label3=(UILabel *)[self.view viewWithTag:12];
        label3.text=@"横扫千军";
    }
}
//触摸手势被意外终止调用此方法,比如来了系统电话
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UILabel * label1=(UILabel *)[self.view viewWithTag:10];
    label1.text=@"触摸取消";
}
- (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

一开始的图片
触摸后的图片

轻扫

#import "ViewController2.h"

@interface ViewController2 ()
@property(retain)UISwipeGestureRecognizer * swipe;
@property(retain)UISwipeGestureRecognizer * swipe2;
@property(retain)UISwipeGestureRecognizer * swipe3;
@property(retain)UISwipeGestureRecognizer * swipe4;

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor purpleColor];
    UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(80, 80, 200, 40)];
    label.backgroundColor=[UIColor cyanColor];
    label.tag=10;
    [self.view addSubview:label];
    UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(80, 320, 80, 40);
    btn.backgroundColor=[UIColor cyanColor];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

    //滑动手势,确定滑动方向(上左下右)
    _swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    设置滑动的方向,此处为向右,共有4个方向
    [_swipe setDirection:UISwipeGestureRecognizerDirectionRight];
    将手势添加到当前视图中
    [self.view addGestureRecognizer:_swipe];
    //滑动手势,确定滑动方向(上左下右)
    _swipe2=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    设置滑动的方向,此处为向左,共有4个方向
    [_swipe2 setDirection:UISwipeGestureRecognizerDirectionLeft];
    将手势添加到当前视图中
    [self.view addGestureRecognizer:_swipe2];
    //滑动手势,确定滑动方向(上左下右)
    _swipe3=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    设置滑动的方向,此处为向上,共有4个方向
    [_swipe3 setDirection:UISwipeGestureRecognizerDirectionUp];
    将手势添加到当前视图中
    [self.view addGestureRecognizer:_swipe3];
    //滑动手势,确定滑动方向(上左下右)
    _swipe4=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    设置滑动的方向,此处为向下,共有4个方向
    [_swipe4 setDirection:UISwipeGestureRecognizerDirectionDown];
    将手势添加到当前视图中
    [self.view addGestureRecognizer:_swipe4];
}
-(void)btnAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    UILabel * label=(UILabel *)[self.view viewWithTag:10];
    if (swipe==_swipe) {
        label.text=@"右扫0";
    }
    if (swipe==_swipe2) {
        label.text=@"左扫0";
    }
    if (swipe==_swipe3) {
        label.text=@"上扫0";
    }
    if (swipe==_swipe4) {
        label.text=@"下扫0";
    }

}
//-(void)swipeAction2:(UISwipeGestureRecognizer *)swipe
//{
//    UILabel * label=(UILabel *)[self.view viewWithTag:10];
//    label.text=@"左扫0";
//}
//-(void)swipeAction3:(UISwipeGestureRecognizer *)swipe
//{
//    UILabel * label=(UILabel *)[self.view viewWithTag:10];
//    label.text=@"上扫0";
//}
//-(void)swipeAction4:(UISwipeGestureRecognizer *)swipe
//{
//    UILabel * label=(UILabel *)[self.view viewWithTag:10];
//    label.text=@"下扫0";
//}
- (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

向左边轻扫
向右边轻扫

点击

#import "ViewController3.h"

@interface ViewController3 ()
@property(retain)UILabel * laber;
@property(retain)UITapGestureRecognizer * tap;
@property(retain)UITapGestureRecognizer * tap2;
@property(retain)UITapGestureRecognizer * tap3;

@end

@implementation ViewController3

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor blueColor];
    UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(80, 80, 200, 40)];
    label.backgroundColor=[UIColor cyanColor];
    label.tag=10;
    [self.view addSubview:label];
    UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(80, 320, 80, 40);
    btn.backgroundColor=[UIColor cyanColor];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

    _tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    //设置手指个数
    _tap.numberOfTouchesRequired=1;
    ///设置点击次数
    _tap.numberOfTapsRequired=1;
    [self.view addGestureRecognizer:_tap];
    _tap2=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
   //设置手指个数
    _tap2.numberOfTouchesRequired=2;
    //设置点击次数
    _tap2.numberOfTapsRequired=2;
    [self.view addGestureRecognizer:_tap2];
    _tap3=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
     //设置手指个数
    _tap3.numberOfTouchesRequired=1;
    //设置点击次数
    _tap3.numberOfTapsRequired=3;
    [self.view addGestureRecognizer:_tap3];
    //设置优先级,当三机事件失败就响应双机事件
    [_tap2 requireGestureRecognizerToFail:_tap3];

}
-(void)btnAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)tapAction:(UITapGestureRecognizer *)sender{
    _laber=(UILabel *)[self.view viewWithTag:10];
    if (sender==_tap) {
        _laber.text=@"单机事件";
    }
    if (sender==_tap2) {
        _laber.text=@"双机事件";
    }
    if (sender==_tap3) {
        _laber.text=@"三机事件";
    }
}
- (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

单机事件
三击事件

长按

#import "ViewController4.h"

@interface ViewController4 ()

@end

@implementation ViewController4

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor yellowColor];
    UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(80, 80, 200, 40)];
    label.backgroundColor=[UIColor cyanColor];
    label.tag=10;
    [self.view addSubview:label];
    UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(80, 320, 80, 40);
    btn.backgroundColor=[UIColor cyanColor];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    //长按手势
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    [self.view addGestureRecognizer:longPress];
      //设置长按的时间,2秒以上判定为长按
    longPress.minimumPressDuration=1;
    //设置长按手指
    longPress.numberOfTouchesRequired=2;

}
-(void)btnAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)longPressAction:(UILongPressGestureRecognizer *)sender
{
    UILabel * label=(UILabel *)[self.view viewWithTag:10];
    label.text=@"长按手势";
}

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

长按

缩放

#import "ViewController5.h"

@interface ViewController5 ()

@end

@implementation ViewController5

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor grayColor];
    UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(40, 80, 250, 250)];
    label.backgroundColor=[UIColor cyanColor];
    label.tag=10;
    label.userInteractionEnabled=YES;
    [self.view addSubview:label];
    UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(80, 400, 80, 40);
    btn.backgroundColor=[UIColor cyanColor];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
     [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    //双指缩放
    UIPinchGestureRecognizer * pin=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinAction:)];
    [label addGestureRecognizer:pin];
}
-(void)btnAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)pinAction:(UIPinchGestureRecognizer *)pin
{
    获取缩放比例 pinch.scale
    NSLog(@"%f",pin.scale);
    //
    UILabel * label=(UILabel *)[self.view viewWithTag:10];
    label.transform=CGAffineTransformMakeScale(pin.scale, pin.scale);
}
- (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

放大
缩小

旋转

#import "ViewController6.h"

@interface ViewController6 ()
@property(retain)NSTimer * timer;

@end

@implementation ViewController6

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor grayColor];
    UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(40, 80, 250, 250)];
    label.backgroundColor=[UIColor cyanColor];
    label.tag=10;
    label.userInteractionEnabled=YES;
    [self.view addSubview:label];
    UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(80, 400, 80, 40);
    btn.backgroundColor=[UIColor cyanColor];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
     [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    //创建旋转手势对象
    UIRotationGestureRecognizer * rot=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotAction:)];
    [label addGestureRecognizer:rot];
    //自动旋转
    _timer=[[NSTimer alloc]init];
    _timer=[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(changeFrame:)userInfo:nil repeats:YES];
}
-(void)btnAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)rotAction:(UIRotationGestureRecognizer *)rot
{
    NSLog(@"%f",rot.rotation);
    UILabel * label=(UILabel *)[self.view viewWithTag:10];
    确定手势旋转的大小,角度,使视图做出相应的反应
     label.transform = CGAffineTransformMakeRotation(rot.rotation);
    //暂停自动旋转
     self.timer.fireDate = [NSDate distantFuture];
}
-(void)changeFrame:(NSTimer *)timer
{
    UILabel * label=(UILabel *)[self.view viewWithTag:10];
    确定手势旋转的大小,角度,使视图做出相应的反应
    label.transform=CGAffineTransformRotate(label.transform, 180/M_PI);
    //label.transform = CGAffineTransformMakeRotation(timer.rotation);
}
- (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

旋转

拖拽

#import "ViewController7.h"

@interface ViewController7 ()

@end

@implementation ViewController7

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor grayColor];
    UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(40, 80, 250, 250)];
    label.backgroundColor=[UIColor cyanColor];
    label.tag=10;
    label.userInteractionEnabled=YES;
    [self.view addSubview:label];
    UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(80, 400, 80, 40);
    btn.backgroundColor=[UIColor cyanColor];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    //创建拖动手势对象
    UIPanGestureRecognizer * pPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];

    //将手势添加到当前视图中

    [self.view addGestureRecognizer:pPan];
}
-(void)btnAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)panGesture:(UIPanGestureRecognizer *)pan
{
    UILabel * label=(UILabel *)[self.view viewWithTag:10];
    //获取拖拽手势的最终位置
    CGPoint screenPoint = [pan locationInView:self.view];

//    CGPoint selfPoint = [pan locationInView:pan.view];
//    label.center = screenPoint;
//    label.center=selfPoint;
//    label.center=CGPointMake(screenPoint.x, selfPoint.y);
    //改变视图
    label.center=CGPointMake(label.center.x+screenPoint.x, label.center.y+screenPoint.y);
    //把这次改变值置为0,下次从当前位置重新移动,不累加上次的变化值
    [pan setTranslation:CGPointZero inView:self.view];
}
- (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

没拖之前
拖拽之后

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值