1

I tried using this thread for help but the code did not work...

I have an array of animation images (scrolls left every 3 secs). What I want to do is when you click the corresponding image, it will show a detail view of that image that will have labels on the VC containing some detailed information regarding that image. However, this is where I get stuck.

I have applied the use of a UITapGestureRecognizer and it does push to the new view controller but its a static push - meaning that no matter what image i clicked on the corresponding VC page will be the same.

My code are as follows

Main View Controller:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var sponsorAnimation: UIImageView!

let images = [
    UIImage(named: "image1")!,
    UIImage(named: "image2")!]

var index = 0
let animationDuration: NSTimeInterval = 1
let switchingInterval: NSTimeInterval = 3

let tappedSponsor = UITapGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.barTintColor = UIColor(red: 119/255, green: 136/255, blue: 153/255, alpha: 1.0)
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

    sponsorAnimation.image = images[index]
    animateImageView()

    tappedSponsor.addTarget(self, action: "imageTapped")
    sponsorAnimation.addGestureRecognizer(tappedSponsor)
    sponsorAnimation.userInteractionEnabled = true

}

func imageTapped() {
// not sure what code to enter in here...
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue == "showSponsor") {
        let destinationVC = segue.destinationViewController as! NewViewController

        destinationVC.sponsorImage = images
        // am i on the right track??

    }
}

func animateImageView() {
    CATransaction.begin()

    CATransaction.setAnimationDuration(animationDuration)
    CATransaction.setCompletionBlock {
        let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC)))
        dispatch_after(delay, dispatch_get_main_queue()) {
            self.animateImageView()
        }
    }

    let transition = CATransition()
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromRight
    /*
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromRight
    */
    sponsorAnimation.layer.addAnimation(transition, forKey: kCATransition)
    sponsorAnimation.image = images[index]

    CATransaction.commit()

    index = index < images.count - 1 ? index + 1 : 0

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

And this is my current detail VC:

import UIKit

class NewViewController: UIViewController {

@IBOutlet weak var sponsorAnimation: UIImageView!


var sponsorImage: UIImage!


override func viewDidLoad() {
    super.viewDidLoad()

    self.sponsorAnimation!.image = self.sponsorImage

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}
Community
  • 1
  • 1
Matty
  • 343
  • 1
  • 5
  • 16
  • Hi there, some questions: Who is performing the segue? Did you configure it in the storyboard to perform automatically? In my view you should have a segue like VC->VC, not button->VC and in the method `imageTapped` you should perfom the segue – pedros Oct 04 '15 at 04:33
  • @pedros Currently I have a segue from the TapGestureRecognizer (attached to the imageView) to NewViewController. If I do VC -> VC, is there a way you can pass the selected image? I'd assume you could by using an if statement with the name of the images - in this case image1 and image2. But I'm very lost as you can see! – Matty Oct 04 '15 at 04:36
  • Did you place a break point at `if (segue == "showSponsor")`? Is it entering the if at all? If so you are on the right track. What you need is to save the image tapped, so in `imageTapped` you need to find what image was tapped and save in a variable, so that you can pass this image to the nextVC. You can see that in `prepareForSegue` you are passing both images to the nextVC – pedros Oct 04 '15 at 04:41
  • @pedros Sounds silly but i'm quite new to Xcode...I place a break point at the if (segue == "showSponsor"). How do I check if its registering? – Matty Oct 04 '15 at 04:47
  • Dont worry, we all need to learn at some time. Try placing the breakpoint inthe the if, then run you app, and tap the image. If the code stops at your breakpoint than its entering the if as expected. – pedros Oct 04 '15 at 04:58
  • @pedros Thanks for understanding. I placed the breakpoint at the if and when i ran the app and tapped on the image nothing happened. It must not be registering :( – Matty Oct 04 '15 at 05:01
  • Ok, so your problem is probably in the segue. I noticed now you are comparing the segue to a string, they wont be equal, what you need to compare is the `segue.identifier`. Also, you need to have previously set the identifier for the segue in the storyboard, this way when your code reaches the if, the segue will have the identifier="showSponsor" and it will enter the if. After solving this the code should stop at the breakpoint. – pedros Oct 04 '15 at 05:09

1 Answers1

0

Well, from animated imageview we can't get the current image. You need alternate ways doing it. Probably by adding a NSTimer for animation. This is an objective C thread that may help you. You would need to convert it to Swift though and I am sure you can do that :)!

Community
  • 1
  • 1
Abhinav
  • 37,684
  • 43
  • 191
  • 309