首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIpageViewController与定时器

UIpageViewController与定时器
EN

Stack Overflow用户
提问于 2017-09-18 11:25:56
回答 1查看 442关注 0票数 1

我在屏幕顶部的一个UICollectionViewCell中有一个UICollectionViewCell,它包含许多图像,并且运行良好,但是我需要它每3秒自动滚动到下一个图像,我该怎么办?

代码语言:javascript
运行
复制
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
    }
}

我在这里加进了我的牢房

代码语言:javascript
运行
复制
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

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

这是我找到的答案,但它仍然有一个错误,即当用户自扫它时,autoscroll是不完美的,可能会跳转到另一张图片。

代码语言:javascript
运行
复制
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)
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-18 11:45:39

首先,用timeInterval设置一个定时器,并选择repeats座位为true。

示例:

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

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

现在了解有关setViewControllers(_:direction:animated:completion:)的知识,以了解如何设置要显示的视图控制器。

而不是用所需的逻辑实现scrollToNextItem方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46278147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档