博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift基础学习(九)
阅读量:7079 次
发布时间:2019-06-28

本文共 5553 字,大约阅读时间需要 18 分钟。

####1. 所用到的知识点

  • scrollview代理方法
  • 手势

####2.效果图

####3.代码

  • 自定义页面的代码
import UIKitclass  JdryZoomView: UIControl, UIScrollViewDelegate {fileprivate let screenWidth: CGFloat = UIScreen.main.bounds.widthfileprivate let screenHeight: CGFloat = UIScreen.main.bounds.heightfileprivate let maxScale: CGFloat = 3.0 // 最大比例fileprivate let minScale: CGFloat = 1.0 // 最小比例fileprivate let animDuration: TimeInterval = 0.2 //动画时间open var originFrame: CGRect = CGRect.zeroopen var image: UIImage? {didSet {if let _image = image{if self.originFrame == CGRect.zero {let imageViewH = _image.size.height / (image?.size.width)! * screenWidthself.imageView?.bounds = CGRect(x: 0, y: 0, width: screenWidth, height: imageViewH)self.imageView?.center = (scrollView?.center)!} else {self.imageView?.frame = self.originFrame}self.imageView?.image = image}}}fileprivate var scrollView: UIScrollView?fileprivate var imageView: UIImageView?fileprivate var scale: CGFloat = 1.0 // 当前缩放比例fileprivate var touchX: CGFloat = 0.0 //双击点的X坐标fileprivate var touchY: CGFloat = 0.0 //双击点Y的坐标fileprivate var isDoubleTaping: Bool = false // 是否双击override init(frame: CGRect) {super.init(frame: frame)initAllView()}required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}fileprivate func initAllView() {self.alpha = 0.0self.scrollView = UIScrollView()self.scrollView?.showsVerticalScrollIndicator = falseself.scrollView?.showsHorizontalScrollIndicator = falseself.scrollView?.maximumZoomScale = maxScaleself.scrollView?.minimumZoomScale = minScaleself.scrollView?.delegate = selfself.scrollView?.backgroundColor = UIColor.blackself.scrollView?.frame = self.boundsself.addSubview(self.scrollView!)//        添加手势//        1.点击手势let  singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTapClick(_:)))singleTap.numberOfTapsRequired = 1self.scrollView?.addGestureRecognizer(singleTap)//        2.双击手势let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTapClick(tap:)))doubleTap.numberOfTapsRequired = 2self.scrollView?.addGestureRecognizer(doubleTap)singleTap.require(toFail: doubleTap)//        初始化 UIImageViewself.imageView = UIImageView()self.scrollView?.addSubview(self.imageView!)}open func show() {self.scrollView?.backgroundColor = UIColor.blackUIView.animate(withDuration: self.animDuration, delay: 0, options: UIViewAnimationOptions.allowUserInteraction, animations: {self.imageView?.bounds = CGRect(x: 0, y: 0, width: self.screenWidth, height: (self.image?.size.height)! / (self.image?.size.width)! * self.screenWidth)self.alpha = 1.0}) { (finished) in}}//    单击手势func singleTapClick(_ tap: UITapGestureRecognizer) {self.scrollView?.setZoomScale(self.minScale, animated: false)UIView.animate(withDuration: self.animDuration, delay: 0, options: .allowUserInteraction, animations: {self.imageView?.frame = self.originFrameself.scrollView?.backgroundColor = UIColor.clearUIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal}) { (finished) inself.alpha = 0self.scrollView?.backgroundColor = UIColor.blackself.originFrame = CGRect.zeroself.scale = self.minScaleself.removeFromSuperview()}}//    双击手势func doubleTapClick(tap: UITapGestureRecognizer) {self.touchX = tap.location(in: tap.view).xself.touchY = tap.location(in: tap.view).yif self.scale > 1.0 {self.scale = 1.0self.scrollView?.setZoomScale(self.scale, animated: true)} else {self.scale = maxScaleself.isDoubleTaping = trueself.scrollView?.setZoomScale(maxScale, animated: true)}self.isDoubleTaping = false}//    scrollview 代理func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {self.scale = scale}func viewForZooming(in scrollView: UIScrollView) -> UIView? {return self.imageView!}open func scrollViewDidZoom(_ scrollView: UIScrollView) {//当捏或移动时,需要对center重新定义以达到正确显示位置var centerX = scrollView.center.xvar centerY = scrollView.center.ycenterX = scrollView.contentSize.width > scrollView.frame.size.width ? scrollView.contentSize.width / 2 : centerXcenterY = scrollView.contentSize.height > scrollView.frame.size.height ?scrollView.contentSize.height / 2 : centerYself.imageView?.center = CGPoint(x: centerX, y: centerY)// ****************双击放大图片关键代码*******************if isDoubleTaping {let contentOffset = self.scrollView?.contentOffsetlet center = self.centerlet offsetX = center.x - self.touchX//            let offsetY = center.y - self.touchYself.scrollView?.contentOffset = CGPoint(x: (contentOffset?.x)! - offsetX * 2.2, y: (contentOffset?.y)!)}// ****************************************************}deinit {print("释放")}}复制代码
  • 主控制器的代码
import UIKitclass ViewController: UIViewController, UIScrollViewDelegate {fileprivate let screenWidth: CGFloat = UIScreen.main.bounds.widthfileprivate let scrollHeight: CGFloat = UIScreen.main.bounds.height@IBOutlet weak var imageView: UIImageView!var zoomView: JdryZoomView?override func viewDidLoad() {super.viewDidLoad()self.imageView.isUserInteractionEnabled = trueself.imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapClick(_:))))}func tapClick(_ tap: UITapGestureRecognizer) {if zoomView == nil {let frame = UIScreen.main.boundszoomView = JdryZoomView(frame: frame)}if zoomView?.superview == nil {UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBarUIApplication.shared.keyWindow?.addSubview(zoomView!)}let  imageviewFram = self.view.convert(self.imageView.frame, to: UIApplication.shared.keyWindow)zoomView?.originFrame = imageviewFramzoomView?.image = self.imageView.imagezoomView?.show()}}复制代码

转载地址:http://ipcml.baihongyu.com/

你可能感兴趣的文章
asp.net跳出iframe结构转向登录
查看>>
QTTabBar
查看>>
MODBUS协议整理——功能码简述
查看>>
eclipse里maven项目An error occurred while filtering resources解决办法
查看>>
MySQL导入SQL文件及常用命令
查看>>
Can't locate find.pl in @INC (@INC contains: /etc/perl xxxx) at perlpath.pl line 7.
查看>>
c#(.Net)解析xml
查看>>
调整Virtual Box硬盘大小
查看>>
阻塞和非阻塞,同步和异步 总结【转】
查看>>
社会实体经济不断下滑的个人思考
查看>>
Pytorch多GPU并行处理
查看>>
【定制Android系统】Android O 在ROM中添加自己的 so 库(1)——Android.mk 与 Android.bp 的区别【转】...
查看>>
C#中把货币、日期转换成中文大写
查看>>
最近累死人的校园招聘20110928
查看>>
数字信号处理实验(零)—— 一维声音信号处理和二维图像处理
查看>>
HTTP请求:GET与POST方法的区别
查看>>
使用CDN对动态网站内容加速有效果吗
查看>>
iOS -view横向变成竖向
查看>>
控件自定义
查看>>
关于INTRAWEB ISAPI DLL发布
查看>>