2016-07-29 222 views
1

我試圖實現與網頁控制滾動視圖來顯示使用斯威夫特,得到這樣的輸出在iOS的圖像顯示圖像這裏查看是另一個視圖控制器的一部分。 我GOOGLE了,但沒有得到所需的輸出幫助。 任何人都可以幫我嗎?滾動視圖中的iOS

+0

澄清你的問題吧。此外,請提供代碼示例,其中介紹瞭如何嘗試實現所請求的功能。 –

回答

1

答案有點晚。因此,可能對別人有所幫助:

斯威夫特3X:

首先,給這樣的代表團:

class YOUR_VIEW_CONTROLLER: UIViewController, UIScrollViewDelegate 

現在連接的UIScrollViewUIPageControl像這樣的店鋪:

@IBOutlet weak var scrollView: UIScrollView! 
@IBOutlet weak var pageControl: UIPageControl! 

注:我用c olours陣列。您可以改爲使用圖像數組。

var colors:[UIColor] = [UIColor.red, UIColor.blue, UIColor.green, UIColor.yellow] 
var frame: CGRect = CGRect(x:0, y:0, width:0, height:0) 

現在只需複製並粘貼下面的代碼在類:

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    configurePageControl() 

    self.view.bringSubview(toFront: pageControl) 
    scrollView.delegate = self 
    self.view.addSubview(scrollView) 
    for index in 0..<4 { 

     frame.origin.x = self.scrollView.frame.size.width * CGFloat(index) 
     frame.size = self.scrollView.frame.size 

     let subView = UIView(frame: frame) 
     subView.backgroundColor = colors[index] 
     self.scrollView.addSubview(subView) 
    } 

    self.scrollView.isPagingEnabled = true 


    self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4,height: self.scrollView.frame.size.height) 

    pageControl.addTarget(self, action: #selector(self.changePage(sender:)), for: UIControlEvents.valueChanged) 

} 

func configurePageControl() { 
    // The total number of pages that are available is based on how many available colors we have. 
    self.pageControl.layer.zPosition = 1 

    self.pageControl.numberOfPages = colors.count 
    self.pageControl.currentPage = 0 
    self.pageControl.tintColor = UIColor.red 
    self.pageControl.pageIndicatorTintColor = UIColor.black 
    self.pageControl.currentPageIndicatorTintColor = UIColor.green 
    self.view.addSubview(pageControl) 

} 

// MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL 
func changePage(sender: AnyObject) ->() { 
    let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width 
    scrollView.setContentOffset(CGPoint(x:x, y:0), animated: true) 
} 

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 

    let pageNumber = round(scrollView.contentOffset.x/scrollView.frame.size.width) 
    pageControl.currentPage = Int(pageNumber) 
}