如何使图像在 UIPageViewController 中可点击?

How to make Images clickable in a UIPageViewController?

我正在创建一个滑动 4 页的 UIPageController。在每一页中都有一张来自我创建的数组的图像。现在我想让滑动视图中的每个图像都可以点击以呈现一个新的特定页面。滑动视图中的每张图像都指向不同的 10 级(按钮)页面。 项目文件在这里: http://s000.tinyupload.com/?file_id=90198426971136689376

这是我在 ViewController 中的代码:

private var pageViewController: UIPageViewController?

private let contentImages = ["Pack_1.png",
    "Pack_2.png",
    "Pack_3.png",
    "nature_pic_4.png"];

override func viewDidLoad() {
    super.viewDidLoad()
    createPageViewController()
    setupPageControl()
}

private func createPageViewController() {

    let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController
    pageController.dataSource = self

    if contentImages.count > 0 {
        let firstController = getItemController(0)!
        let startingViewControllers: NSArray = [firstController]
        pageController.setViewControllers(startingViewControllers as? [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
    }

    pageViewController = pageController
    addChildViewController(pageViewController!)
    self.view.addSubview(pageViewController!.view)
    pageViewController!.didMoveToParentViewController(self)
}

private func setupPageControl() {
    let appearance = UIPageControl.appearance()
    appearance.pageIndicatorTintColor = UIColor.grayColor()
    appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
    appearance.backgroundColor = UIColor.darkGrayColor()
}


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

    let itemController = viewController as! PageItemController

    if itemController.itemIndex > 0 {
        return getItemController(itemController.itemIndex-1)
    }

    return nil
}

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

    let itemController = viewController as! PageItemController

    if itemController.itemIndex+1 < contentImages.count {
        return getItemController(itemController.itemIndex+1)
    }

    return nil
}

private func getItemController(itemIndex: Int) -> PageItemController? {

    if itemIndex < contentImages.count {
        let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as! PageItemController
        pageItemController.itemIndex = itemIndex
        pageItemController.imageName = contentImages[itemIndex]
        return pageItemController
    }

    return nil
}
}

此代码在我的 pageItemController 中:

var itemIndex: Int = 0
var imageName: String = "" {

    didSet {

        if let imageView = contentImageView {
            imageView.image = UIImage(named: imageName)
        }

    }
}

@IBOutlet var contentImageView: UIImageView?

override func viewDidLoad() {
    super.viewDidLoad()
    contentImageView!.image = UIImage(named: imageName)
    self.view.backgroundColor = UIColor (red: 100, green: 100, blue: 100, alpha: 0)

}
}

根据这个版本的问题:

"I'm creating a UIPageControllerView that shows 4 images. is there any way to make this images clickable? each image should present a dedicate page. this is my code in viewController:"


解决方案:

使用 UIGestureRecognizer。

1) 点击你的 Main.Storyboard.

2) Select UIGestureRecognizer.

3) 将其拖动到您选择的图像上。

3.5) 使用Cmd+Alt+Enter打开助手编辑器

4) 通过按住 Ctrl 从 UITapGestureRecogniser 拖动到助理编辑器来创建一个 IBAction。

5) 将此代码放入您的 ViewController.

class ViewController {

    let itemIndex: Int!        

    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)
    {
       if (!completed)
      {
        return
      }
      self.pageControl.currentPageIndex = pageViewController.viewControllers!.first!.view.tag //Page Index
      self.itemIndex = self.pageControl.currentPageIndex
    }

    @IBAction func presentDedicatedPage(sender: UIImageView) {

    //pseudo-code here, for example:

    switch self.itemIndex  {

        case 0:
        // present these 10 levels
        break

        case 1:
        //present these other 10 levels
        break

        case 2:
        //present these other 10 levels
        break

        case 3:
        //present these other 10 levels
        break

    }
}

在您的项目页面控制器上:

var itemIndex:Int?
var imageName:String?

将点击手势添加到 ImageView。

override func viewDidLoad() {
        super.viewDidLoad()
        let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
        targetImageView.userInteractionEnabled = true
        targetImageView.addGestureRecognizer(tapGestureRecognizer)
        targetImageView.image =  UIImage(named: imageName!)
    }

关于其触发方式:

func imageTapped(img: AnyObject)
    {
        print(imageName)
        print(itemIndex)

        //Using a switch statement
        let targetImageIndex =  itemIndex! as Int

        switch (targetImageIndex) {
        case 0:
            print("case 0")
            break;
        case 1:
            print("case 1")
            break;
        case 2:
            print("case 2")
            break;

        default:
            break;
        }        
    }