关闭

UI手势

标签: UI手势
74人阅读 评论(0) 收藏 举报
分类:

   UI手势就是给UI增加手势,大致可分为6种:

    UITapGestureRecognizer   //Tap(点击)

    UIPinchGestureRecognizer    //pin(捏合)

    UIRotationGestureRecognizer   //rotation(旋转)

    UISwipeGestureRecognizer  //swipe(滑动,快速的移动,用于监测滑动的方向)

    UIPanGestureRecognizer   //pan(拖动,慢速移动,用于监测偏移的量)

    UILongPressGestureRecognizer   //longPress(长按)

    //可以把它看作是一块操作的区域

    UIView *tapView = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 300, 500)];

    tapView.backgroundColor = [UIColor redColor];

    [self.view addSubview:tapView];

    //单击,必须写冒号

    UITapGestureRecognizer *singTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];

    //点击的次数

    singTap.numberOfTapsRequired = 1;//单击

    singTap.numberOfTouchesRequired = 1;//手指个数

    [tapView addGestureRecognizer:singTap];

    //双击

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

    doubleTap.numberOfTapsRequired = 2;

    [tapView addGestureRecognizer:doubleTap];

      //手势冲突

    //这句代码非常关键,双击手势确定监测失败才会触发单击手势的操作(意思就是说:单击手势和双击手势同时存在优先执行双击手势)

    [singTap requireGestureRecognizerToFail:doubleTap];

    //捏合

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

    //一般用比例,不用速度

    [tapView addGestureRecognizer:pinch];

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

    

    NSLog(@"捏合:%f",pinch.scale);

    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);

//由于缩放的时候会在上次的基础上缩放,所以要设置在原来的基础上缩放

    pinch.scale = 1;

}

//旋转

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

    [tapView addGestureRecognizer:rotation];

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

    NSLog(@"旋转:%f",rotation.rotation);

    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);

    //如果不设置为0,就会转得非常快

    rotation.rotation = 0;

}

 //长按

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

    [tapView addGestureRecognizer:longPress];


    //滑动

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

    swipe.direction = UISwipeGestureRecognizerDirectionRight;

    [tapView addGestureRecognizer:swipe];

- (void)swipe:(UISwipeGestureRecognizer*)swipe {

    //移动

    //获取当前viewlocation

    CGPoint point  = [swipe locationInView:self.view];

    swipe.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);

}





0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1827次
    • 积分:125
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档