iOS-触摸手势

//演示手势的使用
// 手势是触摸的封装

    //学习6种手势
    //UITapGestureRecognizer        轻击
    //[self testTap];
    //UIPanGestureRecognizer        拖动
    //[self testPan];
    
    //UIRotationGestureRecognizer   旋转
    //[self testRotation];
    
    //UIPinchGestureRecognizer      缩放
    //[self testPinch];
    
    //UISwipeGestureRecognizer      滑动手势
    //[self testSwipe];
    
    //UILongPressGestureRecognizer  长按手势
    [self testLongPress];
-(void)testLongPress
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dealLongPress:)];
    [_imageView addGestureRecognizer:longPress];
}
-(void)dealLongPress:(UILongPressGestureRecognizer *)longPress
{
    NSLog(@"longPress");
    //注意: 这个事件处理方法会执行两次
    if(longPress.state == UIGestureRecognizerStateBegan
       )
    {
        //显示对话框
        UIAlertView *alertView = [[UIAlertView alloc] init];
        alertView.message = @"真的要保存嘛?";
        [alertView addButtonWithTitle:@"确定"];
        [alertView show];
    }
}

-(void)testSwipe
{
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dealSwipe:)];
    //默认: 从左到右滑动生效
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [_imageView addGestureRecognizer:swipe];
}
-(void)dealSwipe:(UIPinchGestureRecognizer *)swipe
{
    NSLog(@"swipe");
    
    //
    _index++;
    if(_index == _images.count)
    {
        _index = 0;
    }
    _imageView.image = [UIImage imageNamed:_images[_index]];

}
-(void)testPinch
{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(dealPinch:)];
    [_imageView addGestureRecognizer:pinch];
}
-(void)dealPinch:(UIPinchGestureRecognizer *)pinch
{
    //
    double scale = pinch.scale;
    _imageView.transform = CGAffineTransformMakeScale(scale, scale);
}

-(void)testRotation
{
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(dealRotation:)];
    [_imageView addGestureRecognizer:rotation];
}
-(void)dealRotation:(UIRotationGestureRecognizer *)rotation
{
    //获取旋转的角度(弧度)
    double r = rotation.rotation;
    NSLog(@"r = %f",r);
    
    //对视图进行旋转
    // 仿射变换对象 CGAffineTransform
    _imageView.transform = CGAffineTransformMakeRotation(r);
    
}
-(void)testPan
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dealPan:)];
    [_imageView addGestureRecognizer:pan];
}
-(void)dealPan:(UIPanGestureRecognizer *)pan
{
    //CGPoint point = [pan locationInView:self.view];
    //_imageView.center = point;
    
    //开始移动的时候记录视图的中心点
    if(pan.state == UIGestureRecognizerStateBegan)
    {
        _startPoint = _imageView.center;
    }
    //移动的时候获取, 移动开始点和当前的点得偏移
    //  imageView视图位置, imageView开始位置 + offset
    if(pan.state == UIGestureRecognizerStateChanged)
    {
        //
        CGPoint offset = [pan translationInView:self.view];
        _imageView.center = CGPointMake(_startPoint.x+offset.x, _startPoint.y + offset.y);
        
    }
}

-(void)testTap
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealTap:)];
    [_imageView addGestureRecognizer:tap];
    
    //
    
}
//事件处理方法参数一般有一个, 参数类型是事件源
-(void)dealTap:(UITapGestureRecognizer *)tap
{
    NSLog(@"tap");
    
    //获取点击位置
    //传入一个参考view, 以这个view左上角点原点,获取位置
    CGPoint point = [tap locationInView:_imageView];
    NSLog(@"x=%f y=%f",point.x,point.y);

}

你可能感兴趣的:(iOS-触摸手势)