UI--手势识别器

例: 创建UIImageview,添加手势

//创建imageview
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    imageView.backgroundColor = [UIColor blueColor];
    imageView.center = self.view.center;
    [self.view addSubview:imageView];
    [imageView release];
    
    //开启用户交互
    imageView.userInteractionEnabled = YES;

#pragma mark - 1.轻怕
    //创建手势对象
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    //手指个数
    tap.numberOfTouchesRequired = 2;
    //轻拍次数
    tap.numberOfTapsRequired = 2;
    //把手势添加给视图
    [imageView addGestureRecognizer:tap];
    [tap release];

#pragma mark - 2.长按 longPress
    //创建对象
    UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
    //最少长按时间
    longpress.minimumPressDuration = 2;
    //添加
    [imageView addGestureRecognizer:longpress];
    [longpress release];

#pragma mark - 3.捏合 pinch
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [imageView addGestureRecognizer:pinch];
    [pinch release];

#pragma mark - 4.轻扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    //轻扫方向
    swipe.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
    [imageView addGestureRecognizer:swipe];
    [swipe release];
    
    


#pragma mark- 手势识别器触发方法
//轻拍
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"轻拍");

}
//长按触发方法
- (void)longAction:(UILongPressGestureRecognizer *)longpress
{
    //其他部分手势也会触发长按
    //长按约束
    if (longpress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    }
    NSLog(@"长按: %@", longpress);
    
}

//捏合
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
    NSLog(@"捏合开始");
    //捏合时图片跟随变化
    
    //1.获取图片对象
    UIImageView *img = (UIImageView *)pinch.view;
    //2.等比例缩放
    [img setTransform:CGAffineTransformScale(img.transform, pinch.scale, pinch.scale)];
    
}

//轻扫
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"轻扫: %@", swipe);
    
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值