Last active
September 20, 2018 06:32
-
-
Save bryanrmq/a4d5037f20c5728fc0fba3eab1a9cb72 to your computer and use it in GitHub Desktop.
CardCollectionViewCell.swift
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
// | |
// Created by Reymonenq Bryan on 16.08.18. | |
// Copyright (c) 2018 Brey. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import Nuke | |
class CatCollectionViewCell: UICollectionViewCell, UIGestureRecognizerDelegate { | |
static var identifier = "Cat" | |
enum Defaults { | |
public static var cornerRadius: CGFloat = 16 | |
public static var borderWidth: CGFloat = 0.0 | |
public static var borderColor: UIColor = UIColor.clear | |
public static var shadowColor: UIColor = UIColor.black | |
public static var shadowOffset: CGSize = CGSize(width: 0, height: 4.0) | |
public static var shadowRadius: CGFloat = 16 | |
public static var shadowOpacity: Float = 0.0 | |
public static var imageContentModel: UIViewContentMode = .scaleAspectFill | |
} | |
lazy var like: UIButton = { | |
let button = UIButton() | |
button.setImage(UIImage(named: "heart"), for: .normal) | |
return button | |
}() | |
lazy var image: UIImageView = { | |
let view = UIImageView() | |
view.backgroundColor = UIColor.lightGray | |
return view | |
}() | |
override var isHighlighted: Bool { | |
get { | |
return super.isHighlighted | |
} | |
set { | |
super.isHighlighted = newValue | |
self.animateHighlight() | |
} | |
} | |
// MARK: - Init | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setupUI() | |
setupLayout() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func setImage(withURL url: String, completion: @escaping () -> Void) { | |
let options = ImageLoadingOptions(placeholder: UIImage(named: LoadingHelper.getRandomCatLoader()), | |
transition: .fadeIn(duration: 0.3), | |
failureImage: nil, | |
failureImageTransition: .fadeIn(duration: 0.3), | |
contentModes: .init( | |
success: .scaleAspectFill, | |
failure: .center, | |
placeholder: .center | |
) | |
) | |
Nuke.loadImage(with: URL(string: url)!, | |
options: options, | |
into: self.image, | |
progress: nil, | |
completion: { (image, err) in | |
completion() | |
}) | |
} | |
// MARK: - UI | |
func setupUI() { | |
self.contentView.addSubview(image) | |
self.contentView.addSubview(like) | |
self.contentView.bringSubview(toFront: like) | |
self.contentView.layer.cornerRadius = Defaults.cornerRadius | |
self.contentView.layer.borderWidth = Defaults.borderWidth | |
self.contentView.layer.borderColor = Defaults.borderColor.cgColor | |
self.contentView.layer.masksToBounds = true | |
self.layer.shadowColor = Defaults.shadowColor.cgColor | |
self.layer.shadowOffset = Defaults.shadowOffset | |
self.layer.shadowRadius = Defaults.shadowRadius | |
self.layer.shadowOpacity = Defaults.shadowOpacity | |
self.layer.masksToBounds = false | |
self.layer.shadowPath = UIBezierPath(roundedRect:self.bounds, cornerRadius:self.contentView.layer.cornerRadius).cgPath | |
} | |
func setupLayout() { | |
image.translatesAutoresizingMaskIntoConstraints = false | |
image.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: -24).isActive = true | |
image.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 24).isActive = true | |
image.topAnchor.constraint(equalTo: self.topAnchor, constant: -24).isActive = true | |
image.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 24).isActive = true | |
like.translatesAutoresizingMaskIntoConstraints = false | |
like.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16).isActive = true | |
like.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -16).isActive = true | |
like.heightAnchor.constraint(equalToConstant: 24) | |
} | |
// MARK: - Animation | |
func animateHighlight(_ value: Bool = false) { | |
let isTouched = (self.isHighlighted || value) ? true : false | |
UIView.animate(withDuration: 0.275, delay: 0, options: .curveEaseOut, animations: { | |
self.transform = isTouched ? CGAffineTransform(scaleX: 0.95, y: 0.95) : CGAffineTransform(scaleX: 1.0, y: 1.0) | |
self.like.transform = isTouched ? CGAffineTransform(translationX: -8, y: -8) : CGAffineTransform(translationX: 0, y: 0) | |
}, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment