画板实现第二波
1.将对图片添加一系列手势操作,原有实现不能实现
- 以前详细说明个六种手势在控件上的操作(手势只能添加在控件上)
- 对原有代码进行改造
1.1添加一个图片处理的VIEW
-(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中一些设置
#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.添加各种手势
-(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,还有就是控件的父控件就不能响应
-(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上时
- 1.self.superView可以获得,再强转,耦合
- 2.使用block:在ImageHandleView.h中定义,在viewControoler.m中写好要执行的代码(因国在viewControoler中可以获得drawView对象),在图片处理完时执行此block(在imageHandleView.m执行)
- 代码依从2所说来写(具体代码请看github)
/** 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.源代码详细地址