#####畫板實現第二波 #####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.源代碼詳細地址事件