iOS 六种手势

本文详细介绍了iOS开发中常用的六种手势识别器的实现方法,包括点击、拖动、旋转、缩放、滑动及长按手势。每种手势通过具体的代码示例展示了如何设置手势识别器并处理相应的手势事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.点击手势

-(void)testTap{

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealTap:)];

    [_imageView addGestureRecognizer:tap];

}

-(void)dealTap:(UITapGestureRecognizer *)tap{

    CGPoint point = [tap locationInView:_imageView];

    NSLog(@"x=%f y=%f",point.x,point.y);

}


2.拖动手势

-(void)testPan{

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dealPan:)];

    [_imageView addGestureRecognizer:pan];

}

-(void)dealPan:(UIPanGestureRecognizer *)pan{

    if (pan.state == UIGestureRecognizerStateBegan) {

        _startPoint = _imageView.center;

    }

//    locationInViewtranslationInView的区别

//    1  translationInViewUIPanGestureRecognizer下面的一个属性

//    locationInView则是UIGestureRecognizer下面的属性

//    2  translationInView 在指定的坐标系中移动

//    locationInView 通常是指单点位置的手势 得到当前点击下在指定视图中位置的坐标

    if (pan.state == UIGestureRecognizerStateChanged) {

        CGPoint offset = [pan translationInView:self.view];

        _imageView.center = CGPointMake(_startPoint.x+offset.x, _startPoint.y+offset.y);

    }

}


3.旋转手势

-(void)testRotation{

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(dealRotation:)];

    [_imageView addGestureRecognizer:rotation];

}

-(void)dealRotation:(UIRotationGestureRecognizer *)rotation{

    double r = rotation.rotation;

    _imageView.transform = CGAffineTransformMakeRotation(r);

}


4.缩放手势

-(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);

}


5.滑动手势

-(void)testSwipe{

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dealSwipe:)];

    swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;

    [_imageView addGestureRecognizer:swipe];

}

-(void)dealSwipe:(UISwipeGestureRecognizer *)swipe{

    _index ++;

    if (_index == _images.count) {

        _index = 0;

    }

    _imageView.image = [UIImage imageNamed:_images[_index]];

}


6.长按手势

-(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];

    }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值