首页 > 代码库 > 手势的6种使用方法

手势的6种使用方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 280, 430)];
    imageView.image = [UIImage imageNamed:@"2.jpg"];
    //将用户交互打开,,切记  只有两个UIImage  和UILabel  都要打开交互
    [imageView setUserInteractionEnabled:YES];
    [self.view addSubview:imageView];
    [imageView release];
    
    /*
    //手势使用  看继承关系,,,有没有自己的初始化方法
    //1.点击
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    //1.1设置要点击几次才会触发方法
    tap.numberOfTapsRequired = 3;
    //1.2需要几个手指点击
    tap.numberOfTouchesRequired = 2;
    //1.3将手势添加到imageView上
    [imageView addGestureRecognizer:tap];
    [tap release];
    */
    
    /*
    //2.长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    //1.1判定为长按手势需要的最短时间
    longPress.minimumPressDuration = 3;
    //1.2判定为长按的过程中,允许用户手指移动的距离
    longPress.allowableMovement = 300;
    [imageView addGestureRecognizer:longPress];
    [longPress release];
    */
    
    /*
    //3.旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [imageView addGestureRecognizer:rotation];
    [rotation release];
     */
    
    /*
    //4.捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imageView addGestureRecognizer:pinch];
    [pinch release];
    */
    /*
    //5.拖拽
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imageView addGestureRecognizer:pan];
    [pan release];
    */
    
    //6.清扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(awipeAction:)];
    [imageView addGestureRecognizer:swipe];
    //清扫的方向,向左  (如果要四个方向,就只能加四个手势)
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [swipe release];
    
    
    
}
//6.清扫
- (void)awipeAction:(UISwipeGestureRecognizer *)awipe
{
    if (awipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"向左");
    }
    NSLog(@"清扫");
}
//5.拖拽
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    UIImageView *view = (UIImageView *)pan.view;
    //获得手势经过的点
    CGPoint p = [pan translationInView:view];
    //对视图的transform属性改变
    view.transform = CGAffineTransformTranslate(view.transform, p.x, p.y);
    //对拖拽的位置进行初始化
    [pan setTranslation:CGPointZero inView:view];
//    NSLog(@"拖拽");
}
//4.捏合
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    //试图的TRanform属性
    UIImageView *view = (UIImageView *)pinch.view;
    //捏合的x,y的方向
//    view.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale); //捏合后重置
    //在原有的基础上在捏合
    view.transform = CGAffineTransformScale(view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;
    NSLog(@"捏合");
}
//3.旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
    //视图的transform属性---变形
    //1.获得添加手势的视图
    UIImageView *imageView = (UIImageView *)rotation.view;
    //2.旋转的角度  (属性,角度)
    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
    rotation.rotation = 0;
    
//    NSLog(@"旋转");
}
//2.长按
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    //长按的方法在手势的各个状态都会触发,所以需要进行判断
//    longPress.state
    if (longPress .state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始喽噢!!");
    }else if (longPress.state == UIGestureRecognizerStateEnded){
        NSLog(@"长按结束了呢!!");
    }
}
//1.点击点击手势,的触发方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"快看,那是个塔!");
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


本文出自 “小刘_Blog” 博客,请务必保留此出处http://liuyafang.blog.51cto.com/8837978/1548157

手势的6种使用方法