Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios pageViewController上的背景图像反向重叠_Ios_Swift_Uipageviewcontroller - Fatal编程技术网

Ios pageViewController上的背景图像反向重叠

Ios pageViewController上的背景图像反向重叠,ios,swift,uipageviewcontroller,Ios,Swift,Uipageviewcontroller,我有一个pageViewController,它根据索引显示背景图像。从左向右(向前)滑动效果很好,但反向滑动时,背景图像彼此重叠。在我实现背景图像之前,每个VC上的UI都可以很好地向前和向后工作,所以我知道它就是背景图像。另外,如果我在我的故事板中改为页面卷曲,而不是滚动,则反过来效果很好,只是对滚动不起作用。这显然是一个已知的bug(),但我需要一个Swift解决方案 pageviewController: func pageViewController(pageViewControlle

我有一个
pageViewController
,它根据索引显示背景图像。从左向右(向前)滑动效果很好,但反向滑动时,背景图像彼此重叠。在我实现背景图像之前,每个VC上的UI都可以很好地向前和向后工作,所以我知道它就是背景图像。另外,如果我在我的
故事板中改为
页面卷曲
,而不是
滚动
,则反过来效果很好,只是对
滚动
不起作用。这显然是一个已知的bug(),但我需要一个Swift解决方案

pageviewController:

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

        guard
            let currentPageViewController = viewController as? SinglePageViewController,
            let currentIndex = indexOfForecast(currentPageViewController.forecast!) as? Int where currentIndex != 0 else { return nil }  //fixed bug where index was -0 only on scroll

        return viewControllerAtIndex(currentIndex - 1)

    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        guard
            let lastForecast = forecasts?.last,
            let currentPageViewController = viewController as? SinglePageViewController, currentForecast = currentPageViewController.forecast,
            let currentIndex = indexOfForecast(currentForecast) as? Int where currentIndex != forecasts?.indexOf(lastForecast) else {return nil}

            return viewControllerAtIndex(currentIndex + 1)

    }

 func viewControllerAtIndex(index: Int) -> UIViewController? {
        if let forecast = forecasts?[index] {
            let time = forecastTimes[index]
            let imageURL = conditionsIcons[index]
            let backgroundImageName = backgroundImageNames.names[index]
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("SinglePageViewController") as! SinglePageViewController
            vc.forecast = forecast
            vc.time = time
            vc.imageURL = imageURL
            vc.backgroundImageName = backgroundImageName

            return vc
        }
        return nil
    }


    func showVC() {
        if let firstVC = viewControllerAtIndex(0) {
            busyAlertController.dismiss()
            let viewControllers = [firstVC]
            self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
        }
    }
override func viewDidLoad() {
        super.viewDidLoad()

        backgroundImage = UIImageView(image: UIImage(named: backgroundImageName!))
        backgroundImage.contentMode = UIViewContentMode.ScaleAspectFill
        self.view.insertSubview(backgroundImage, atIndex: 0)
}
singlePageViewController:

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

        guard
            let currentPageViewController = viewController as? SinglePageViewController,
            let currentIndex = indexOfForecast(currentPageViewController.forecast!) as? Int where currentIndex != 0 else { return nil }  //fixed bug where index was -0 only on scroll

        return viewControllerAtIndex(currentIndex - 1)

    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        guard
            let lastForecast = forecasts?.last,
            let currentPageViewController = viewController as? SinglePageViewController, currentForecast = currentPageViewController.forecast,
            let currentIndex = indexOfForecast(currentForecast) as? Int where currentIndex != forecasts?.indexOf(lastForecast) else {return nil}

            return viewControllerAtIndex(currentIndex + 1)

    }

 func viewControllerAtIndex(index: Int) -> UIViewController? {
        if let forecast = forecasts?[index] {
            let time = forecastTimes[index]
            let imageURL = conditionsIcons[index]
            let backgroundImageName = backgroundImageNames.names[index]
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("SinglePageViewController") as! SinglePageViewController
            vc.forecast = forecast
            vc.time = time
            vc.imageURL = imageURL
            vc.backgroundImageName = backgroundImageName

            return vc
        }
        return nil
    }


    func showVC() {
        if let firstVC = viewControllerAtIndex(0) {
            busyAlertController.dismiss()
            let viewControllers = [firstVC]
            self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
        }
    }
override func viewDidLoad() {
        super.viewDidLoad()

        backgroundImage = UIImageView(image: UIImage(named: backgroundImageName!))
        backgroundImage.contentMode = UIViewContentMode.ScaleAspectFill
        self.view.insertSubview(backgroundImage, atIndex: 0)
}
根据我的回答,这里有一个快速版本。注意,这是使用在线转换器从Objective-C转换为Swift的(因为我不在我的开发机器上——通常不在Swift中工作),并通过一些额外的修改来获得弱的自参考。请随时指出执行中的任何错误

pageViewController.setViewControllers(
    viewControllers, 
    direction: UIPageViewControllerNavigationDirectionForward, 
    animated: true, 
    completion: { [weak pageViewController] (finished: Bool) in 
        if finished {
            dispatch_async(dispatch_get_main_queue(), {                            
                pageViewController.setViewControllers(
                    viewControllers, 
                    direction: UIPageViewControllerNavigationDirectionForward, 
                    animated: false, 
                    completion: nil
                )

        })
    }

})
[weak…]
的技巧称为“捕获列表”,在“定义捕获列表”一节以及“定义捕获列表”一节中都有详细介绍

(如果您没有对此进行跟踪,其目的是我们不希望闭包具有对
pageViewController
的强引用,从而防止ARC在不再需要时取消分配它。因此,我们将该变量标记为弱引用,并且如果闭包是对
的最后一个引用>pageViewController
,sayonara)

另一点是,我没有注意到事情的可选方面,所以要警告实现者


编辑:

看看你的代码,我的答案根本不是你想要的。我的答案解决的问题是通过编程设置视图控制器,但你说的是刷卡

我猜您还没有实现所有的委托方法——我只看到了
viewControllerAtIndex
。难道不需要执行
viewControllerBeforeViewController
viewControllerAfterViewController
才能正常工作吗?(对不起,我已经有一段时间没有处理过那个类了。)粗略地看一眼我就这么想


无论如何,我认为出现了错误的情况。请检查一下,也许可以详细说明你的问题,显示更多的代码,等等。

这个问题的答案很简单,只需将图像上的
clipsToBounds
设置为
true

除了@GarySabo的答案之外,我还必须对图像的约束进行布局superview(在本例中为默认视图)如下所示:


据我所见,您似乎正在添加多个视图控制器,但从未删除它们,也许这是有意从左到右,但从右到左,您不想再次添加Hanks@Knight0fDragon您的评论引导我进行搜索,我在这里发现了一个确切的问题:但我把我的问题留给了aNSWER仅在Obj-C中,我正在使用Swift。谢谢Matt!!问题在于[weak pageViewController]在哪里定义了pageViewController?这里不应该是self,对吗?因为我尝试使用[weak self]和self.setViewController,但它没有阻止issue@PSUSabes没问题。不过,这里的方向有一些小的变化。请查看我编辑的post.hmmmm,我确实实现了这些委托方法,并将它们发布到了我的问题中。我不明白的是,为什么我的代码在属性检查器中设置了一个“curl”时可以完美地工作,但是当你向后滚动时,“滚动”会导致VCs混乱?再次感谢你的时间!非常感谢!我也遇到了同样的问题,并且尝试了几个小时:)你救了我;)@pasql很高兴它帮助了你=)