UI手势

作者: dliys | 来源:发表于2016-02-18 17:39 被阅读0次

    UIGestureRecognizer手势识别器

    手势:有规律的触摸

    UIGestureRecognizer抽象类

    七种手势:轻拍(tap)长按(longPress)旋转(rotation)捏合(pinch)拖拽(pan)轻扫(swipe)屏幕边缘拖拽(screenEdgePan)

    轻拍tap

    创建对象

    获取到轻拍手势时让self调用tapAction:方法

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(tapAction:)];

    添加手势

    [imgView addGestureRecognizer:tap];

    内存管理

    [tap release];

    点击次数

    tap.numberOfTapsRequired =2;

    手指个数

    tap.numberOfTouchesRequired =2;

    #pragma mark -轻拍tap

    - (void)tapAction:(UITapGestureRecognizer*)tap

    {

    NSLog(@"轻拍");

    }

    长按longPress

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(longPressAction:)];

    [imgView addGestureRecognizer:longPress];

    [longPress release];

    长按时间

    longPress.minimumPressDuration =1;

    #pragma mark -长按longPress

    - (void)longPressAction:(UILongPressGestureRecognizer*)longPress

    {

    if(longPress.state==UIGestureRecognizerStateBegan) {

    NSLog(@"长按");

    }

    }

    旋转rotation

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:selfaction:@selector(rotationAction:)];

    [imgView addGestureRecognizer:rotation];

    [rotation release];

    #pragma mark -旋转rotation

    - (void)rotationAction:(UIRotationGestureRecognizer*)rotation

    {

    *UIView transform属性专门用来进行形变(位置position/旋转rotation/缩放scale)设置

    UIImageView*imgView = (UIImageView*)rotation.view;

    imgView.transform=CGAffineTransformMakeRotation(rotation.rotation);

    NSLog(@"旋转");

    }

    捏合pinch

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:selfaction:@selector(pinchAction:)];

    [imgView addGestureRecognizer:pinch];

    [pinch release];

    #pragma mark -捏合pinch

    - (void)pinchAction:(UIPinchGestureRecognizer*)pinch

    {

    UIImageView*imgView = (UIImageView*)pinch.view;

    imgView.transform=CGAffineTransformMakeScale(pinch.scale, pinch.scale);

    //缩放scale(比例)(下面两行代码效果等同于上一行)

    //    imgView.transform = CGAffineTransformScale(imgView.transform, pinch.scale, pinch.scale);

    //    pinch.scale = 1;

    NSLog(@"捏合");

    }

    拖拽pan

    UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];

    [imgViewaddGestureRecognizer:pan];

    [panrelease];

    #pragma mark -拖拽pan

    - (void)panAction:(UIPanGestureRecognizer*)pan

    {

    NSLog(@"拖拽");

    UIImageView*imgView = (UIImageView*)pan.view;

    CGPointp = [pantranslationInView:imgView];

    imgView.transform=CGAffineTransformMakeTranslation(p.x, p.y);

    //设置跟随属性(下面两行代码效果等同于上一行)

    //    imgView.transform = CGAffineTransformTranslate(imgView.transform, p.x, p.y);

    //    [pan setTranslation:CGPointZero inView:imgView];

    }

    轻扫swipe

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(swipeAction:)];

    默认只识别向右

    设置方向时最多只能设置水平(左/右)或者垂直(上/下)

    swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;

    [imgView addGestureRecognizer:swipe];

    [swipe release];

    #pragma mark -轻扫swipe

    - (void)swipeAction:(UISwipeGestureRecognizer*)swipe

    {

    if(swipe.direction==UISwipeGestureRecognizerDirectionLeft) {

    NSLog(@"向左");

    }

    NSLog(@"轻扫");

    }

    屏幕边缘拖拽screenEdgePan

    UIScreenEdgePanGestureRecognizer*sep = [[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:@selector(sepAction:)];

    sep.edges=UIRectEdgeLeft;

    [self.viewaddGestureRecognizer:sep];

    [seprelease];

    #pragma mark -屏幕边缘拖拽screenEdgePan

    - (void)sepAction:(UIScreenEdgePanGestureRecognizer*)sep

    {

    NSLog(@"屏幕边缘");

    }

    相关文章

      网友评论

          本文标题:UI手势

          本文链接:https://www.haomeiwen.com/subject/ibwmkttx.html