博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
手势 - 单击、长按、拖动等
阅读量:4987 次
发布时间:2019-06-12

本文共 2794 字,大约阅读时间需要 9 分钟。

@implementation ViewController

 

UILabel *labelView;

 

int startX;

 

int startY;

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    startX = 100;

    startY = 100;

    

    labelView = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

    labelView.backgroundColor = [UIColor redColor];

    labelView.text = @"click text";

    labelView.textColor = [UIColor whiteColor];

    labelView.font = [UIFont systemFontOfSize:15];

    labelView.contentMode = UIViewContentModeCenter;

    labelView.textAlignment = kCTTextAlignmentCenter;

    labelView.userInteractionEnabled = YES;

    // 单击

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleClick:)];

    [labelView addGestureRecognizer:tapGesture];

    // 滑动事件

    // 左右滑屏切题

    UISwipeGestureRecognizer *leftSwapGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureHandle:)];

    [leftSwapGesture setDirection:UISwipeGestureRecognizerDirectionLeft];

    UISwipeGestureRecognizer *rightSwapGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureHandle:)];

    [rightSwapGesture setDirection:UISwipeGestureRecognizerDirectionRight];

    [self.view addGestureRecognizer:leftSwapGesture];

    [self.view addGestureRecognizer:rightSwapGesture];

    // 拖动

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)];

    [labelView addGestureRecognizer:panGesture];

    // 长按

    UILongPressGestureRecognizer *longPressGuesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

    [labelView addGestureRecognizer:longPressGuesture];

    [self.view addSubview:labelView];

}

-(void)singleClick:(UITapGestureRecognizer *)gesture

{

    NSLog(@"CLICK");

}

 

-(void)longPress:(UILongPressGestureRecognizer *)gesture

{

    NSLog(@"LONG CLICK");

}

 

-(void)swipeGestureHandle:(UISwipeGestureRecognizer *)gesture

{

    NSInteger direction = gesture.direction;

    NSLog(@"direction is: %i", direction);

    switch (direction) {

        case UISwipeGestureRecognizerDirectionRight:

            NSLog(@"TO LEFT");

            CGRect frame = CGRectMake(100, 100, labelView.frame.size.width, labelView.frame.size.height);

            labelView.frame = frame;            break;

        case UISwipeGestureRecognizerDirectionLeft:

            NSLog(@"TO RIGHT");

            break;

        default:

            break;

    }

}

 

-(void)panGestureRecognizer:(UIPanGestureRecognizer *)gesture

{

    // 位移

    CGPoint point = [gesture translationInView:labelView];

    NSLog(@"point : x = %g, y=%g",point.x, point.y);

    CGRect frame = CGRectMake(startX + point.x, startY + point.y, labelView.frame.size.width, labelView.frame.size.height);

    labelView.frame = frame;

    NSLog(@"gusture is : %i", gesture.state);

    if(gesture.state == UIGestureRecognizerStateEnded)

    {

        startX = labelView.frame.origin.x;

        startY = labelView.frame.origin.y;

    }

}

 

转载于:https://www.cnblogs.com/xiangjune/p/4950481.html

你可能感兴趣的文章
本地jar上传到本地仓库
查看>>
四则运算C++带Qt界面版本,吾王镇楼。。。。。
查看>>
安卓7.0手机拍照闪退问题解决
查看>>
ME525+ Defy+ 刷机指南[zz]
查看>>
支持触屏的jQuery轮播图插件
查看>>
差一点搞混了Transactional注解
查看>>
javascript基本函数
查看>>
前端公共库cdn服务推荐//提高加载速度/节省流量
查看>>
snprintf 返回值陷阱 重新封装
查看>>
asp.net GridView多行表头的实现,合并表头
查看>>
C#套打
查看>>
PolyCluster: Minimum Fragment Disagreement Clustering for Polyploid Phasing 多聚类:用于多倍体的最小碎片不一致聚类...
查看>>
【每日进步】July 2012
查看>>
327 作业
查看>>
sql 取汉字首字母
查看>>
bzoj4034: [HAOI2015]树上操作(树剖)
查看>>
${sessionScope.user}的使用方法
查看>>
WCF开发框架形成之旅---结合代码生成工具实现快速开发
查看>>
Spring事务管理
查看>>
linux下mysql配置文件my.cnf详解
查看>>