Created
September 11, 2016 23:46
-
-
Save charleshkang/19741099e3362d00d5c9a6caa80a0b2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ProductDetailViewController.swift | |
// WalmartLabs | |
// | |
// Created by Charles Kang on 9/7/16. | |
// Copyright © 2016 Charles Kang. All rights reserved. | |
// | |
import UIKit | |
import HCSStarRatingView | |
class ProductDetailViewController: UIViewController { | |
@IBOutlet weak var productNameLabel: UILabel! | |
@IBOutlet weak var productReviewCountLabel: UILabel! | |
@IBOutlet weak var productDescTextView: UITextView! | |
@IBOutlet weak var productPriceLabel: UILabel! | |
@IBOutlet weak var productImageView: UIImageView! | |
@IBOutlet weak var outOfStockLabel: UILabel! | |
@IBOutlet weak var starRatingView: HCSStarRatingView! | |
var allProducts = [Product]() | |
var selectedIndex = 0 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
navigationController!.navigationBar.tintColor = UIColor.whiteColor(); | |
updateUIWithProduct() | |
let rightSwipe = UISwipeGestureRecognizer(target: self, | |
action: #selector(ProductDetailViewController.handleSwipeGestures(_:))) | |
let leftSwipe = UISwipeGestureRecognizer(target: self, | |
action: #selector(ProductDetailViewController.handleSwipeGestures(_:))) | |
rightSwipe.direction = .Right | |
leftSwipe.direction = .Left | |
view.addGestureRecognizer(rightSwipe) | |
view.addGestureRecognizer(leftSwipe) | |
} | |
func updateUIWithProduct() { | |
let product = allProducts[selectedIndex] | |
productNameLabel.text = product.productName | |
productPriceLabel.text = product.productPrice | |
productReviewCountLabel.text = String("(\(product.reviewCount))") | |
productDescTextView.text = product.longDesc | |
starRatingView.value = CGFloat(product.reviewRating) | |
outOfStockLabel.hidden = true | |
if product.inStock?.boolValue == false { | |
outOfStockLabel.hidden = false | |
outOfStockLabel.textColor = UIColor.redColor() | |
} | |
let productImageURL: NSURL? = NSURL(string: product.productImage) | |
let placeholderImg = UIImage(named: "placeholder_img") | |
if let imageURL = productImageURL { | |
productImageView.sd_setImageWithURL(imageURL, placeholderImage: placeholderImg) | |
} | |
} | |
// MARK: - Swipe Animations | |
func handleSwipeGestures(sender: UISwipeGestureRecognizer) { | |
// let swipes = Swipes() | |
if sender.direction == .Right { | |
if selectedIndex > 0 { | |
selectedIndex -= 1 | |
updateUIWithProduct() | |
animateRightViews() | |
} | |
} else if sender.direction == .Left { | |
if selectedIndex + 1 < allProducts.count { | |
selectedIndex += 1 | |
updateUIWithProduct() | |
animateLeftViews() | |
} | |
} | |
} | |
func animateRightViews(){ | |
let transform = CATransform3DTranslate(CATransform3DIdentity, -500, 0, 0) | |
productNameLabel.layer.transform = transform | |
productReviewCountLabel.layer.transform = transform | |
productDescTextView.layer.transform = transform | |
productPriceLabel.layer.transform = transform | |
productImageView.layer.transform = transform | |
outOfStockLabel.layer.transform = transform | |
starRatingView.layer.transform = transform | |
UIView.animateWithDuration(0.5, animations: { () -> Void in | |
self.productNameLabel.layer.transform = CATransform3DIdentity | |
self.productReviewCountLabel.layer.transform = CATransform3DIdentity | |
self.productDescTextView.layer.transform = CATransform3DIdentity | |
self.productPriceLabel.layer.transform = CATransform3DIdentity | |
self.productImageView.layer.transform = CATransform3DIdentity | |
self.outOfStockLabel.layer.transform = CATransform3DIdentity | |
self.starRatingView.layer.transform = CATransform3DIdentity | |
}) | |
} | |
func animateLeftViews() { | |
let transform = CATransform3DTranslate(CATransform3DIdentity, 500, 0, 0) | |
productNameLabel.layer.transform = transform | |
productReviewCountLabel.layer.transform = transform | |
productDescTextView.layer.transform = transform | |
productPriceLabel.layer.transform = transform | |
productImageView.layer.transform = transform | |
outOfStockLabel.layer.transform = transform | |
starRatingView.layer.transform = transform | |
UIView.animateWithDuration(0.5, animations: { () -> Void in | |
self.productNameLabel.layer.transform = CATransform3DIdentity | |
self.productReviewCountLabel.layer.transform = CATransform3DIdentity | |
self.productDescTextView.layer.transform = CATransform3DIdentity | |
self.productPriceLabel.layer.transform = CATransform3DIdentity | |
self.productImageView.layer.transform = CATransform3DIdentity | |
self.outOfStockLabel.layer.transform = CATransform3DIdentity | |
self.starRatingView.layer.transform = CATransform3DIdentity | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment