您的位置:首页 > 移动开发 > IOS开发

iOS笔记之手势

2015-11-11 11:04 387 查看
// 创建一个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(@"我被开始长按了吖");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: