一 问题描述
做照片放大缩小功能,添加UITapGestureRecognizer 事件后,点击图片没有事件相应。
二 解决方法
//设置imageView setUserInteractionEnable 属性
[self.imageView setUserInteractionEnabled:YES];
三 图片放大缩小代码
//=====声明参数部分=======
/**阴影按钮*/
@property (nonatomic, weak) UIButton *cover;
/**记录头像原始尺寸*/
@property (nonatomic, assign)CGRect oldIconBtnFrame;
//=====设置 UITapGestureRecognizer 部分=======
[_showFrontImageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapping)];
[singleTap setNumberOfTapsRequired:1];
[_showFrontImageView addGestureRecognizer:singleTap];
#pragma mark ---------------------图片放大--------------------
-(void)singleTapping
{
if (self.cover!=nil) {
[self smallImage];
}else{
[self bigImage];
}
}
#pragma mark 大图
// 1.设置图片背景
-(void)bigImage{
UIButton *cover = [[UIButton alloc]init];
cover.frame= CGRectMake(0, 0, ScreenWidth, ScreenHeight);
[cover setBackgroundColor:[UIColor blackColor]];
// 先添加在赋值,因为该属性是weak 有可能释放
[self.view addSubview:cover];
self.cover=cover;
cover.alpha=0.0;
[self.view bringSubviewToFront:[_showFrontImageView superview]];
[[_showFrontImageView superview] bringSubviewToFront:_showFrontImageView];
// 保存旧值
self.oldIconBtnFrame = _showFrontImageView.frame;
// 设置监听
[cover addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
// 2.设置图片位置
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
CGRect viewRect = self.view.frame;
// CGFloat iconW = viewRect.size.width;
CGFloat iconW =ScreenWidth;
CGFloat iconH =240;
// CGFloat iconH = self.bagImageView.image.size.height;
CGFloat iconX = 0;
CGFloat iconY = (viewRect.size.height-iconH)*0.5;
_showFrontImageView.frame = CGRectMake(iconX, iconY, iconW, iconH);
cover.alpha = 1.0;
[UIView commitAnimations];
}
#pragma mark 缩小图片
-(void)smallImage
{
[UIView animateWithDuration:1 animations:^{
[[self navigationController] setNavigationBarHidden:NO animated:NO];
_showFrontImageView.frame=self.oldIconBtnFrame;
self.cover.alpha = 0;
}completion:^(BOOL finished) {
[self.cover removeFromSuperview];
self.cover = nil;
}];
}