UI手势

   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);

}





阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MengHuaYin/article/details/51545812
文章标签: UI手势
个人分类: iOS软件开发
上一篇地理编码与反编码
下一篇iOS 零碎知识点总结
想对作者说点什么? 我来说一句

GestureRcognizer

2015年10月30日 1012KB 下载

没有更多推荐了,返回首页

关闭
关闭