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.
}
}