iOS—仿微信单击放大图片

编程入门 行业动态 更新时间:2024-10-23 18:22:30

iOS—仿微信<a href=https://www.elefans.com/category/jswz/34/1771345.html style=单击放大图片"/>

iOS—仿微信单击放大图片

//
//  ImageZoomView.h
//  手势缩放图片
//
//  Created by strong on 16/4/7.
//  Copyright © 2016年 LYX. All rights reserved.
//

#import <UIKit/UIKit.h>@interface ImageZoomView : UIView
- (instancetype)initWithFrame:(CGRect)frame andWithImage:(UIImageView *)imageview;
@end
//
//  ImageZoomView.m
//  手势缩放图片
//
//  Created by strong on 16/4/7.
//  Copyright © 2016年 LYX. All rights reserved.
//

#import "ImageZoomView.h"
static CGRect oldframe;
@interface ImageZoomView ()<UIScrollViewDelegate>@end@implementation ImageZoomView{UIScrollView *holderView;UIImageView *showImgView;BOOL isFirst;
}
- (instancetype)initWithFrame:(CGRect)frame andWithImage:(UIImageView *)imageview{if(self = [super initWithFrame:frame]){UIImage *image=imageview.image;holderView = [[UIScrollView alloc]initWithFrame:frame];holderView.backgroundColor=[UIColor blackColor];holderView.showsHorizontalScrollIndicator = NO; //水平holderView.showsVerticalScrollIndicator = NO; // 竖直holderView.scrollEnabled=YES;holderView.directionalLockEnabled = NO;holderView.bounces=NO;holderView.delegate=self;holderView.autoresizesSubviews=YES;holderView.maximumZoomScale=4;holderView.minimumZoomScale=1;[holderView setZoomScale:0.5 animated:NO];UIWindow *window = [UIApplication sharedApplication].keyWindow;oldframe = [imageview convertRect:imageview.bounds toView:window];[holderView setBackgroundColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0]];//此时视图不会显示[holderView setAlpha:0];//将所展示的imageView重新绘制showImgView = [[UIImageView alloc] initWithFrame:oldframe];[showImgView setImage:imageview.image];[showImgView setTag:0];[holderView addSubview:showImgView];[self addSubview:holderView];//动画放大所展示的ImageView[UIView animateWithDuration:0.4 animations:^{CGFloat y,width,height;y = ([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) * 0.5;//宽度为屏幕宽度width = [UIScreen mainScreen].bounds.size.width;//高度 根据图片宽高比设置height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;[showImgView setFrame:CGRectMake(0, y, width, height)];//重要! 将视图显示出来[holderView setAlpha:1];} completion:^(BOOL finished) {}];UITapGestureRecognizer *doubleTapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];[doubleTapGesture setNumberOfTapsRequired:2];[holderView addGestureRecognizer:doubleTapGesture];[self addSubview:holderView];UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];[singleTapGestureRecognizer setNumberOfTapsRequired:1];[holderView addGestureRecognizer:singleTapGestureRecognizer];[singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGesture];}return self;
}- (void)scrollViewDidZoom:(UIScrollView *)scrollView{CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;showImgView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,scrollView.contentSize.height * 0.5 + offsetY);
}-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{return showImgView;
}- (void)handleDoubleTap:(UIGestureRecognizer *)gesture{if(!isFirst){isFirst=YES;CGPoint pointInView = [gesture locationInView:holderView];CGFloat newZoomScale = holderView.zoomScale * 4.0f;newZoomScale = MIN(newZoomScale, holderView.maximumZoomScale);CGSize scrollViewSize =holderView.bounds.size;CGFloat w = scrollViewSize.width / newZoomScale;CGFloat h = scrollViewSize.height / newZoomScale;CGFloat x = pointInView.x - (w / 2.0f);CGFloat y = pointInView.y - (h / 2.0f);CGRect rectToZoomTo = CGRectMake(x, y, w, h);[holderView zoomToRect:rectToZoomTo animated:YES];}else{isFirst=NO;CGFloat newZoomScale = holderView.zoomScale / 4.0f;newZoomScale = MAX(newZoomScale, holderView.minimumZoomScale);[holderView setZoomScale:newZoomScale animated:YES];}}- (void)singleTap:(UITapGestureRecognizer*)tap{[UIView animateWithDuration:0.4 animations:^{[showImgView setFrame:oldframe];[holderView setAlpha:0];} completion:^(BOOL finished) {//完成后操作->将背景视图删掉
        [holderView removeFromSuperview];[self removeFromSuperview];}];}@end
 
//点击事件
ImageZoomView *img=[[ImageZoomView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN, HEIGHT_SCREEN) andWithImage:imgView];//当前视图UIWindow *window = [UIApplication sharedApplication].keyWindow;[window addSubview:img];

 

转载于:.html

更多推荐

iOS—仿微信单击放大图片

本文发布于:2024-02-27 02:15:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1704820.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   图片   iOS   仿微信

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!