当前位置: 首页 > 面试题库 >

如何使用swift使用页面控件创建滚动视图?

阎庆
2023-03-14
问题内容

我正在尝试创建具有多个视图的页面视图控制器。我想要一个简单的代码示例,它将使用 UIPageViewController


问题答案:

import UIKit

class DummyVC: UIViewController, UIScrollViewDelegate {

    let scrollView = UIScrollView(frame: CGRect(x:0, y:0, width:320,height: 300))
    var colors:[UIColor] = [UIColor.red, UIColor.blue, UIColor.green, UIColor.yellow]
    var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)
    var pageControl : UIPageControl = UIPageControl(frame: CGRect(x:50,y: 300, width:200, height:50))

    override func viewDidLoad() {
        super.viewDidLoad()

        configurePageControl()

        scrollView.delegate = self
        scrollView.isPagingEnabled = true

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

}


 类似资料:
  • 本文向大家介绍如何使用JavaFX创建滚动窗格?,包括了如何使用JavaFX创建滚动窗格?的使用技巧和注意事项,需要的朋友参考一下 滚动窗格包含一个UI元素,并提供它的可滚动视图。在JavaFX中,可以通过实例化javafx.scene.control.ScrollPane类来创建滚动窗格。您可以使用setContent()方法将内容设置到滚动窗格。 要将滚动窗格添加到节点- 实例化ScrollP

  • 我为Android原生应用程序编写了自动测试,并被迫解决了滚动问题。 我需要滚动Android应用程序页面,并谷歌下一个方法:http://appium.io/docs/en/commands/interactions/touch/scroll/ 但在第一行我得到了一个例外“java.lang.ClassCastException:io.appium.java_client.android.And

  • 用于在地图上生成点的数据, 我不知道前面该怎么做,请大家帮帮我。

  • 本文向大家介绍如何使用CSS创建自定义滚动条?,包括了如何使用CSS创建自定义滚动条?的使用技巧和注意事项,需要的朋友参考一下 要使用CSS创建自定义滚动条,代码如下- 示例

  • 问题内容: 有没有一种方法可以使用JavaScript / jQuery控制浏览器滚动? 当我向下滚动页面一半,然后触发重新加载时,我希望页面进入顶部,但它会尝试找到最后一个滚动位置。所以我这样做: 但是没有运气。 编辑 : 因此,当页面加载后,我打电话给您时,您的两个答案都有效。但是,如果我只是在页面上刷新,则看起来浏览器会在事件发生后计算并滚动到其旧的滚动位置(我也测试了body onload

  • 问题内容: 我正在尝试创建带有分页的UICollectionView,并且每个项目的最大宽度为250点,我已经设法创建了它,但是我遇到了两个问题:第一个项目开始时不正确,但是开始时有更多空间当我尝试滑动时,总会有一些东西无法让我平稳滑动。 看起来是这样的: 影片连结 这是我的代码: CenterCellCollectionFlowLayout.swift MainViewController.sw