在scrollview中循环重置(swift3)

时间:2017-03-07 05:18:50

标签: ios swift loops uiscrollview xib

我的代码是一个包含两个页面和一个按钮的uiscrolview。问题是,如果我在视图控制器信息(中间图像)。然后按下按钮返回到滚动视图,它返回到第1页。因此,无论从信息幻灯片到滚动视图的内容,它总是返回到查看控制器1并且循环重置。如果我在vc 2上点击信息按钮,请转到信息幻灯片。如果我点击信息幻灯片上的按钮,我想返回到vc 2.我的代码如下。基本上,循环总是重置我想要修复它,如果可能的话。

page one

enter image description here

enter image description here

    import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet var pageControl: UIPageControl!
@IBOutlet var slideScrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    slideScrollView.delegate = self


    let slides:[slide] = createSlides()
    setupSlideScrollView(slides: slides)
    pageControl.numberOfPages = slides.count
    pageControl.currentPage = 0
    view.bringSubview(toFront: pageControl)



}
override var prefersStatusBarHidden: Bool {
    return true
}

func createSlides() -> [slide]{
    let slide1:slide = Bundle.main.loadNibNamed("slide", owner: self, options: nil)?.first as! slide
    slide1.label.text = "1"






    let slide2:slide = Bundle.main.loadNibNamed("slide", owner: self, options: nil)?.first as! slide
    slide2.label.text = "2"






    return [slide1,slide2 ]
}

func setupSlideScrollView(slides:[slide]) {
    slideScrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
    slideScrollView.contentSize = CGSize(width: view.frame.width * CGFloat(slides.count), height: view.frame.height)

    for i in 0..<slides.count{
        slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
        slideScrollView.addSubview(slides[i])
        slideScrollView.isPagingEnabled = true

    }
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
    pageControl.currentPage = Int(pageIndex)

}}

1 个答案:

答案 0 :(得分:0)

这听起来像ViewWillAppearViewWillAppear问题,请检查您是否在这些功能中设置了任何内容。