注意图片点击要设置,把需要监听的view/imageView传进方法就行啦,如果是view的话就改一下最后一句添加手势那里
setUserInteractionEnabled 为yes
#pragma mark - 设置图片可以点击并监听
- (void)setImageClickWirhView:(UIView *)myUIView OrImageView:(UIImageView *)imageView{
//单指单击
UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleFingerEvent:)];
singleFingerOne.numberOfTouchesRequired = 1; //手指数
singleFingerOne.numberOfTapsRequired = 1; //tap次数
// singleFingerOne.delegate= self;
[imageView addGestureRecognizer:singleFingerOne];
}
处理点击事件,根据个人需求把要查看的图片放进去
#pragma mark - 处理单指事件
- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
{
if(sender.numberOfTapsRequired == 1) {
UITableViewCell *cell = [SeminarSonService handleCellSuperViewForIOSVersion:sender];
NSIndexPath *indexpath = [self.seminarSonTableView indexPathForCell:cell];
SeminarSonCell *seminarSonCell = (SeminarSonCell *)[_seminarSonTableView cellForRowAtIndexPath:indexpath];
//单指单击
[self showImageWithImageView:seminarSonCell.seminarSonImageView];
}
}
查看图片 ,注释有标明包括了监听手势了
#pragma mark - 显示照片
-(void)showImageWithImageView:(UIImageView *)photoImageView{
UIImage *image=photoImageView.image;
UIWindow *window=[UIApplication sharedApplication].keyWindow;
UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.oldframe = [photoImageView convertRect:photoImageView.bounds toView:window];
backgroundView.backgroundColor=[UIColor blackColor];
backgroundView.alpha=0;
UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.oldframe];
imageView.image=image;
imageView.tag=1;
[backgroundView addSubview:imageView];
[window addSubview:backgroundView];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImage:)];
[backgroundView addGestureRecognizer: tap];
[self addGestureRecognizerToView:imageView];
//如果处理的是图片,要
[imageView setUserInteractionEnabled:YES];
[imageView setMultipleTouchEnabled:YES];
[UIView animateWithDuration:0.3 animations:^{
imageView.frame=CGRectMake(0,
([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2,
[UIScreen mainScreen].bounds.size.width,
image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
backgroundView.alpha=1;
} completion:^(BOOL finished) {
}];
}
关闭查看
#pragma mark - 隐藏
- (void)hideImage:(UITapGestureRecognizer*)tap{
UIView *backgroundView=tap.view;
UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];
[UIView animateWithDuration:0.3 animations:^{
imageView.frame=self.oldframe;
backgroundView.alpha=0;
} completion:^(BOOL finished) {
[backgroundView removeFromSuperview];
}];
}
手势监听方法 ,包括了三种手势啦
// 添加所有的手势
- (void) addGestureRecognizerToView:(UIImageView *)view
{
// 旋转手势
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
[view addGestureRecognizer:rotationGestureRecognizer];
// 缩放手势
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
[view addGestureRecognizer:pinchGestureRecognizer];
// 移动手势
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
[view addGestureRecognizer:panGestureRecognizer];
}
各种手势实现
// 处理旋转手势
- (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer
{
UIView *view = rotationGestureRecognizer.view;
if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {
view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);
[rotationGestureRecognizer setRotation:0];
}
}
// 处理缩放手势
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
UIView *view = pinchGestureRecognizer.view;
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
pinchGestureRecognizer.scale = 1;
}
}
// 处理拖拉手势
- (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer
{
UIView *view = panGestureRecognizer.view;
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [panGestureRecognizer translationInView:view.superview];
[view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
[panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
}
}
好啦,以上方法是我筹集回来的,加上自己的项目代码,把自己项目代码替换就好啦
不懂得可以留言