IOS开发之——画板-照片(89),面试前必看的一本书书籍

本文介绍了如何在iOS开发中利用手势识别实现对UIImageView的长按、捏合和旋转操作,详细讲解了相关代码实现,包括添加手势识别器、处理手势状态变化以及实现图片的截图和传递。
摘要由CSDN通过智能技术生成

#import “HandleImageView.h”

#import “UIImage+Tool.h”

@interface HandleImageView()

@property (nonatomic,weak) UIImageView *imageView;

@end

@implementation HandleImageView

  • (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

//添加UIImageView

[self addImageView];

//添加手势

[self addGestureRecognizers];

}

return self;

}

-(void)addGestureRecognizers

{

//1.长按

UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

[_imageView addGestureRecognizer:longPress];

[self addPinch];

[self addRotation];

}

//长按方法

-(void)longPress:(UILongPressGestureRecognizer *)longPress

{

if(longPress.state==UIGestureRecognizerStateEnded)

{

[UIView animateWithDuration:0.5 animations:^{

_imageView.alpha=0.3;

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.5 animations:^{

_imageView.alpha=1;

} completion:^(BOOL finished) {

//1.截屏

UIImage *newImage=[UIImage imageWithCaptureView:self];

//2.把图片传给控制器

_block(newImage);

//3.把自己移除父控制器

[self removeFromSuperview];

}];

}];

}

}

#pragma mark-捏合

-(void)addPinch

{

UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];

// 设置代理的原因:想要同时支持多个手势

pinch.delegate = self;

[_imageView addGestureRecognizer:pinch];

}

-(void)pinch:(UIPinchGestureRecognizer *)pinch

{

_imageView.transform=CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);

//复位

pinch.scale=1;

}

// Simultaneous:同时

// 默认是不支持多个手势

// 当你使用一个手势的时候就会调用

  • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

return YES;

}

  • (void)addRotation

{

// rotation

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];

rotation.delegate = self;

[_imageView addGestureRecognizer:rotation];

}

  • (void)rotation:(UIRotationGestureRecognizer *)rotation

{

// _imagView.transform = CGAffineTransformMakeRotation(rotation.rotation);

_imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);

// 复位

rotation.rotation = 0;

}

-(void)setImage:(UIImage *)image

{

_image=image;

_imageView.image=image;

}

-(void)addImageView

{

UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.bounds];

imageView.userInteractionEnabled=YES;

_imageView=imageView;

[self addSubview:imageView];

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

  • (void)drawRect:(CGRect)rect {

// Drawing code

}

@en
d

erform custom drawing.

// An empty implementation adversely affects performance during animation.

  • (void)drawRect:(CGRect)rect {

// Drawing code

}

@en[外链图片转存中…(img-4JeIf7Uk-1643443621932)]
d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值