iOS-基础知识--实现照片对手势的响应代码总结

iOS-基础知识--实现照片对手势的响应代码总结_第1张图片
从今若许闲乘月,拄杖无时夜叩门!

在视图控制器中代码实现照片对手势的响应,进行一些平移,旋转,或者变色等一些操作的实现,一般不建议在视图控制里面进行布局,此处方便学习手势的控制,在ViewController进行了一个图片的布局,下面是详细的介绍

//要想实现手势首先必须满足手势协议
@interface rootViewController ()

//声明一个UIImageView属性用来放添加手势的照片
 @property(nonatomic,strong)UIImageView *imageView;

实现部分:

//给UIImageView布局加照片 不过好像很少有人在这个方法里面弄可以去ViewDidLoad方法里面弄
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
  {
 //self.imageView = [[MyImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  self.imageView = [[UIImageView alloc ] initWithFrame:CGRectMake(100, 200, 300, 400)];
  self.imageView.image = [UIImage imageNamed:@"照片名.格式"];
  self.imageView.userInteractionEnabled=YES;
  }
  [self.view addSubview:self.imageView];
   return self;
}


方法实现

//实现协议内部的方法,Simultaneously: 同时识别手势
//如果想要同时触发手势的作用,需要实现此方法

-(BOOL)gestureRecognizer:(UIGestureRecognizer     *)gestureRecognizer  shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
{
NSLog(@"可以手势!");
return YES;
}

//自定义旋转图片方法的手势

-(void)rotationGesture
{
UIRotationGestureRecognizer*rotationXuanZhuan = [UIRotationGestureRecognizer new];
//手势添加响应事件
 [rotationXuanZhuan addTarget:selfaction:@selector(XuanZhuan:)];

//图片上添加手势
 // [self.imageView.picture addGestureRecognizer:rotationXuanZhuan];
  [self.imageView addGestureRecognizer:rotationXuanZhuan];
}

//定义旋转方法事件

-(void)XuanZhuan:(UIRotationGestureRecognizer * )rorationXZ

{

/*

 struct
CGAffineTransform {

 CGFloat a, b,
c, d;

 CGFloat tx,
ty;

 };

 其中a,d表示放大缩小有关

 b,c表示的是选择角度有关

 tx,ty是和平移有关*/


rorationXZ.view.transform = CGAffineTransformRotate(rorationXZ.view.transform, rorationXZ.rotation);
rorationXZ.rotation=0;//rotation代表旋转弧度
NSLog(@"转转转转!!");
}

//封装一个长按手势

-(void)longPressGesture
{
UILongPressGestureRecognizer *longPress =[UILongPressGestureRecognizer new];
[longPress addTarget:self action:@selector(longPress)];

 //至少按0.5秒才会有反应 一个属性
longPress.minimumPressDuration = 0.5;

[self.imageView addGestureRecognizer:longPress];
}

定义长按手势的事件

-(void)longPress

{ 
 NSLog(@"你在长按着我~~");
// self.imageView.backgroundColor = [UIColor redColor];// 图片背景变成红色
 self.view.backgroundColor=[UIColor yellowColor];//背景变成黄色
}

//封装平移手势

-(void)panGusture

{

UIPanGestureRecognizer *pan = [UIPanGestureRecognizer new];

[pan addTarget:self action:@selector(panView:)];

//将手势添加到图片上

[self.imageView addGestureRecognizer:pan];
}

//实现平移方法事件

-(void)panView:(UIPanGestureRecognizer *)pan

{

NSLog(@"~~~~~我要平移~~~~~~~~~~~~%@",pan);

//在pan.view上移动的距离

CGPoint translation = [pan translationInView:pan.view];

//获取View的中心点

CGPoint center = pan.view.center;

//中心点的x值+挪动距离的x值

center.x = center.x + translation.x;

//中心点的y值+挪动距离的y值

center.y = center.y+translation.y;

pan.view.center = center;

//清空移动距离

[pan setTranslation:CGPointZero inView:pan.view];

}

//封装缩放手势

-(void)pinchgesture

{


UIPinchGestureRecognizer *pinch = [UIPinchGestureRecognizer new];

[pinch addTarget:self action:@selector(pinchView:)];

//将手势添加到图片之上

[self.imageView addGestureRecognizer:pinch];
}

//实现缩放的手势事件

-(void)pinchView:(UIPinchGestureRecognizer *)pinch

{

NSLog(@"捏合,捏一捏~~~~~~");
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
//scale  相对于屏幕来说的坐标点
   pinch.scale = 1;
}

//添加相应的方法
- (void)viewDidLoad {

   [super viewDidLoad];

  //[self rotationGesture];

  // [self longPressGesture];

  // [self panGusture];

    [self pinchgesture];

}

你可能感兴趣的:(iOS-基础知识--实现照片对手势的响应代码总结)