ios - 在 swift4 中使用 NSTimer 处理 UIPagecontrol
问题描述
我正在尝试添加NSTimer
用于图像滑动的 UIPagecontrol。但无法得到它。我已经尝试了下面给出的东西。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var pagecontrol: UIPageControl!
@IBOutlet weak var imgview: UIImageView!
var tTime: Timer!
var index = 0
var items = ["1.png","11.png","111.png","1111.png"]
override func viewDidLoad() {
super.viewDidLoad()
configurePageControl()
let timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
// Do any additional setup after loading the view, typically from a nib.
}
func configurePageControl() {
self.pagecontrol.numberOfPages = items.count
self.pagecontrol.currentPage = 0
self.pagecontrol.tintColor = UIColor.red
self.pagecontrol.pageIndicatorTintColor = UIColor.black
self.pagecontrol.currentPageIndicatorTintColor = UIColor.green
}
@objc func update() {
index = index + 1
}
}
如果有人提供帮助,那就太好了。提前致谢。
解决方案
改变你的
@objc func update() {
index = index + 1
}
作用于
@objc func update() {
index = index + 1
if index >= items.count {
index = 0
}
imgview.image = UIImage(named: items[index])
pagecontrol.currentPage = index
}
它应该工作。
推荐阅读
- php - 运行事务时出现 SQL 语法错误,但是将相同的 SQL 直接插入 phpmyadmin 时它可以工作
- python - 如何从不同的for循环写入文件到一个f.write
- javascript - 需要帮助将加密逻辑从 Js 转换为 Java
- apache-kafka - 如何用 Kafka 生成连续编号的消息?
- python - 如何跨文件共享python实例?
- r - 如何将使用 ComplexHeatmap 包创建的热图保存为 png 格式?
- ubuntu-16.04 - Zabbix 从 4.0.21 升级到 5.2 mysqlversion 错误
- c# - 在字节中间设置 3 位 C#(按位运算符)
- javascript - 如何对所有列表(数组)项目进行操作?
- reactjs - React:如何填充保存在本地存储中的数据?