4.

遍历数组的一种方法

//遍历数组,同时获得index
        for (index,value) in images.enumerated() {
            let imageView = UIImageView(frame: CGRect(x: YHWidth*CGFloat(index), y: 0, width: YHWidth, height: YHHeight))
            imageView.image = UIImage(named: value)
            //限制边界
            imageView.clipsToBounds = true
            imageView.contentMode = .scaleAspectFill
            scrollBG.addSubview(imageView)
        }

_

待编辑

逻辑比较要转换成相同类型

 let number = Int(round(scrollBG.contentOffset.x/YHWidth))
        /*由于swift是类型安全的,所以通过逻辑比较时,需要两边的类型相同,不同需要转换一下类型*/
        if  number >= 0 && number <= 2 && number != currentPage {
            currentPage = number
            pageControl.currentPage = currentPage
        }

<li><h1>学习代码:<h1></li>

ViewController.swift

import UIKit

let YHRect = UIScreen.main.bounds
let YHHeight = YHRect.size.height
let YHWidth = YHRect.size.width

class ViewController: UIViewController, UIScrollViewDelegate {
    
    let scrollBG = UIScrollView(frame: YHRect)
    let images = ["first","second","three"]
    let pageControl = UIPageControl(frame: CGRect(x: 0, y: YHHeight-30, width: YHWidth, height: 20))
    var currentPage = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
    }
    override var prefersStatusBarHidden: Bool {
        return true
    }
    func setupView() {
        //遍历数组,同时获得index
        for (index,value) in images.enumerated() {
            let imageView = UIImageView(frame: CGRect(x: YHWidth*CGFloat(index), y: 0, width: YHWidth, height: YHHeight))
            imageView.image = UIImage(named: value)
            //限制边界
            imageView.clipsToBounds = true
            imageView.contentMode = .scaleAspectFill
            scrollBG.addSubview(imageView)
        }
        scrollBG.delegate = self
        scrollBG.isPagingEnabled = true
        scrollBG.contentSize = CGSize(width: YHWidth*CGFloat(images.count), height: YHHeight)
        
        pageControl.currentPage = currentPage
        pageControl.numberOfPages = 3
        pageControl.isEnabled = false
        pageControl.pageIndicatorTintColor = .white
        pageControl.currentPageIndicatorTintColor = .gray
        
        view.addSubview(scrollBG)
        view.addSubview(pageControl)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let number = Int(round(scrollBG.contentOffset.x/YHWidth))
        /*由于swift是类型安全的,所以通过逻辑比较时,需要两边的类型相同,不同需要转换一下类型*/
        if  number >= 0 && number <= 2 && number != currentPage {
            currentPage = number
            pageControl.currentPage = currentPage
        }
    }

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


}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 136.泛型 泛型代码让你可以写出灵活,可重用的函数和类型,它们可以使用任何类型,受你定义的需求的约束。你可以写出...
    无沣阅读 5,497评论 0 4
  • 132.转换错误成可选值 通过转换错误成一个可选值,你可以使用 try? 来处理错误。当执行try?表达式时,如果...
    无沣阅读 5,170评论 0 3
  • 53.计算字符 在字符串中获取字符值的数量, 可以使用字符串字符属性中的计数属性: let unusualMena...
    无沣阅读 4,922评论 0 4
  • 关于 Swift 重要这个文档所包含的准备信息, 是关于开发的 API 和技术的。这个信息可能会改变, 根据这个文...
    无沣阅读 9,978评论 1 27
  • 86.复合 Cases 共享相同代码块的多个switch 分支 分支可以合并, 写在分支后用逗号分开。如果任何模式...
    无沣阅读 5,353评论 1 5

友情链接更多精彩内容