我是iOS的新手。我正在尝试使用NSTimer进行页面控制以实现自动滚动,并且我想更改页面控制中的点的形状。我需要带有矩形框的页面控制。如果有人帮助我,会很棒。
答案 0 :(得分:0)
我有一个示例示例,用于在iOS或Android应用程序中像ganna.com这样的集合视图自动滚动。
对于Swift 4.2
代码:
var timer: Timer?
@IBOutlet weak var collectionView : UICollectionView!
@IBOutlet weak var pageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.reloadData()
startTimer()
}
func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(scrollAutomatically), userInfo: nil, repeats: true)
}
@objc func scrollAutomatically(_ timer1: Timer) {
for cell in collectionView.visibleCells {
if banner.count == 1 {
return
}
let indexPath = collectionView.indexPath(for: cell)!
if indexPath.row < (banner.count - 1) {
let indexPath1 = IndexPath.init(row: indexPath.row + 1, section: indexPath.section)
collectionView.scrollToItem(at: indexPath1, at: .right, animated: true)
pageControl.currentPage = indexPath1.row
}
else {
let indexPath1 = IndexPath.init(row: 0, section: indexPath.section)
collectionView.scrollToItem(at: indexPath1, at: .left, animated: true)
pageControl.currentPage = indexPath1.row
}
}
}
banner.Count
=>许多包含集合视图的单元格
我希望您觉得这有用。
答案 1 :(得分:0)
尝试一下
var index = 0
let timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
func update() {
index = index + 1
let x = CGFloat(index) * scrollView.frame.size.width
scrollView.setContentOffset(CGPoint(x:x, y:0), animated: true)
}