iOS畫板實現第二波

#####畫板實現第二波 #####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上時對象

  • 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.源代碼詳細地址事件

相關文章
相關標籤/搜索