iOS七大手势之(平移、捏合、轻扫、屏幕边缘轻扫)手势识别器方法

   // 创建UIImageView对象

    UIImage *image = [UIImage imageNamed:@"5.jpg"];

    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];

    imageView.frame = CGRectMake(30, 20, 500, 500);

    imageView.userInteractionEnabled = YES;

    [self.view addSubview:imageView];

    [imageView release];

    

    // imageView添加手势识别器(轻拍)

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

    [imageView addGestureRecognizer:tapGestureRecognizer];

    [tapGestureRecognizer release];

    

    // imageView添加手势识别器(旋转)

    UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(romation:)];

    [imageView addGestureRecognizer:rotationGestureRecognizer];

    [rotationGestureRecognizer release];

    

    // imageView添加手势识别器(捏合)

    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];

    [imageView addGestureRecognizer:pinchGestureRecognizer];

    [pinchGestureRecognizer release];

    

    // imageView添加手势识别器(长按)

    UILongPressGestureRecognizer *longGestureRecongnizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

    [imageView addGestureRecognizer:longGestureRecongnizer];

    [longGestureRecongnizer release];

    

    // imageView添加手势识别器(平移)

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

    [imageView addGestureRecognizer:panGestureRecognizer];

    [panGestureRecognizer release];

    

    // imageView添加手势识别器(轻扫)

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

    /*

     UISwipeGestureRecognizerDirectionRight = 1 << 0,

     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,

     UISwipeGestureRecognizerDirectionUp    = 1 << 2,

     UISwipeGestureRecognizerDirectionDown  = 1 << 3

     */

    // 轻扫的方向

    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;

    [imageView addGestureRecognizer:swipeGestureRecognizer];

    [swipeGestureRecognizer release];

    

    // imageView添加手势识别器(屏幕边缘轻扫)

    UIScreenEdgePanGestureRecognizer *screenEdgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePanGestureRecognizer:)];

    /*

     UIRectEdgeNone   = 0,

     UIRectEdgeTop    = 1 << 0,

     UIRectEdgeLeft   = 1 << 1,

     UIRectEdgeBottom = 1 << 2,

     UIRectEdgeRight  = 1 << 3,

     UIRectEdgeAll    = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight

     */

    screenEdgePanGestureRecognizer.edges = UIRectEdgeTop;

    [imageView addGestureRecognizer:screenEdgePanGestureRecognizer];

    [screenEdgePanGestureRecognizer release];








//  轻拍手势的方法

- (void)tap:(UIGestureRecognizer *)tapGestureRecognizer

{

    UIView *view = tapGestureRecognizer.view;

    // 修改视图的大小

    view.bounds = CGRectMake(0, 0, 100, 100);

    NSLog(@"a");

}


//  旋转手势的方法

- (void)romation:(UIRotationGestureRecognizer *)rotationGestureRecognizer

{

    UIView *view = rotationGestureRecognizer.view;

    

//    // 顺时针旋转PI/4

//    view.transform = CGAffineTransformMakeRotation(M_PI_4);

    

//    // 逆时针旋转PI/4

//    view.transform = CGAffineTransformRotate(view.transform, -M_PI_4);

    

    // 可以两个方向旋转

    view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);


}

//  捏合手势的方法

- (void)pinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer

{

    

    UIView *view = pinchGestureRecognizer.view;

    //  让视图在原来的基础上放大1.5

    view.transform = CGAffineTransformScale(view.transform, 1.5, 1.5);

//    // 让视图在原来的基础上缩小0.5

//    view.transform = CGAffineTransformScale(view.transform, 0.5, 0.5);

}


//  长按手势的方法

- (void)longPress:(UILongPressGestureRecognizer *)longPressGestureRecongizer

{

    

    NSLog(@"长按");

}


//  平移手势的方法

- (void)panGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer

{

    UIView *view = panGestureRecognizer.view;

    

    CGPoint delPoint = [panGestureRecognizer translationInView:view];

    

    //  视图是在上次的基础上平移

    view.transform = CGAffineTransformMakeTranslation(delPoint.x,delPoint.y);


    //  每次平移后,xy轴的平移是不是归零

    [panGestureRecognizer setTranslation:(CGPointZero) inView:view];

    NSLog(@"平移");

}


//  轻扫手势的方法

- (void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)swipeGestureRecognizer

{

    UIImageView *view = (UIImageView *)swipeGestureRecognizer.view;

    if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {

        view.image = [UIImage imageNamed:@"9.jpg"];

    }

}


//  屏幕边缘轻扫手势的方法

- (void)screenEdgePanGestureRecognizer:(UIScreenEdgePanGestureRecognizer *)screenEdgePanGestureRecognizer

{

    UIImageView *view = (UIImageView *)screenEdgePanGestureRecognizer.view;

    view.bounds = CGRectMake(0, 0, 200, 100);

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值