UIGesture的一些属性

本文深入探讨了iOS开发中常用的手势识别与交互技术,包括单击、移动、旋转、缩放、长按与滑动操作的实现方法及应用案例。通过详细的代码解析,展示了如何利用Swift语言和UIKit框架,为用户界面添加丰富且直观的交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.单击

-(void)createTapGesture{

    //点击

    //UITapGestureRecognizer:继承自手势基类UIGestureRecognizer

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

    //设置点击的手指个数:默认是1

    tap.numberOfTouchesRequired = 1;

    //设置点击次数:默认是1

    //单击、双击

    tap.numberOfTapsRequired = 2;

 

 //当前视图默认只支持单点触摸,不支持多点触摸,所以要开启视图的多点触摸模式

    self.view.multipleTouchEnabled=YES;

 

//设置点击事件的优先级

    //双击事件的优先级大于单击事件的优先级

    [singelTap requireGestureRecognizerToFail:doubleTap];

 

    [_iv addGestureRecognizer:tap];

    [tap release];

}

 

-(void)tapAction:(UITapGestureRecognizer *)tap{

    //单击:修改背景颜色

    self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

}

2.移动

-(void)createPanGesture{

    //移动

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];

    //把手势添加在_iv上

    [_iv addGestureRecognizer:pan];

    [pan release];

}

 

-(void)panAction:(UIPanGestureRecognizer *)pan{

    //获取pan手势在视图中的位置 --> point

    CGPoint pt = [pan locationInView:self.view];

    //修改_iv的center

    _iv.center = pt;

 

 

 

    //获取 拖动手势的偏移量

    //手势偏移量 相对的视图 要和 _imageView相对的坐标视图保持一致(相对的都是同一个视图)

    CGPoint offSet = [pan translationInView:self.view];//相对于self.view的偏移量

    //修改中心点位置

    CGPoint point = _imageView.center;

    _imageView.center = CGPointMake(point.x+offSet.x, point.y+offSet.y);

    //把手势的偏移量 每次重置(0,0)CGPointZero

    [pan setTranslation:CGPointZero inView:self.view];

}    

3.旋转

-(void)createRotationGesture{

    //旋转

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

    rotation.delegate = self;

    [_iv addGestureRecognizer:rotation];

    [rotation release];

}

 

-(void)rotaionAction:(UIRotationGestureRecognizer *)rotate{

    //视图的变换

    //第一个参数:CGAffineTransform --> 跟随要进行变换的视图

    //第二个参数:角度 --> 跟随手指的旋转角度 --> rotate.rotation

    [_iv setTransform:CGAffineTransformRotate(_iv.transform, rotate.rotation)];

    //旋转角度置为0

    rotate.rotation = 0;

 

 //<2>不需要将rotate.rotation置0

    [_iv setTransform:CGAffineTransformMakeRotation(rotate.rotation)];

}

4.缩放/捏合

-(void)createPinchGesture{

    //缩放

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

    //想让旋转和缩放同时触发,需要分别在旋转和缩放的手势创建过程中设置代理

    pinch.delegate = self;

    //把缩放手势添加到要进行缩放的视图上

    [_iv addGestureRecognizer:pinch];

    [pinch release];

}

 

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

    //视图变换

    //第一个参数:CGAffineTransform类型 --> 跟随要进行变换的视图

    //第二/三个参数:在x/y方向的位移 --> 根据手势的捏合程度进行缩放

    [_iv setTransform:CGAffineTransformScale(_iv.transform, pinch.scale, pinch.scale)];

    //把pinch.scale 比例置为1

    pinch.scale = 1;

 

 

//<2>不需要把scale置为1

    //参数1:等比例改变x方向的内容

    //参数2:等比例改变y方向的内容

    [_iv setTransform:CGAffineTransformMakeScale(pinch.scale, pinch.scale)];

}

5.长按:删除/发消息等

-(void)createLongPressGesture{

    //长按

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

    longPress.delegate = self;

 

//是指手指根数

    [longPress setNumberOfTouchesRequired:1];

    //设置长按的最小时间值,如果不超过该时间就不会触发长按事件

    [longPress setMinimumPressDuration:1];

 

    [_iv addGestureRecognizer:longPress];

    [longPress release];

}

 

-(void)longPressAction:(UILongPressGestureRecognizer *)longPress{

    //每个手势都是有状态的

    //1.如果当前手势状态是开始 --> 开始录音

    if (longPress.state == UIGestureRecognizerStateBegan) {

        NSLog(@"开始录音");

    }

    //2.如果当前手势状态是停止 --> 录音结束,发送消息

    if (longPress.state == UIGestureRecognizerStateEnded) {

        //判断录音是否完成 --> 有录音才发送

        NSLog(@"录音结束,发送消息");

    }

}

6.滑动

-(void)createSwipeGesture{

    //滑动手势:让_iv一次移动5

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

    //需要指定滑动方向:一个滑动手势只能指定一个方向,如果需要设置多个方向的滑动,要创建多个滑动手势

    [swipe setDirection:UISwipeGestureRecognizerDirectionLeft];

    [_iv addGestureRecognizer:swipe];

    [swipe release];

}

 

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

    //滑动

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {

        //如果是向左滑动 --> _iv向左移5个单位

        _iv.frame = CGRectMake(_iv.frame.origin.x - 5_iv.frame.origin.y_iv.frame.size.width_iv.frame.size.height);

    }

}

 

/*

 如果要想让两个没有相互影响的手势同时生效 那么必须要设置这两个手势的代理,两个手势委托代理来处理就可以了

 代理需要遵守协议

 

 比如 旋转和捏合没有相互影响可以设置同时生效 那么就需要设置旋转手势的代理和捏合手势的代理

 同一个代理

 */

#pragma mark - UIGestureRecognizerDelegate协议

//是否允许 两个手势同时生效

//NO不允许

//YES允许

//两个手势 必须要设置代理

 

缩放+捏合必须要设置代理

 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    NSLog(@"first:%@",[gestureRecognizer class]);

    NSLog(@"second:%@",[otherGestureRecognizer class]);

    return YES;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值