简体   繁体   English

使用页面控件滚动视图以在iOS中显示图像

[英]Scroll view with page control to display images in iOS

I am trying to implement a scroll view with page control to display images in iOS using Swift, to get an output like this: 我正在尝试使用页面控件实现滚动视图以使用Swift在iOS中显示图像,以获取如下输出:

所需输出

The scroll view here is a part of another view controller. 这里的滚动视图是另一个视图控制器的一部分。 I googled but got no help to get the required output. 我用谷歌搜索,但没有得到所需输出的帮助。 Can anyone please help me? 谁能帮帮我吗?

The answer is a bit late. 答案有点晚了。 So, may be helpful for someone else: 因此,可能对其他人有帮助:

Swift 3x: Swift 3x:

First of all, give the delegation like this: 首先,给这样的代表团:

class YOUR_VIEW_CONTROLLER: UIViewController, UIScrollViewDelegate 

Now connect the outlets of UIScrollView and UIPageControl like this: 现在像这样连接UIScrollViewUIPageControl的出口:

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

NOTE:I'm using colours array. 注意:我正在使用颜色数组。 You can use image array instead. 您可以改用图像数组。

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

Now just copy and paste the below code in your class: 现在,只需将以下代码复制并粘贴到您的课程中:

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM