-3

I am creating a UIPageController which swipes 4 pages. In each page there is an image from the array I created. Now I want to make each image from the swipe view clickable to present a new specific page. Each image from the swipe view leads to a different 10 levels (buttons) page. the project file is here: http://s000.tinyupload.com/?file_id=90198426971136689376

This is my code in 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
}
}

and this code is in my 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)

}
}
AllPO
  • 1
  • 4

2 Answers2

1

As per this version of the quesion:

"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:"


SOLUTION:

Use UIGestureRecognizer.

1) Click on your Main.Storyboard.

2) Select UIGestureRecognizer.

enter image description here

3) Drag it on your Image of choice.

enter image description here

3.5) Use Cmd+Alt+Enter to open the Assistant Editor

4) Create an IBAction by Ctrl-dragging from your UITapGestureRecogniser to the Assistant Editor.

5) Put this code in your 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

    }
}
Coder1000
  • 4,071
  • 9
  • 35
  • 84
  • hi, i have an array list of images that are in the ui page view controller. these images changes by swipe. these are packs of a game. i want to when user clicked on each image, image present a dedicate page. – AllPO May 10 '16 at 11:31
  • @AllPO What do you mean by a dedicated page ? My solution normally still applies. – Coder1000 May 10 '16 at 11:33
  • thank you so much, but i added an image of my pages in my question please see it. i have one image view that show 4 image of array. your solution is for different images view. – AllPO May 10 '16 at 11:48
  • @AllPO No it's not. As I said, my solution still applies here. It applies to all UIImageViews. You just need to use a switch case. – Coder1000 May 10 '16 at 11:49
  • thank you so much, but I'm new to swift, i don't know how to get array images an action – AllPO May 10 '16 at 11:55
  • @AllPO Also, please clarify what you want to achieve exactly. It is still very unclear. – Coder1000 May 10 '16 at 12:04
  • thanx a lot but i can't solve it :( i copied your solution in my code but have 6 error:, i think i haven't swift knowledge. use of unresolved identifier 'ImageArrayIndex' – AllPO May 10 '16 at 12:42
  • @AllPO What I showed you was pseudo-code. If you want real code, I need to know exactly what you want to achieve and your question is very unclear. Please rewrite what you want to do in detail so we may understand you and I may provide you with code. – Coder1000 May 10 '16 at 12:46
  • thank you again , i am creating a uipage controller which it swipes 4 pages , in each page it has an image from the array i created , now i want to make each image from the swipe view clickable and head it through a new page , that i don't know how !! – AllPO May 10 '16 at 13:03
  • Will all images redirect to the same buttons ? – Coder1000 May 10 '16 at 13:19
  • no each image leads to a new page that has its own 10 levels. – AllPO May 10 '16 at 13:21
  • @AllPO Ahhhh ! See ? Had you put all that information in your question, you would already have received an answer that solves your issue ! Please edit your question, I will edit my answer ;) – Coder1000 May 10 '16 at 13:24
  • this line "let itemController = itemController as! PageItemController" have an error. use of unresolved identifier 'viewController'. i have "GameSelectPack" view controller with no id, "PageItemViewController" with "ItemController" id, and PageController with "PageController" id – AllPO May 10 '16 at 13:56
  • @AllPO Where did you put the IBAction ? – Coder1000 May 10 '16 at 13:57
  • i putted IBAction in "PageItemController", is it right? – AllPO May 10 '16 at 14:00
  • @AllPO No, put it in ViewController. – Coder1000 May 10 '16 at 14:03
  • may i have your email so i can send you the project? – AllPO May 10 '16 at 14:13
  • No :) But you can put a link in your question so that we may all help you. – Coder1000 May 10 '16 at 14:14
  • thank you Coder1000, I uploaded my project here and edited my question again: http://s000.tinyupload.com/?file_id=90198426971136689376 – AllPO May 10 '16 at 14:59
  • in the pageItemController.swift – AllPO May 11 '16 at 12:38
  • yes you wrote IBoutlet should be in view controller, but when the contentImage is in the pageItemController and i added tap gesture on it, how can ctrl + drag into view controller? i can only ctrl + drag into the pageItemController. i have 2 view controller and 1 pageViewController. i have uploaded my source code as you wish, and send url, plz read it. i know all of mistakes is from me, sorry – AllPO May 11 '16 at 12:58
  • ok plz tell me what keyword for tutorial can help me. i accepted your answer and glad that you help me last 2 days. apologize again for poor knowledge. – AllPO May 11 '16 at 13:01
  • @AllPO Look, you accepted my answer and now you remove the checkmark, that's fine by me. But if I tell you twice to put the IBAction in your ViewController and you come back to me saying it doesn't work without actually trying the solution I provided, there is nothing else I can do to help you :D – Coder1000 May 11 '16 at 13:01
  • @AllPO Here is a link: https://spin.atomicobject.com/2015/12/23/swift-uipageviewcontroller-tutorial/ – Coder1000 May 11 '16 at 13:03
  • @AllPO I updated my answer. Tell me if it works this time. – Coder1000 May 11 '16 at 13:20
1

On your ItemPageController:

var itemIndex:Int?
var imageName:String?

Add UITapGesture To 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!)
    }

On its triggered method:

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;
        }        
    }
Alvin George
  • 14,148
  • 92
  • 64
  • hi alvin i test your solution but i have error on print(image name) print(image index) and let targetimageindex = item index as int – AllPO May 10 '16 at 19:58
  • thanx for update; i have this code in my itemPageController: var itemIndex: Int = 0 var imageName: String = "" { didSet { if let imageView = contentImageView { imageView.image = UIImage(named: imageName) } } } should i change to your solution? my targetImageIndex is: "contentImage" and itemIndex is: "itemIndex". is that right? and one more question: i run it and got this error: unrecognized selector sent to instance. sorry for low knowledge of swift :( – AllPO May 11 '16 at 11:33
  • I think the problem would be about adding image to imageView using didSet. We are adding UITapGesture on ViewDidLoad which executes after didSet. Otherwise the code looks fine. – Alvin George May 11 '16 at 11:46
  • @ AIIPO : As it mentions: unrecognized selector sent to instance , please check your method name to avoid spelling mistakes. – Alvin George May 11 '16 at 11:58