swift - UIpageViewController 和定时器

标签 swift timer uipageviewcontroller

我在屏幕顶部的一个 My UICollectionViewCell 中有一个 UIPageViewController (height = frame.height/3) 它包含许多图像 它运行良好,但我需要它每 3 秒自动滚动到下一张图片,我该怎么办?

import UIKit

class PageViewController: UIPageViewController , UIPageViewControllerDataSource{

    var scrollingTimer = Timer()
    let imageNames = ["Apple_Watch_Main" , "Pic2" , "Pic3"]

    override func viewDidLoad() {
        super.viewDidLoad()

        dataSource = self
        let frameViewController = FrameViewController()
        frameViewController.imageName = imageNames.first

        let viewControllers = [frameViewController]
        setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        let currentImageName = (viewController as! FrameViewController).imageName
        let currentIndex = imageNames.index(of: currentImageName!)
        if currentIndex! > 0 {
            let frameViewController = FrameViewController()
            frameViewController.imageName = imageNames[currentIndex! - 1]
            return frameViewController
        }
        return nil
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

        let currentImageName = (viewController as! FrameViewController).imageName
        let currentIndex = imageNames.index(of: currentImageName!)
        if currentIndex! < imageNames.count - 1 {
            let frameViewController = FrameViewController()
            frameViewController.imageName = imageNames[currentIndex! + 1]
            return frameViewController
        }
        return nil
    }
}

我在这里添加到我的单元格

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        switch indexPath.item {
        case 0 :
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as!ImageCell

            let pagecontroller = PageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
            self.addChildViewController(pagecontroller)

            cell.addSubview(pagecontroller.view)

            pagecontroller.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/3)
            return cell
        case 1 :
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "filterandorganize", for: indexPath) as! FilterAndOrganize
            return cell
        case 2 :
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Offers", for: indexPath) as! Offers
            return cell
        case 3 :
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OfferedProducts", for: indexPath) as! OfferedProducts
            cell.firstViewController = self
            return cell
        default:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Offers", for: indexPath) as! Offers
            return cell
        }
    }

在情况 0

我搜索了很多,我发现了一些这样的问题,但没有任何完整和正确的答案

这是我找到的答案,但它仍然有一个错误,那就是当用户自己滑动它时,自动滚动并不完美,可能会从一张图片跳到另一张

class PageViewController: UIPageViewController , UIPageViewControllerDataSource{
    var index = 0
    let imageNames = ["Apple_Watch_Main" , "Pic2" , "Pic3"]

    override func viewDidLoad() {
        super.viewDidLoad()
        dataSource = self

        let frameViewController = FrameViewController()
        frameViewController.imageName = imageNames[index]
        index += 1
        let viewControllers = [frameViewController]
        setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)

        Timer.scheduledTimer(timeInterval: 6.0 ,
                             target: self,
                             selector: #selector(myFunc(_:)),
                             userInfo: index,
                             repeats: true)
    }
    //FIXME: needs some fixes
    func myFunc(_ timer: Timer) {
        if index == imageNames.count  {
            index = 0
        }
        else {
            self.changePIC(index)
            index += 1
        }
    }
    func changePIC(_ i: Int) {
        let frameViewController = FrameViewController()
        frameViewController.imageName = imageNames[i]
        let viewControllers = [frameViewController]
        setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
    }

最佳答案

首先用 timeInterval 和选项 repeats seat 设置一个 Timer。

示例:

Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(scrollToNextItem), userInfo: nil, repeats: true)

现在您有了 Timer,它将每 3 秒触发一次 scrollToNextItem 方法。

现在阅读 setViewControllers(_:direction:animated:completion:)了解如何设置要显示的 View Controller 。

然后用所需的逻辑实现 scrollToNextItem 方法。

关于swift - UIpageViewController 和定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46278147/

相关文章:

ios - 在 Swift 中按下按钮时返回字符串

swift - Xcode Swift 3 计时器错误

ios - 找不到添加 UIBarButtonItem 的位置

javascript - 倒计时器可额外增加 2 分钟

swift - 不同宽度的 UIPageViewController 页面

ios - 是否可以将 View 从一个 ViewController 动画到下一个 ViewController?

ios - 使用cornerRadius实现的圆形UIView看起来是 block 状的

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

ios - 如何使用 Graphics Context 清除具有关闭路径的任何 imageView 的图像

ios - slider 擦洗不太顺畅