手势

手势,有规律的触摸事件的封装
手势类,抽象类,使用他的子类
UIGestureRecognizer 手势识别器

轻拍
UITapGestureRecognizer
长按
UILongPressGestureRecognizer
旋转
UIRotationGestureRecognizer
捏合
UIPinchGestureRecognizer
平移
UIPanGestureRecognizer
清扫
UISwipeGestureRecognizer
屏幕边缘清扫
UIScreenEdgePanGestureRecognizer


self.view.backgroundColor = [UIColor lightGrayColor];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, 350, 350)];

imageView.backgroundColor = [UIColor greenColor];
[self.view addSubview:imageView];

imageView.image = [UIImage imageNamed:@"6.jpg"];
imageView.contentMode = UIViewContentModeCenter;
//imageView, label 的用户交互默认是关闭的
imageView.userInteractionEnabled = YES;

//轻拍
//Target: 由谁来调用这个方法
//? 多态 =>
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
   //需要轻拍几次
   tap.numberOfTapsRequired = 1;
    //需要几根手指
    tap.numberOfTouchesRequired = 1;
    
    //将轻拍手势添加到imageView上,别的控件也可以添加手势
    [imageView addGestureRecognizer:tap];

}

//清扫
//在平移方法实现的情况下,清扫的优先级就没有平移的高

// {
// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// //从左往右划,冲右往左划
// //对于NS_OPTIONS类型的枚举,可以使用或( | )来连接多个值
// //不能同时支持上下左右滑动
// swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
// swipe.numberOfTouchesRequired = 1;
// [imageView addGestureRecognizer:swipe];
// }

//长按

// {
// UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
//
// //长按运行了两次
// [imageView addGestureRecognizer:longPress];
// }

//旋转

// {
// UIRotationGestureRecognizer *rotain = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
//
// [imageView addGestureRecognizer:rotain];
// }

//平移
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    
    [imageView addGestureRecognizer:pan];
    
    
}

//捏合
{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imageView addGestureRecognizer:pinch];
    
}

}

//方法写在那里就是哪里的方法

  • (void)tapAction:(UITapGestureRecognizer *)tap{

    NSLog(@"点击");
    //tap.view 获取手势所在的视图
    //强转保证要强转的类型就是你需要的类型
    UIImageView *imageView = (UIImageView *)tap.view;
    imageView.image = [UIImage imageNamed:@"3.jpg"];
    //怎么多次改变

}

  • (void)swipeAction:(UISwipeGestureRecognizer *)swipe{

    NSLog(@"清扫");
    UIImageView *imageView = (UIImageView *)swipe.view;
    imageView.image = [UIImage imageNamed:@"3.jpg"];
    }

  • (void)longAction:(UILongPressGestureRecognizer *)longPress{

    //判断,在长按手势生效的时候执行一次代码
    if (longPress.state == UIGestureRecognizerStateBegan) {

      NSLog(@"长按");
      UIImageView *imageView = (UIImageView *)longPress.view;
      imageView.image = [UIImage imageNamed:@"3.jpg"];
    

    }
    }

  • (void)rotationAction:(UIRotationGestureRecognizer *)rotation{

// 动画, transform2D放射变换

UIImageView *imageView = (UIImageView *)rotation.view;

imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;

}

  • (void)panAction:(UIPanGestureRecognizer *)pan{

    UIImageView *imageView = (UIImageView *)pan.view;
    //获取平移手势在视图上移动的距离
    CGPoint change = [pan translationInView:imageView];

imageView.transform = CGAffineTransformTranslate(imageView.transform, change.x , change.y);
[pan  setTranslation:CGPointZero inView:imageView];

}

  • (void)pinchAction:(UIPinchGestureRecognizer *)pinch{

    UIImageView *imageView = (UIImageView *)pinch.view;
    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
    //复位
    pinch.scale = 1;
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容