let scaleFactor = scrollView.bounds.width / imageView.bounds.width
scrollView.minimumZoomScale = scaleFactor
scrollView.zoomScale = scaleFactor
extension DetailViewController: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
}
对子视图的布局进行修改的时候 最好放在viewDidLayoutSubviews 或者 viewDidAppear里面
想把缩放的内容始终居中
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let scaleFactor = scrollView.bounds.width / imageView.bounds.width
scrollView.minimumZoomScale = scaleFactor
//会受当前minimumZoomScale 和 maximumZoomSCale 的值的现值 所以必须在min和max设置之后
scrollView.zoomScale = scaleFactor
}
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
if imageView.frame.height < scrollView.frame.height {
let offsetY = (scrollView.frame.height - imageView.frame.height) / 2
imageView.center = CGPoint(x: imageView.frame.width / 2, y: imageView.frame.height / 2 + offsetY)
}
}
当图片过长 无法全屏显示的时候 可以改变scrollView.contentInsetAdjustmentBehavior = .never
//滚动条的样式
scrollView.indicatorStyle = .default
//是否启动分页机制
scrollView.isPagingEnabled = false
//锁定
/*如果此属性为false,则允许在水平和垂直方向滚动。如果此属性为true,并且用户开始沿一个常规方向(水平或垂直)拖动,则滚动视图将禁用沿另一个方向的滚动。如果拖动方向是对角线,则滚动将不会被锁定,用户可以在拖动完成之前向任何方向拖动。默认值为false
*/
scrollView.isDirectionalLockEnabled = false
//反弹
scrollView.bounces = true
//
scrollView.alwaysBounceVertical = false
scrollView.alwaysBounceHorizontal = false
//缩放的时候的反弹
scrollView.bouncesZoom = true
scrollView.minimumZoomScale = 1
scrollView.maximumZoomScale = 1
scrollView.zoomScale = 0.2
scrollView.setZoomScale(0.2, animated: true)
//把视图缩放到指定大小
scrollView.zoom(to: CGRect(x: 1, y: 1, width: 50, height: 50), animated: true)
//拖拽的时候键盘也不消失
scrollView.keyboardDismissMode = .none
//
scrollView.isDragging
scrollView.isTracking
scrollView.isDecelerating//减速
scrollView.isZooming //是否在进行缩放
scrollView.isZoomBouncing//是否在进行缩放的回弹
scrollView.panGestureRecognizer
scrollView.pinchGestureRecognizer
//减速速率
scrollView.decelerationRate = .normal
//返回顶部 只限于当前Controller里面只有一个scrollview才有效果 多个的就没有效果
scrollView.scrollsToTop = true
RefreshControl
data.append("rrwew")
tableView.reloadData()
DispatchQueue.main.async {
self.tableView.refreshControl?.endRefreshing()
}
let refreshControl = UIRefreshControl()
refreshControl.tintColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
refreshControl.attributedTitle = NSAttributedString(string: "加载中", attributes: [.foregroundColor : UIColor(red: 1, green: 0.1491314173, blue: 0, alpha: 1)])
tableView.refreshControl = refreshControl
tableView.refreshControl?.addTarget(self, action: #selector(refresh), for: .valueChanged)
网友评论