这让我疯了!
我将UIScrollView
基于http://koreyhinton.com/blog/uiscrollview-crud.html以使其成为程序设计,因此在我的滚动视图中设置了容器视图。但它平移,但不会放大。
var scrollView: UIScrollView!
var containerView : UIView!
override func viewDidLoad() {
super.viewDidLoad()
let width:CGFloat = self.view.bounds.width
let height:CGFloat = self.view.bounds.height
scrollView = UIScrollView()
scrollView.delegate = self
scrollView.minimumZoomScale = 0.5
scrollView.maximumZoomScale = 2.0
scrollView.contentSize = CGSize(width: width*2, height: 2000)
scrollView.backgroundColor = .red
containerView = UIView()
scrollView.addSubview(containerView)
view.addSubview(scrollView)
containerView.isUserInteractionEnabled = true
scrollView.isUserInteractionEnabled = true
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = view.bounds
containerView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
}
override func viewWillLayoutSubviews() {
//I create a view called "theView"
containerView.addSubview(theView)
}
以下功能不会在任何时候触发
func update(zoomScale: CGFloat, offSet: CGPoint) {
scrollView.zoomScale = zoomScale
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return containerView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
NSLog("scroll")
}
答案 0 :(得分:1)
你真的不需要为此目的做那么多代码。 您可以在故事板中设置scrollView所需的全部内容,并且只需要要放大的视图的插座。 设置控制器,添加scrollview,将delegate属性连接到视图控制器,在IB中添加缩放视图作为子视图。
在类中,将控制器符合UIScrollViewDelegate,并使用viewForZooming,一种scrollView委托方法。
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var zoomer: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return zoomer
}
}
P.S。使用更新的学习资源,Ray Wenderlich,AppCoda等 - 这是一个充满良好资源的大型网站,Swift正在不断变化。