-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArticleViewController.swift
More file actions
187 lines (148 loc) · 6.53 KB
/
ArticleViewController.swift
File metadata and controls
187 lines (148 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// ArticleViewController.swift
// Example
//
// Created by Hirohisa Kawasaki on 2/13/15.
// Copyright (c) 2015 Hirohisa Kawasaki. All rights reserved.
//
import UIKit
import Union
class ArticleViewController: UIViewController {
let imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true
return imageView
}()
let backgroundView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.blackColor()
return view
}()
let textView: UITextView = {
let textView = UITextView()
textView.font = UIFont(name: "AppleSDGothicNeo-Regular", size: 15.0)
textView.contentInset = UIEdgeInsets(top: 30, left: 0, bottom: 0, right: 0)
return textView
}()
let iconView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true
imageView.image = UIImage(named: "dribbble")
imageView.sizeToFit()
return imageView
}()
let texts: NSArray = [
"Orange (voiced by Dane Boedigheimer) is the lead protagonist and main character,appearing in every episode since the series began on October 9, 2009.",
"He has yellow teeth, grey eyes, and a braying laugh.",
"He is known for his annoying puns which he uses in every episode,although hardly any of the fruits seem to find his puns amusing.",
"His standard reply, uttered after being taken to task for being annoying, is, 'I'm not annoying,', 'I'm an orange!', Unlike many fruits think, Orange doesn't have control over his tendency to be annoying.",
"It was suggested by Mango in 'It takes two to Mango' that Orange is annoying because of the carnage he has seen during his life.",
]
required init(imageView: UIImageView, center: CGPoint) {
self.imageView.image = imageView.image
self.imageView.bounds = imageView.bounds
self.imageView.center = center
backgroundView.center = center
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
title = "The Annoying Orange?"
edgesForExtendedLayout = .None
configureBackgroundView()
configureTextView()
configureIconView()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
navigationController?.delegate = nil
}
func configureBackgroundView() {
backgroundView.frame = view.frame
// background View
let mask = CAShapeLayer()
let center = imageView.center
let path = UIBezierPath(arcCenter: center, radius: CGRectGetHeight(imageView.frame)/2, startAngle: 0, endAngle: 360, clockwise: true)
mask.path = path.CGPath
backgroundView.layer.mask = mask
view.addSubview(backgroundView)
backgroundView.addSubview(imageView)
}
func configureTextView() {
let length = CGRectGetWidth(view.frame)
textView.frame = CGRect(
x: 0,
y: CGRectGetHeight(view.frame),
width: length,
height: CGRectGetHeight(view.frame) - 200
)
textView.text = texts.componentsJoinedByString("\n")
textView.showsVerticalScrollIndicator = false
view.addSubview(textView)
}
func configureIconView() {
iconView.center = CGPoint(x: CGRectGetWidth(view.frame) - 50, y: CGRectGetHeight(imageView.frame))
iconView.transform = CGAffineTransformMakeScale(0.0, 0.0)
view.addSubview(iconView)
}
}
extension ArticleViewController: Union.Delegate {
func animationsBeforeTransition(from fromViewController: UIViewController, to toViewController: UIViewController) -> [Animation] {
return []
}
func animationsDuringTransition(from fromViewController: UIViewController, to toViewController: UIViewController) -> [Animation] {
let switchAnimation = switchLayerAnimation()
let slideImgAnimation = slideImageViewAnimation()
slideImgAnimation.previous = switchAnimation
let slideTxtAnimation = slideTextViewAnimation()
slideTxtAnimation.previous = switchAnimation
return [revealAnimation(), switchAnimation, slideImgAnimation, slideTxtAnimation, scaleIconViewAnimation()]
}
func revealAnimation() -> Animation {
let mask = backgroundView.layer.mask as! CAShapeLayer
let startPath = mask.path
let endPath = UIBezierPath(arcCenter: imageView.center, radius: CGRectGetHeight(view.frame)/2, startAngle: 0, endAngle: 360, clockwise: true).CGPath
let animation = CABasicAnimation(keyPath: "path")
animation.fromValue = startPath
animation.toValue = endPath
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animation.duration = 0.7
return Animation(layer:mask, animation:animation)
}
func switchLayerAnimation() -> Animation {
let animation = Animation(duration: 0.3, animations: {})
animation.completion = { _ in
self.view.insertSubview(self.imageView, aboveSubview: self.backgroundView)
}
return animation
}
func slideImageViewAnimation() -> Animation {
let point = CGPoint(x: imageView.layer.position.x, y: 100)
let animation = CABasicAnimation(keyPath: "position")
animation.fromValue = NSValue(CGPoint: imageView.layer.position)
animation.toValue = NSValue(CGPoint: point)
animation.duration = 0.3
return Animation(layer:imageView.layer, animation:animation)
}
func slideTextViewAnimation() -> Animation {
let position = CGPoint(x: textView.layer.position.x, y: CGRectGetHeight(imageView.frame) + CGRectGetHeight(textView.frame)/2)
let animation = CABasicAnimation(keyPath: "position")
animation.fromValue = NSValue(CGPoint: textView.layer.position)
animation.toValue = NSValue(CGPoint: position)
animation.duration = 0.3
return Animation(layer:textView.layer, animation:animation)
}
func scaleIconViewAnimation() -> Animation {
let animation = CAKeyframeAnimation(keyPath: "transform.scale")
animation.values = [0.0, 0.1, 0.2, 0.3, 0.5]
animation.duration = 0.1
let a = Animation(layer:iconView.layer, animation:animation)
a.delay = 0.6
return a
}
}