iOS笔记之手势

    // 创建一个imageView
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];
    self.imageView.frame = CGRectMake(0, 0, 375, 667);
    [self.view addSubview:self.imageView];
    [_imageView release];

    NSLog(@"%@", self.imageView);

    // 图片如果要添加手势, 要把图片的用户交互打开, 它和Label默认是NO
    self.imageView.userInteractionEnabled = YES;
    NSLog(@"%@", self.imageView);
   // 控件的交互如果打开, 打印控件对象没有userInterfaceEnabled = NO这句话, 就可以根据有没有这句话判断交互有咩有打开


//    1. 点击    gesture  手势
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//    //把手势加到指定的视图上
//    [self.imageView addGestureRecognizer:tap];
//    [tap release];   
//    tap.numberOfTapsRequired = 2;  // 需要点击的次数
//    tap.numberOfTouchesRequired = 1;  // 需要几个手指进行触摸


//    2. 长按
//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
//    // 把手势添加到imageView上
//    [self.imageView addGestureRecognizer:longPress];
//    [longPress release];
//    
//    // 设置长按触发的时间
//    longPress.minimumPressDuration = 2;


//    3. 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [self.imageView addGestureRecognizer:rotation];
    [rotation release];


//   4. 捏合(缩放)
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.imageView addGestureRecognizer:pinch];
    [pinch release];

//    5. 拖拽
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
//    [self.imageView addGestureRecognizer:pan];
//    [pan release];
//    


//    6. 轻扫
//    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//    [self.imageView addGestureRecognizer:swipe];
//    [swipe release];
//    // 轻扫的方向
//    swipe.direction = UISwipeGestureRecognizerDirectionDown;


// 旋转地方法
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {
    // 可以通过手势, 知道手势添加到哪个视图上
    UIImageView *imageView = (UIImageView *)rotation.view;
    // 找到视图, 并且让视图进行旋转
    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
    rotation.rotation = 0;





}

//   4. 捏合(缩放)
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
    UIImageView *imageView = (UIImageView *)pinch.view;
    // 让视图进行缩放
    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
    NSLog(@"%f", pinch.scale);
    pinch.scale = 1;

}


// 拖拽的方法
- (void)panAction:(UIPanGestureRecognizer *)pan {
    // 通过手势找视图
    UIImageView *imageView = (UIImageView *)pan.view;
    // 通过手势来获取移动的点
    CGPoint p = [pan translationInView:imageView];
    // 通过设置transform实现拖拽
    imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
    //[pan setTranslation:CGPointZero inView:imageView];
}


// 轻扫手势的方法
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {
    if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"向下轻扫");
    }
}

// 点击之后触发的方法
- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"我被点击了吖");
}


// 长按之后触发的方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"我被开始长按了吖");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值