UI中的几种手势

    UITapGestureRecognizer   点击手势
    UIPanGestureRecognizer   拖拽,慢速移动

    UISwipeGestureRecognizer 快递移动

    UIRotationGestureRecognizer  旋转手势

    UIPinchGestureRecognizer  缩放

    UILongPressGestureRecognizer 长按


//手势的声明



   
// 拖拽,慢速移动手势
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
//    [_imageView addGestureRecognizer:pan];
   
/*
    // 快递移动 (下)
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hanleSwipe:)];
    // 设置快速手势的方向我下
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [_imageView addGestureRecognizer:swipeDown];
   
    // 快递移动 (上)
    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hanleSwipe:)];
    // 设置快速手势的方向为上
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [_imageView addGestureRecognizer:swipeUp];
*/

   
   
// 旋转手势
//    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
//    [_imageView addGestureRecognizer:rotation];
   
   
   
// 长按手势
//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
//    // 长按时间,2秒后相应方法
//    longPress.minimumPressDuration = 2;
//    [_imageView addGestureRecognizer:longPress];
   
   
   
// 挤压手势(一般用来做图片缩放)
   
UIPinchGestureRecognizer *pinch = [[ UIPinchGestureRecognizer alloc ] initWithTarget : self action : @selector (handlePinch:)];
    [
_imageView addGestureRecognizer :pinch];
}

// 拖拽,慢速移动手势
- (
void )handlePan:( UIPanGestureRecognizer *)pan
{
   
// 获取手指在屏幕上的偏移量
   
CGPoint point = [pan translationInView : self . view ];
//    NSLog(@"%f:x   %f:y", point.x, point.y);
   
_imageView . center = CGPointMake ( _imageView . center . x + point. x , _imageView . center . y + point. y );
   
// 将手势位置还原
    [pan
setTranslation : CGPointZero inView : self . view ];
}

// 快速移动手势
- (
void )hanleSwipe:( UISwipeGestureRecognizer *)swipe
{
   
// 判断手势的方向
   
if (swipe. direction == UISwipeGestureRecognizerDirectionDown )
    {
       
// 动画
        [
UIView beginAnimations : nil context : NULL ];
       
// 多久之后执行动画
//        [UIView setAnimationDelay:1];
       
// 动画持续时间
        [
UIView setAnimationDuration : 1 ];
       
       
// 代码写在中间...
       
CGRect rect = _imageView . frame ;
        rect.
origin . y = 300 ;
       
_imageView . frame = rect;
       
       
// 提交动画
        [
UIView commitAnimations ];
    }
   
else if (swipe. direction == UISwipeGestureRecognizerDirectionUp )
    {
        [
UIView animateWithDuration : 1 animations :^{
           
CGRect rect = _imageView . frame ;
            rect.
origin . y = - 100 ;
           
_imageView . frame = rect;
        }];
    }
}

// 旋转手势
- (
void )handleRotation:( UIRotationGestureRecognizer *)rotation
{
   
static CGFloat lastScale;
   
   
// 利用仿射变换旋转
   
_imageView . transform = CGAffineTransformMakeRotation (lastScale + rotation. rotation );
   
   
// 判断是否旋转结束
   
if (rotation. state == UIGestureRecognizerStateEnded ) {
        lastScale += rotation.
rotation ;
    }
}

// 长按手势,会被多次的调用
- (
void )handleLongPress:( UILongPressGestureRecognizer *)longPress
{
   
static BOOL isHiden = NO ;
   
   
if (longPress. state == UIGestureRecognizerStateBegan )
    {
       
NSLog ( @"开始" );
       
        isHiden = !isHiden;
       
        [
UIView animateWithDuration : 1 animations :^{
           
_imageView . alpha = isHiden ? 0.1 : 1 ;
        }];
    }
   
else if (longPress. state == UIGestureRecognizerStateEnded )
    {
       
NSLog ( @"结束" );
    }
}

// 缩放手势
- (
void )handlePinch:( UIPinchGestureRecognizer *)pinch
{
//    NSLog(@"%f", pinch.scale);
   
// CGAffineTransformScale 缩放变换
//    _imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
//    pinch.scale = 1;
   
   
   
// 限制最大和最小倍数
   
static CGFloat lastScale;
   
static CGFloat max = 1.3 ;
   
static CGFloat min = 0.8 ;
   
   
if (pinch. state == UIGestureRecognizerStateBegan )
    {
        lastScale = pinch.
scale ;
    }
   
   
if (pinch. state == UIGestureRecognizerStateBegan ||
        pinch.
state == UIGestureRecognizerStateChanged )
    {
       
// KVC
       
CGFloat currScale = [[ _imageView . layer valueForKeyPath : @"transform.scale" ] floatValue ];
       
       
CGFloat newScale = 1 - (lastScale - pinch. scale );
       
        newScale =
MAX (newScale, min / currScale);
        newScale =
MIN (newScale, max / currScale);
       
       
_imageView . transform = CGAffineTransformScale ( _imageView . transform , newScale, newScale);
       
        lastScale = pinch.
scale ;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值