缩放在滚动视图中不起作用
Posted
技术标签:
【中文标题】缩放在滚动视图中不起作用【英文标题】:Zoom not working in scroll view 【发布时间】:2017-03-02 23:07:34 【问题描述】:我在 swift 3 中有一个滚动视图。缩放不起作用。我已经设置了滚动视图并在里面放了一个图像视图。我已将 self 设置为委托并实现 viewForZoomingInScrollView。请看一下。谢谢。
edit:我之前没有提到的另一件事是,这是导航控制器中的视图。会不会有影响?
class FlowDiagramViewController: UIViewController, UIScrollViewDelegate
var scrollView: UIScrollView!
var imageView:UIImageView! = UIImageView()
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
let image = UIImage(named: "flowNewRotated1242x2208.png")
imageView = UIImageView(image: image)
setupPictureWithZoom()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func setupPictureWithZoom()
// http://***.com/questions/30014241/uiimageview-pinch-zoom-swift
imageView.frame = CGRect(x: 0, y: 0, width: 1242, height: 2208)
// imageView.transform = imageView.transform.rotated(by: CGFloat(M_PI_2))
imageView!.layer.cornerRadius = 11.0
imageView!.clipsToBounds = false
scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
scrollView.minimumZoomScale = 0.25
scrollView.maximumZoomScale = 5.0
scrollView.delegate = self
scrollView.backgroundColor = UIColor(red: 90, green: 90, blue: 90, alpha: 0.90)
scrollView.alwaysBounceVertical = false
scrollView.alwaysBounceHorizontal = false
scrollView.showsVerticalScrollIndicator = true
scrollView.flashScrollIndicators()
scrollView.contentSize = CGSize(width: 1242, height: 2208)
scrollView.addSubview(imageView!)
view.addSubview(scrollView!)
scrollView.setZoomScale(0.25, animated: true)
func viewForZoomingInScrollView(scrollView: UIScrollView!) -> UIView!
return imageView
我已经尝试在故事板和代码中定义滚动视图。
【问题讨论】:
【参考方案1】:这是一个 swift 3 问题。
新的函数原型如下:
Swift 3 版本
func viewForZooming(in scrollView: UIScrollView) -> UIView?
return imageView
Swift 2 版本
// func viewForZoomingInScrollView(scrollView: UIScrollView!) -> UIView!
// return imageView
//
【讨论】:
移动代码时的常见问题,很难找到。点赞!【参考方案2】:这两行必须在viewDidLoad中:
scrollView.minimumZoomScale = 0.25
scrollView.maximumZoomScale = 5.0
【讨论】:
【参考方案3】:尝试像下面这样更改内容大小器,它就像一个魅力
scrollView.contentSize = imageView.frame.size
【讨论】:
以上是关于缩放在滚动视图中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Recyclerview 滚动在嵌套滚动视图中的片段中不起作用