Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve/image stack #14

Merged
merged 5 commits into from
Aug 18, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions Source/BottomView/StackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class StackView: UIView {
view.layer.borderWidth = 1
view.contentMode = .ScaleAspectFill
view.clipsToBounds = true
view.alpha = 0
array.append(view)
}
return array
Expand All @@ -33,11 +34,10 @@ class StackView: UIView {
return gesture
}()

lazy var viewSize: CGSize = CGSize(width: self.frame.width * 0.6, height: self.frame.height * 0.6)

override init(frame: CGRect) {
super.init(frame: frame)
views.map{ self.addSubview($0) }
views[0].alpha = 1
self.addGestureRecognizer(tapGestureRecognizer)
layoutSubviews()
ImageStack.sharedStack.delegate = self
Expand All @@ -46,6 +46,8 @@ class StackView: UIView {

override func layoutSubviews() {
let step = -4
let viewSize = CGSize(width: self.frame.width * 0.6, height: self.frame.height * 0.6)

for (i, view) in enumerate(views) {
var side = i * step
var frame = CGRect(origin: CGPoint(x: side, y: side), size: viewSize)
Expand All @@ -64,26 +66,59 @@ class StackView: UIView {

extension StackView: ImageStackDelegate {
func imageDidPush(image: UIImage) {

//TODO indexOf in swift 2
let emptyView = views.filter( {$0.image == nil} ).first

if let emptyView = emptyView {
println("Animating")
animateImageView(emptyView)
}
renderViews()
}

func imageStackDidDrop(image: UIImage) {
// Uncomment if you want fancy animations
// let viewToEmpty = views.filter( {$0.image == image} ).first
//
// if let viewToEmpty = viewToEmpty {
// animateImageView(viewToEmpty)
// }

renderViews()
}

func renderViews() {
let photos = ImageStack.sharedStack.images
//TODO: Find better way to limit value to bounds
var size = min(photos.count - 1, 3)
//because Swift is not functional language
if ImageStack.sharedStack.images.count < 1 {
views.map { $0.image = nil}
return
}

for (index, view) in enumerate(views.reverse()) {
if index <= size {
view.image = photos.reverse()[index]
let photos = suffix(ImageStack.sharedStack.images, 4)

//TODO: This can be done in functional-style
for (index, view) in enumerate(views) {
if index <= photos.count - 1 {
view.image = photos[index]
view.alpha = 1
} else {
view.image = nil
view.alpha = 0
}
}
}

private func animateImageView(imageView: UIImageView) {
imageView.transform = CGAffineTransformMakeScale(0, 0)

UIView.animateWithDuration(0.3, animations: { [unowned self] in
imageView.transform = CGAffineTransformMakeScale(1.05, 1.05)
}, completion: { _ in
UIView.animateWithDuration(0.2, animations: { _ in
imageView.transform = CGAffineTransformIdentity
})
})
}
}