从UIPageViewController中删除图像

时间:2015-11-25 08:02:10

标签: swift uipageviewcontroller

我设置了一个PageViewController,以便我可以添加来自相机或库的照片,并且每次调用imagepickercontroller时,pageviewcontroller都会自行重置,以便pageviewcontrol更新并显示。它运作完美(我认为)。我所知道的是可以选择删除其中一张照片然后调用相同的重置。

问题是,我无法获得pageviewcontroller中显示的当前图像的准确索引值。我尝试从viewControllerBeforeViewController或AfterViewController或pagecontrolleratindex获取索引...但索引没有正确遵循pagecontrol的设计方式。我读到了关于willtransitiontoviewcontrollers和didfinsihanimating,但那些没有被调用,我在想,因为我从数组而不是新的视图控制器获取图像。

我的删除方法有效,但索引不准确,所以即使显示第二张图片,第一张图片也可能是删除的图像。

有没有一种很好的方法来获取当前显示的数组索引,以便我可以从数组中删除图像? //这个数组保存来自imagepicker的图像,我调用restartviewcontrollers evertime有一个imagepicked来更新页面数量,这是有效的。

更新

var pageViewController:UIPageViewController!
var pageImages: [UIImage]? = [] //this array holds images from imagepicker, i call the restartviewcontrollers evertime there is an imagepicked to update the number of pages and this works.
var deletePageIndex: Int = 0


func pageViewControllerAtIndex(index: Int) -> PhotoContentPageViewController {

    if (self.pageImages!.count == 0 || index >= self.pageImages!.count) {
        return PhotoContentPageViewController()
    }

    var vc = self.storyboard?.instantiateViewControllerWithIdentifier("PhotoPageContentStoryBoard") as! PhotoContentPageViewController

    vc.imageFileName = pageImages![index] as! UIImage
    vc.pageIndex = index

    return vc
}

func restartViewController() {
    var startVc = self.pageViewControllerAtIndex(0) as PhotoContentPageViewController
    var viewControllers = NSArray(object: startVc)

    self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)
}


func startViewControllers() {
    self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PhotoPageViewControllerStoryBoard") as! UIPageViewController
    self.pageViewController.dataSource = self
    pageViewController.delegate = self

    var startVc = self.pageViewControllerAtIndex(0) as PhotoContentPageViewController
    var viewControllers = NSArray(object: startVc)

    self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)

    self.pageViewController.view.frame = CGRectMake(0, -10, self.view.frame.size.width, self.view.frame.size.height-30)
    self.addChildViewController(self.pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)
}



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

    var vc = viewController as! PhotoContentPageViewController
    var index = vc.pageIndex as Int

    if (index == 0 || index == NSNotFound) {
        return nil
    }

    index--

    return self.pageViewControllerAtIndex(index)
}


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

    var vc = viewController as! PhotoContentPageViewController
    var index = vc.pageIndex as Int

    if (index == NSNotFound) {
        return nil
    }

    index++

    if (index == self.pageImages!.count) {
        return nil
    }

    return self.pageViewControllerAtIndex(index)
}


func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {


}


func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if (!completed) {
        return
    }

}

`

1 个答案:

答案 0 :(得分:0)

如果您实际使用的是UIPageViewControllerDelegate,则可以使用 willtransitiontoviewcontrollers ,但只有将页面视图控制器的委托属性设置为视图控制器时,该方法才有效。这样,您只需在页面视图控制器中获取当前视图控制器的索引并将其用于删除。

myPageViewController.delegate = self

确保您的视图控制器实现UIPageViewControllerDelegate

<强>更新 假设您有一组视图控制器,您可以像这样实现数据源:

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

    if let index = pages.indexOf(viewController)?.advancedBy(1) where index < pages.count {
        return pages[index]
    }
    return nil
}

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

    if let index = pages.indexOf(viewController)?.advancedBy(-1) where index > -1 {
        return pages[index]
    }
    return nil
}