UI基础学习之(十四):手势

接上篇:transform

手势配合使用transform


手势可以添加在任何想要的地方,一般添加在imageView上。

GestureRecognizer 手势识别器

使用手势,首先要确定控件的手势交互式打开的:
//打开交互
self.rv.imv.userInteractionEnabled = YES;
 
1、轻拍
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
设置属性
tap.numberOfTapsRequired = 1; // 次数
tap.numberOfTouchesRequired = 2; // 触控数
 
//轻拍
- (void)tapAction:(UIGestureRecognizer *)sender
{
    NSLog(@"tap");
}
 
//将手势添加在视图上
[self.rv.imv addGestureRecognizer:tap];
 
2、长按
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.rv.imv addGestureRecognizer:longPress];
//长按
- (void)longPress:(UILongPressGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan) {
//       开始
        sender.view.bounds = CGRectMake(0, 0, 300, 300);
       
    } else if (sender.state == UIGestureRecognizerStateEnded) {
//       结束
        sender.view.bounds = CGRectMake(0, 0, 250, 250);
    }
}

 
3、轻扫
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
 
属性 轻扫方向 只能相对的方向,左右或上下
swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
 
//轻扫
- (void)swipeAction:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"swipe");
}
 
//将手势添加在视图上
[self.rv.imv addGestureRecognizer:swipe];
 
 
4、拖动
 
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
 
//拖动
- (void)panAction:(UIPanGestureRecognizer *)sender
{
//    获取坐标
    CGPoint point = [sender translationInView:sender.view];
   
    sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y);
   
//    清零
    [sender setTranslation:CGPointZero inView:sender.view];
}

//将手势添加在视图上
[self.rv.imv addGestureRecognizer:pan];
 

5、旋转
 
UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
 
//旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)sender
{
    sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
   
    sender.rotation = 0;
}
 
//将手势添加在视图上
[self.rv.imv addGestureRecognizer:rotation];
 
6、捏合
 
UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
 
//捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)sender
{
    sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
   
//    置1 
    sender.scale = 1;
   
}
 
//将手势添加在视图上
[self.rv.imv addGestureRecognizer:pinch];
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值