当前位置: 首页 > news >正文

swift基础学习(九)

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

  • scrollview代理方法
  • 手势

####2.效果图

####3.代码

  • 自定义页面的代码
import UIKit

class  JdryZoomView: UIControl, UIScrollViewDelegate {

fileprivate let screenWidth: CGFloat = UIScreen.main.bounds.width
fileprivate let screenHeight: CGFloat = UIScreen.main.bounds.height
fileprivate let maxScale: CGFloat = 3.0 // 最大比例
fileprivate let minScale: CGFloat = 1.0 // 最小比例
fileprivate let animDuration: TimeInterval = 0.2 //动画时间

open var originFrame: CGRect = CGRect.zero
open var image: UIImage? {
didSet {
if let _image = image{
if self.originFrame == CGRect.zero {
let imageViewH = _image.size.height / (image?.size.width)! * screenWidth
self.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.0
self.scrollView = UIScrollView()
self.scrollView?.showsVerticalScrollIndicator = false
self.scrollView?.showsHorizontalScrollIndicator = false
self.scrollView?.maximumZoomScale = maxScale
self.scrollView?.minimumZoomScale = minScale
self.scrollView?.delegate = self
self.scrollView?.backgroundColor = UIColor.black
self.scrollView?.frame = self.bounds
self.addSubview(self.scrollView!)

//        添加手势
//        1.点击手势
let  singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTapClick(_:)))
singleTap.numberOfTapsRequired = 1
self.scrollView?.addGestureRecognizer(singleTap)

//        2.双击手势
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTapClick(tap:)))
doubleTap.numberOfTapsRequired = 2
self.scrollView?.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)

//        初始化 UIImageView
self.imageView = UIImageView()
self.scrollView?.addSubview(self.imageView!)
}

open func show() {
self.scrollView?.backgroundColor = UIColor.black
UIView.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.originFrame
self.scrollView?.backgroundColor = UIColor.clear
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal

}) { (finished) in

self.alpha = 0
self.scrollView?.backgroundColor = UIColor.black
self.originFrame = CGRect.zero
self.scale = self.minScale
self.removeFromSuperview()

}
}

//    双击手势
func doubleTapClick(tap: UITapGestureRecognizer) {
self.touchX = tap.location(in: tap.view).x
self.touchY = tap.location(in: tap.view).y

if self.scale > 1.0 {
self.scale = 1.0
self.scrollView?.setZoomScale(self.scale, animated: true)
} else {
self.scale = maxScale
self.isDoubleTaping = true
self.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.x
var centerY = scrollView.center.y
centerX = scrollView.contentSize.width > scrollView.frame.size.width ? scrollView.contentSize.width / 2 : centerX
centerY = scrollView.contentSize.height > scrollView.frame.size.height ?scrollView.contentSize.height / 2 : centerY

self.imageView?.center = CGPoint(x: centerX, y: centerY)

// ****************双击放大图片关键代码*******************

if isDoubleTaping {
let contentOffset = self.scrollView?.contentOffset
let center = self.center
let offsetX = center.x - self.touchX
//            let offsetY = center.y - self.touchY
self.scrollView?.contentOffset = CGPoint(x: (contentOffset?.x)! - offsetX * 2.2, y: (contentOffset?.y)!)
}

// ****************************************************

}

deinit {
print("释放")
}
}

复制代码
  • 主控制器的代码

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

fileprivate let screenWidth: CGFloat = UIScreen.main.bounds.width
fileprivate let scrollHeight: CGFloat = UIScreen.main.bounds.height


@IBOutlet weak var imageView: UIImageView!

var zoomView: JdryZoomView?


override func viewDidLoad() {
super.viewDidLoad()
self.imageView.isUserInteractionEnabled = true
self.imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapClick(_:))))
}

func tapClick(_ tap: UITapGestureRecognizer) {
if zoomView == nil {
let frame = UIScreen.main.bounds
zoomView = JdryZoomView(frame: frame)

}

if zoomView?.superview == nil {
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
UIApplication.shared.keyWindow?.addSubview(zoomView!)
}

let  imageviewFram = self.view.convert(self.imageView.frame, to: UIApplication.shared.keyWindow)
zoomView?.originFrame = imageviewFram
zoomView?.image = self.imageView.image
zoomView?.show()
}

}

复制代码

相关文章:

  • MySQL Workbench关键字转成小写设置
  • iOS-关于autoresizingMask在7.x及以下版本的一个bug
  • XV Open Cup named after E.V. Pankratiev. GP of Three Capitals
  • View 和Activity生命周期
  • Swift 2 0 如何替代 pch
  • 使用阿里云Maven镜像的正确姿势
  • 高德地图系列web篇——目的地公交导航
  • iOS 错误提示 [NSTaggedPointerString countByEnumeratingWithState objects
  • Android Fragment 从源码的角度去解析(上)
  • 数据结构中的各种排序方法-JS实现
  • Asp.net缓存简介
  • Android鬼点子 使用Kotlin编写的颜色选择器
  • 合唱队形
  • 复选框提交功能
  • [cb]UIGrid+UIStretch的自适应
  • $translatePartialLoader加载失败及解决方式
  • conda常用的命令
  • Git的一些常用操作
  • JavaScript-Array类型
  • Java教程_软件开发基础
  • PAT A1092
  • python学习笔记-类对象的信息
  • react 代码优化(一) ——事件处理
  • 大数据与云计算学习:数据分析(二)
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 驱动程序原理
  • 时间复杂度与空间复杂度分析
  • 使用 QuickBI 搭建酷炫可视化分析
  • 小程序01:wepy框架整合iview webapp UI
  • Prometheus VS InfluxDB
  • 国内开源镜像站点
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • 移动端高清、多屏适配方案
  • #pragam once 和 #ifndef 预编译头
  • $.ajax中的eval及dataType
  • (floyd+补集) poj 3275
  • (Matlab)使用竞争神经网络实现数据聚类
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (六)vue-router+UI组件库
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (转)Linux下编译安装log4cxx
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • (转)树状数组
  • (转载)OpenStack Hacker养成指南
  • .chm格式文件如何阅读
  • .NET Core 通过 Ef Core 操作 Mysql
  • .NET Framework杂记
  • .Net Remoting常用部署结构
  • .net 按比例显示图片的缩略图
  • .NET 使用 XPath 来读写 XML 文件
  • .NET/C# 在 64 位进程中读取 32 位进程重定向后的注册表
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • [Android]使用Retrofit进行网络请求