#####画板实现第二波 #####1.将对图片添加一系列手势操做,原有实现不能实现git
#####1.1添加一个图片处理的VIEWgithub
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ NSLog(@"%@",info); UIImage *image=info[UIImagePickerControllerOriginalImage]; HYLImageHandleView *imageHandView=[[HYLImageHandleView alloc]initWithFrame:self.drawView.bounds]; [self.drawView addSubview:imageHandView]; imageHandView.imageView.image=image; [self dismissViewControllerAnimated:YES completion:nil]; }
#####1.2图片view中一些设置atom
#pragma mark - setter/getter -(UIImageView *) imageView{ if (_imageView==nil) { UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.bounds]; _imageView=imageView; [self addSubview:_imageView]; } return _imageView; } #pragma mark -drawRect - (void)drawRect:(CGRect)rect { }
#####2.添加各类手势.net
-(instancetype) initWithFrame:(CGRect)frame{ if (self=[super initWithFrame:frame]) { [self setPan]; [self setLongPress]; [self setRotation]; [self setPinch]; } return self; }
#pragma mark - setpan -(void) setPan{ UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]; [self.imageView addGestureRecognizer:pan]; } -(void) pan:(UIPanGestureRecognizer *)pan{ } #pragma mark -setLongPress -(void) setLongPress{ UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)]; [self.imageView addGestureRecognizer:longPress]; } -(void) longPress:(UILongPressGestureRecognizer *)longPress{ } #pragma mark - setRotation -(void) setRotation{ UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)]; [self.imageView addGestureRecognizer:rotation]; } -(void) rotation:(UIRotationGestureRecognizer *)rotation{ } #pragma mark -setPinch -(void) setPinch{ UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)]; [self.imageView addGestureRecognizer:pinch]; } -(void) pinch:(UIPinchGestureRecognizer *)pinch{ }
#####3.对于手势没法响应问题的解决 #####3.1解决的方法:首先检查这个控件是否能响应事件,如:imageView,还有就是控件的父控件就不能响应code
-(UIImageView *) imageView{ if (_imageView==nil) { UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.bounds]; _imageView=imageView; //注意点,容许用户交互,不能响应事件时通常查这个 _imageView.userInteractionEnabled=YES; self.backgroundColor=[UIColor clearColor]; [self setPan]; [self setLongPress]; [self setRotation]; [self setPinch]; [self addSubview:_imageView]; } return _imageView; }
#####3.2手势里面的写法差很少 #####4.介绍block的一种用法,用来解决耦合 #####4.1当操做了手势后,想保存图片到drawView上时对象
/** imageHandleCompletionBlock*/ @property (nonatomic,strong) void (^imageHandleCompletionBlock)(UIImage *image);
//保存一份代码,当图片处理完时调用; imageHandView.imageHandleCompletionBlock=^(UIImage *image){ self.drawView.image=image; };
// self.imageView.image=image; //重画drawView,执行block if (self.imageHandleCompletionBlock) { self.imageHandleCompletionBlock(image); }
#####5.源代码详细地址事件