把图片绘制上去,可以理解为就是绘制一条线
这里的代码就是对上一篇文章的补充,可以拖动图片,做一些操作后在把图片画上去,而不是跟上一篇文章的
代码一样直接把图片画上去
#import <UIKit/UIKit.h>
//定义协议
@class HandleView;
@protocol HandleViewDelegate <NSObject>
- (void)handleView:(HandleView *)handleV newImage:(UIImage *)newImage;
@end
@interface HandleView : UIView
@property (nonatomic, strong) UIImage *image;
//定义代理属性
@property (nonatomic, weak) id<HandleViewDelegate> delegate;
@end
#import "HandleView.h"
@interface HandleView ()<UIGestureRecognizerDelegate>
@property (nonatomic, weak) UIImageView *imageV;
@end
@implementation HandleView
- (UIImageView *)imageV {
if (_imageV == nil) {
UIImageView *imageV = [[UIImageView alloc] init];
imageV.frame = self.bounds;
imageV.userInteractionEnabled = YES;
[self addSubview:imageV];
_imageV = imageV;
//添加手势
[self addGes];
}
return _imageV;
}
//添加手势
-(void)addGes{
// pan
// 拖拽手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(pan:)];
[self.imageV addGestureRecognizer:pan];
// pinch
// 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[self.imageV addGestureRecognizer:pinch];
//添加旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.imageV addGestureRecognizer:rotation];
// 长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.imageV addGestureRecognizer:longPress];
}
//捏合的时候调用.
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
self.imageV.transform = CGAffineTransformScale( self.imageV.transform, pinch.scale, pinch.scale);
// 复位
pinch.scale = 1;
}
//旋转的时候调用
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
// 旋转图片
self.imageV.transform = CGAffineTransformRotate( self.imageV.transform, rotation.rotation);
// 复位,只要想相对于上一次旋转就复位
rotation.rotation = 0;
}
//长按的时候调用
// 什么时候调用:长按的时候调用,而且只要手指不离开,拖动的时候会一直调用,手指抬起的时候也会调用
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
//设置透明度
[UIView animateWithDuration:0.25 animations:^{
self.imageV.alpha = 0;
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.25 animations:^{
self.imageV.alpha = 1;
}completion:^(BOOL finished) {
//对当前的View截屏
//1.开启一个位图上下文
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.layer renderInContext:ctx];
//从上下文当中生成一张新的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//调用代理方法
if ([self.delegate respondsToSelector:@selector(handleView:newImage:)]) {
[self.delegate handleView:self newImage:newImage];
}
//移除当前View
[self removeFromSuperview];
//关闭上下文
UIGraphicsEndImageContext();
}];
}];
}
}
//拖动的时候调用
- (void)pan:(UIPanGestureRecognizer *)pan{
CGPoint transP = [pan translationInView:pan.view];
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, transP.x, transP.y);
//复位
[pan setTranslation:CGPointZero inView:pan.view];
}
//能够同时支持多个手势
-(BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (void)setImage:(UIImage *)image {
_image = image;
self.imageV.image = image;
}
@end