我想在几个不同的ViewControllers上设置几个不同的Scrollviews

时间:2016-12-14 19:11:18

标签: ios swift uiscrollview scrollview

我无法将scrollview添加到几个不同的View控制器 - 到目前为止,我已经在视图控制器的前端,但是,我还没有弄清楚如何让它们工作(我是初学者)

这是代码

class ViewController: UIViewController, UIScrollViewDelegate {

     @IBOutlet weak var firstscrollView: UIScrollView!
     @IBOutlet weak var firstimageview: UIImageView!
     @IBOutlet weak var secondscrollView: UIScrollView!
     @IBOutlet weak var secondimageView: UIImageView!

     override func viewDidLoad() {

         super.viewDidLoad()

         self.firstscrollView.minimumZoomScale = 1.0
         self.firstscrollView.maximumZoomScale = 6.0

         self.secondscrollView.minimumZoomScale = 1.0
         self.secondscrollView.maximumZoomScale = 6.0
     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }

     func viewForZooming(in firstscrollView: UIScrollView) -> UIView? {

         return self.firstimageview
         return self.secondimageView
     }
}

1 个答案:

答案 0 :(得分:0)

首先delegateUIScrollViewfirstscrollView.delegate = self secondscrollView.delegate = self分配viewDidLoad

然后在委托方法中检查UIScrollView这样的

func viewForZooming(in scrollView: UIScrollView) -> UIView? {

    if scrollView == firstscrollView {
        return firstimageview
    } else if scrollView == secondscrollView {
        return secondscrollView
    }
    return nil
}

我希望这会有所帮助。