Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Motion
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
Motion
Commits
d6904f6a
Commit
d6904f6a
authored
May 29, 2017
by
Daniel Dahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added base Motion class and MotionAnimation class
parent
5dc54456
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
1217 additions
and
0 deletions
+1217
-0
Motion.xcodeproj/project.pbxproj
+4
-0
Sources/Motion.swift
+307
-0
Sources/MotionAnimation.swift
+820
-0
Sources/MotionContext.swift
+59
-0
Sources/MotionController.swift
+27
-0
No files found.
Motion.xcodeproj/project.pbxproj
View file @
d6904f6a
...
...
@@ -10,6 +10,7 @@
961409AA1E43CF1B00E7BA99
/* Motion+Obj-C.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
961409A91E43CF1B00E7BA99
/* Motion+Obj-C.swift */
;
};
961409B61E43D17200E7BA99
/* Motion+Obj-C.swift in Headers */
=
{
isa
=
PBXBuildFile
;
fileRef
=
961409A91E43CF1B00E7BA99
/* Motion+Obj-C.swift */
;
settings
=
{
ATTRIBUTES
=
(
Public
,
);
};
};
961409B81E43D21300E7BA99
/* Motion.h in Headers */
=
{
isa
=
PBXBuildFile
;
fileRef
=
96C98DED1E438A5700B22906
/* Motion.h */
;
settings
=
{
ATTRIBUTES
=
(
Public
,
);
};
};
964C153D1EDCF6EA00F0869D
/* Motion.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
964C153C1EDCF6EA00F0869D
/* Motion.swift */
;
};
9657A6AC1EDA1601004461DE
/* MotionObserver.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
9657A6AB1EDA1601004461DE
/* MotionObserver.swift */
;
};
9657A6AE1EDA19D8004461DE
/* MotionAnimator.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
9657A6AD1EDA19D8004461DE
/* MotionAnimator.swift */
;
};
9657A6B31EDA63FC004461DE
/* MotionContext.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
9657A6B21EDA63FC004461DE
/* MotionContext.swift */
;
};
...
...
@@ -21,6 +22,7 @@
/* Begin PBXFileReference section */
961409A91E43CF1B00E7BA99
/* Motion+Obj-C.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
"Motion+Obj-C.swift"
;
sourceTree
=
"<group>"
;
};
964C153C1EDCF6EA00F0869D
/* Motion.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
Motion.swift
;
sourceTree
=
"<group>"
;
};
9657A6AB1EDA1601004461DE
/* MotionObserver.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
MotionObserver.swift
;
sourceTree
=
"<group>"
;
};
9657A6AD1EDA19D8004461DE
/* MotionAnimator.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
MotionAnimator.swift
;
sourceTree
=
"<group>"
;
};
9657A6B21EDA63FC004461DE
/* MotionContext.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
MotionContext.swift
;
sourceTree
=
"<group>"
;
};
...
...
@@ -65,6 +67,7 @@
96C98DE21E43809D00B22906
/* LICENSE */
,
96C98DDD1E424B9000B22906
/* Info.plist */
,
96C98DED1E438A5700B22906
/* Motion.h */
,
964C153C1EDCF6EA00F0869D
/* Motion.swift */
,
96C98DE31E4382B100B22906
/* MotionController.swift */
,
9657A6AB1EDA1601004461DE
/* MotionObserver.swift */
,
9657A6B21EDA63FC004461DE
/* MotionContext.swift */
,
...
...
@@ -163,6 +166,7 @@
9657A6AC1EDA1601004461DE
/* MotionObserver.swift in Sources */
,
9657A6B31EDA63FC004461DE
/* MotionContext.swift in Sources */
,
961409AA1E43CF1B00E7BA99
/* Motion+Obj-C.swift in Sources */
,
964C153D1EDCF6EA00F0869D
/* Motion.swift in Sources */
,
96C98DE41E4382B100B22906
/* MotionController.swift in Sources */
,
);
runOnlyForDeploymentPostprocessing
=
0
;
...
...
Sources/Motion.swift
0 → 100644
View file @
d6904f6a
/*
* Copyright (C) 2015 - 2017, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of CosmicMind nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import
UIKit
@objc(MotionAnimationFillMode)
public
enum
MotionAnimationFillMode
:
Int
{
case
forwards
case
backwards
case
both
case
removed
}
/**
Converts the MotionAnimationFillMode enum value to a corresponding String.
- Parameter mode: An MotionAnimationFillMode enum value.
*/
public
func
MotionAnimationFillModeToValue
(
mode
:
MotionAnimationFillMode
)
->
String
{
switch
mode
{
case
.
forwards
:
return
kCAFillModeForwards
case
.
backwards
:
return
kCAFillModeBackwards
case
.
both
:
return
kCAFillModeBoth
case
.
removed
:
return
kCAFillModeRemoved
}
}
@objc(MotionAnimationTimingFunction)
public
enum
MotionAnimationTimingFunction
:
Int
{
case
`
default
`
case
linear
case
easeIn
case
easeOut
case
easeInEaseOut
}
/**
Converts the MotionAnimationTimingFunction enum value to a corresponding CAMediaTimingFunction.
- Parameter function: An MotionAnimationTimingFunction enum value.
- Returns: A CAMediaTimingFunction.
*/
public
func
MotionAnimationTimingFunctionToValue
(
timingFunction
:
MotionAnimationTimingFunction
)
->
CAMediaTimingFunction
{
switch
timingFunction
{
case
.
default
:
return
CAMediaTimingFunction
(
name
:
kCAMediaTimingFunctionDefault
)
case
.
linear
:
return
CAMediaTimingFunction
(
name
:
kCAMediaTimingFunctionLinear
)
case
.
easeIn
:
return
CAMediaTimingFunction
(
name
:
kCAMediaTimingFunctionEaseIn
)
case
.
easeOut
:
return
CAMediaTimingFunction
(
name
:
kCAMediaTimingFunctionEaseOut
)
case
.
easeInEaseOut
:
return
CAMediaTimingFunction
(
name
:
kCAMediaTimingFunctionEaseInEaseOut
)
}
}
public
typealias
MotionDelayCancelBlock
=
(
Bool
)
->
Void
fileprivate
var
MotionInstanceKey
:
UInt8
=
0
fileprivate
var
MotionInstanceControllerKey
:
UInt8
=
0
fileprivate
struct
MotionInstance
{
/// An optional reference to the motion identifier.
fileprivate
var
identifier
:
String
?
/// An optional reference to the motion animations.
fileprivate
var
animations
:
[
MotionAnimation
]?
}
fileprivate
struct
MotionInstanceController
{
/// A boolean indicating whether Motion is enabled.
fileprivate
var
isMotionEnabled
:
Bool
}
extension
UIViewController
{
/// MotionInstanceController reference.
fileprivate
var
motionControllerInstance
:
MotionInstanceController
{
get
{
return
AssociatedObject
.
get
(
base
:
self
,
key
:
&
MotionInstanceControllerKey
)
{
return
MotionInstanceController
(
isMotionEnabled
:
false
)
}
}
set
(
value
)
{
AssociatedObject
.
set
(
base
:
self
,
key
:
&
MotionInstanceControllerKey
,
value
:
value
)
}
}
/// A boolean that indicates whether motion is enabled.
open
var
isMotionEnabled
:
Bool
{
get
{
return
motionControllerInstance
.
isMotionEnabled
}
set
(
value
)
{
motionControllerInstance
.
isMotionEnabled
=
value
}
}
}
extension
UIView
{
/// MotionInstance reference.
fileprivate
var
motionInstance
:
MotionInstance
{
get
{
return
AssociatedObject
.
get
(
base
:
self
,
key
:
&
MotionInstanceKey
)
{
return
MotionInstance
(
identifier
:
nil
,
animations
:
nil
)
}
}
set
(
value
)
{
AssociatedObject
.
set
(
base
:
self
,
key
:
&
MotionInstanceKey
,
value
:
value
)
}
}
/// An identifier value used to connect views across UIViewControllers.
open
var
motionIdentifier
:
String
?
{
get
{
return
motionInstance
.
identifier
}
set
(
value
)
{
motionInstance
.
identifier
=
value
}
}
/// The animations to run while in transition.
open
var
motionAnimations
:
[
MotionAnimation
]?
{
get
{
return
motionInstance
.
animations
}
set
(
value
)
{
motionInstance
.
animations
=
value
}
}
}
extension
UIView
{
/**
Snapshots the view instance for animations during transitions.
- Parameter afterUpdates: A boolean indicating whether to snapshot the view
after a render update, or as is.
- Parameter shouldHide: A boolean indicating whether the view should be hidden
after the snapshot is taken.
- Returns: A UIView instance that is a snapshot of the given UIView.
*/
open
func
transitionSnapshot
(
afterUpdates
:
Bool
,
shouldHide
:
Bool
=
true
)
->
UIView
{
isHidden
=
false
let
oldCornerRadius
=
layer
.
cornerRadius
layer
.
cornerRadius
=
0
var
oldBackgroundColor
:
UIColor
?
if
shouldHide
{
oldBackgroundColor
=
backgroundColor
backgroundColor
=
.
clear
}
let
oldTransform
=
motionTransform
motionTransform
=
CATransform3DIdentity
let
v
=
snapshotView
(
afterScreenUpdates
:
afterUpdates
)
!
layer
.
cornerRadius
=
oldCornerRadius
if
shouldHide
{
backgroundColor
=
oldBackgroundColor
}
motionTransform
=
oldTransform
let
contentView
=
v
.
subviews
.
first
!
contentView
.
layer
.
cornerRadius
=
layer
.
cornerRadius
contentView
.
layer
.
masksToBounds
=
true
v
.
motionIdentifier
=
motionIdentifier
v
.
layer
.
position
=
motionPosition
v
.
bounds
=
bounds
v
.
layer
.
cornerRadius
=
layer
.
cornerRadius
v
.
layer
.
zPosition
=
layer
.
zPosition
v
.
layer
.
opacity
=
layer
.
opacity
v
.
isOpaque
=
isOpaque
v
.
layer
.
anchorPoint
=
layer
.
anchorPoint
v
.
layer
.
masksToBounds
=
layer
.
masksToBounds
v
.
layer
.
borderColor
=
layer
.
borderColor
v
.
layer
.
borderWidth
=
layer
.
borderWidth
v
.
layer
.
shadowRadius
=
layer
.
shadowRadius
v
.
layer
.
shadowOpacity
=
layer
.
shadowOpacity
v
.
layer
.
shadowColor
=
layer
.
shadowColor
v
.
layer
.
shadowOffset
=
layer
.
shadowOffset
v
.
contentMode
=
contentMode
v
.
motionTransform
=
motionTransform
v
.
backgroundColor
=
backgroundColor
isHidden
=
shouldHide
return
v
}
}
open
class
Motion
{
/**
Executes a block of code after a time delay.
- Parameter duration: An animation duration time.
- Parameter animations: An animation block.
- Parameter execute block: A completion block that is executed once
the animations have completed.
*/
@discardableResult
open
class
func
delay
(
_
time
:
TimeInterval
,
execute
block
:
@escaping
()
->
Void
)
->
MotionDelayCancelBlock
?
{
func
asyncAfter
(
completion
:
@escaping
()
->
Void
)
{
DispatchQueue
.
main
.
asyncAfter
(
deadline
:
.
now
()
+
time
,
execute
:
completion
)
}
var
cancelable
:
MotionDelayCancelBlock
?
let
delayed
:
MotionDelayCancelBlock
=
{
if
!
$0
{
DispatchQueue
.
main
.
async
(
execute
:
block
)
}
cancelable
=
nil
}
cancelable
=
delayed
asyncAfter
{
cancelable
?(
false
)
}
return
cancelable
}
/**
Cancels the delayed MotionDelayCancelBlock.
- Parameter delayed completion: An MotionDelayCancelBlock.
*/
open
class
func
cancel
(
delayed
completion
:
MotionDelayCancelBlock
)
{
completion
(
true
)
}
/**
Disables the default animations set on CALayers.
- Parameter animations: A callback that wraps the animations to disable.
*/
open
class
func
disable
(
_
animations
:
(()
->
Void
))
{
animate
(
duration
:
0
,
animations
:
animations
)
}
/**
Runs an animation with a specified duration.
- Parameter duration: An animation duration time.
- Parameter animations: An animation block.
- Parameter timingFunction: An MotionAnimationTimingFunction value.
- Parameter completion: A completion block that is executed once
the animations have completed.
*/
open
class
func
animate
(
duration
:
CFTimeInterval
,
timingFunction
:
MotionAnimationTimingFunction
=
.
easeInEaseOut
,
animations
:
(()
->
Void
),
completion
:
(()
->
Void
)?
=
nil
)
{
CATransaction
.
begin
()
CATransaction
.
setAnimationDuration
(
duration
)
CATransaction
.
setCompletionBlock
(
completion
)
CATransaction
.
setAnimationTimingFunction
(
MotionAnimationTimingFunctionToValue
(
timingFunction
:
timingFunction
))
animations
()
CATransaction
.
commit
()
}
/**
Creates a CAAnimationGroup.
- Parameter animations: An Array of CAAnimation objects.
- Parameter timingFunction: An MotionAnimationTimingFunction value.
- Parameter duration: An animation duration time for the group.
- Returns: A CAAnimationGroup.
*/
open
class
func
animate
(
group
animations
:
[
CAAnimation
],
timingFunction
:
MotionAnimationTimingFunction
=
.
easeInEaseOut
,
duration
:
CFTimeInterval
=
0.5
)
->
CAAnimationGroup
{
let
group
=
CAAnimationGroup
()
group
.
fillMode
=
MotionAnimationFillModeToValue
(
mode
:
.
forwards
)
group
.
isRemovedOnCompletion
=
false
group
.
animations
=
animations
group
.
duration
=
duration
group
.
timingFunction
=
MotionAnimationTimingFunctionToValue
(
timingFunction
:
timingFunction
)
return
group
}
}
Sources/MotionAnimation.swift
View file @
d6904f6a
...
...
@@ -30,3 +30,823 @@
import
UIKit
public
enum
MotionAnimationKeyPath
:
String
{
case
backgroundColor
case
barTintColor
case
borderColor
case
borderWidth
case
cornerRadius
case
transform
case
rotation
=
"transform.rotation"
case
rotationX
=
"transform.rotation.x"
case
rotationY
=
"transform.rotation.y"
case
rotationZ
=
"transform.rotation.z"
case
scale
=
"transform.scale"
case
scaleX
=
"transform.scale.x"
case
scaleY
=
"transform.scale.y"
case
scaleZ
=
"transform.scale.z"
case
translation
=
"transform.translation"
case
translationX
=
"transform.translation.x"
case
translationY
=
"transform.translation.y"
case
translationZ
=
"transform.translation.z"
case
position
case
opacity
case
zPosition
case
width
=
"bounds.size.width"
case
height
=
"bounds.size.height"
case
size
=
"bounds.size"
case
shadowPath
case
shadowOffset
case
shadowOpacity
case
shadowRadius
}
public
enum
MotionAnimation
{
case
delay
(
TimeInterval
)
case
timingFunction
(
MotionAnimationTimingFunction
)
case
duration
(
TimeInterval
)
case
custom
(
CABasicAnimation
)
case
backgroundColor
(
UIColor
)
case
barTintColor
(
UIColor
)
case
borderColor
(
UIColor
)
case
borderWidth
(
CGFloat
)
case
cornerRadius
(
CGFloat
)
case
transform
(
CATransform3D
)
case
rotationAngle
(
CGFloat
)
case
rotationAngleX
(
CGFloat
)
case
rotationAngleY
(
CGFloat
)
case
rotationAngleZ
(
CGFloat
)
case
spin
(
CGFloat
)
case
spinX
(
CGFloat
)
case
spinY
(
CGFloat
)
case
spinZ
(
CGFloat
)
case
scale
(
CGFloat
)
case
scaleX
(
CGFloat
)
case
scaleY
(
CGFloat
)
case
scaleZ
(
CGFloat
)
case
translate
(
x
:
CGFloat
,
y
:
CGFloat
)
case
translateX
(
CGFloat
)
case
translateY
(
CGFloat
)
case
translateZ
(
CGFloat
)
case
x
(
CGFloat
)
case
y
(
CGFloat
)
case
point
(
x
:
CGFloat
,
y
:
CGFloat
)
case
position
(
x
:
CGFloat
,
y
:
CGFloat
)
case
fade
(
Double
)
case
zPosition
(
Int
)
case
width
(
CGFloat
)
case
height
(
CGFloat
)
case
size
(
width
:
CGFloat
,
height
:
CGFloat
)
case
shadowPath
(
CGPath
)
case
shadowOffset
(
CGSize
)
case
shadowOpacity
(
Float
)
case
shadowRadius
(
CGFloat
)
case
depth
(
shadowOffset
:
CGSize
,
shadowOpacity
:
Float
,
shadowRadius
:
CGFloat
)
}
@available(iOS 10, *)
extension
CALayer
:
CAAnimationDelegate
{}
extension
CALayer
{
/**
A function that accepts CAAnimation objects and executes them on the
view's backing layer.
- Parameter animation: A CAAnimation instance.
*/
open
func
animate
(
_
animations
:
CAAnimation
...
)
{
animate
(
animations
)
}
/**
A function that accepts CAAnimation objects and executes them on the
view's backing layer.
- Parameter animation: A CAAnimation instance.
*/
open
func
animate
(
_
animations
:
[
CAAnimation
])
{
for
animation
in
animations
{
if
nil
==
animation
.
delegate
{
animation
.
delegate
=
self
}
if
let
a
=
animation
as?
CABasicAnimation
{
a
.
fromValue
=
(
presentation
()
??
self
)
.
value
(
forKeyPath
:
a
.
keyPath
!
)
}
if
let
a
=
animation
as?
CAPropertyAnimation
{
add
(
a
,
forKey
:
a
.
keyPath
!
)
}
else
if
let
a
=
animation
as?
CAAnimationGroup
{
add
(
a
,
forKey
:
nil
)
}
else
if
let
a
=
animation
as?
CATransition
{
add
(
a
,
forKey
:
kCATransition
)
}
}
}
open
func
animationDidStart
(
_
anim
:
CAAnimation
)
{}
/**
A delegation function that is executed when the backing layer stops
running an animation.
- Parameter animation: The CAAnimation instance that stopped running.
- Parameter flag: A boolean that indicates if the animation stopped
because it was completed or interrupted. True if completed, false
if interrupted.
*/
open
func
animationDidStop
(
_
anim
:
CAAnimation
,
finished
flag
:
Bool
)
{
guard
let
a
=
anim
as?
CAPropertyAnimation
else
{
if
let
a
=
(
anim
as?
CAAnimationGroup
)?
.
animations
{
for
x
in
a
{
animationDidStop
(
x
,
finished
:
true
)
}
}
return
}
guard
let
b
=
a
as?
CABasicAnimation
else
{
return
}
guard
let
v
=
b
.
toValue
else
{
return
}
guard
let
k
=
b
.
keyPath
else
{
return
}
setValue
(
v
,
forKeyPath
:
k
)
removeAnimation
(
forKey
:
k
)
}
/**
A function that accepts a list of MotionAnimation values and executes them.
- Parameter animations: A list of MotionAnimation values.
*/
open
func
motion
(
_
animations
:
MotionAnimation
...
)
{
motion
(
animations
)
}
/**
A function that accepts an Array of MotionAnimation values and executes them.
- Parameter animations: An Array of MotionAnimation values.
- Parameter completion: An optional completion block.
*/
open
func
motion
(
_
animations
:
[
MotionAnimation
],
completion
:
(()
->
Void
)?
=
nil
)
{
motion
(
delay
:
0
,
duration
:
0.35
,
timingFunction
:
.
easeInEaseOut
,
animations
:
animations
,
completion
:
completion
)
}
/**
A function that executes an Array of MotionAnimation values.
- Parameter delay: The animation delay TimeInterval.
- Parameter duration: The animation duration TimeInterval.
- Parameter timingFunction: The animation MotionAnimationTimingFunction.
- Parameter animations: An Array of MotionAnimations.
- Parameter completion: An optional completion block.
*/
fileprivate
func
motion
(
delay
:
TimeInterval
,
duration
:
TimeInterval
,
timingFunction
:
MotionAnimationTimingFunction
,
animations
:
[
MotionAnimation
],
completion
:
(()
->
Void
)?
=
nil
)
{
var
t
=
delay
for
v
in
animations
{
switch
v
{
case
let
.
delay
(
time
):
t
=
time
default
:
break
}
}
Motion
.
delay
(
t
)
{
[
weak
self
]
in
guard
let
s
=
self
else
{
return
}
var
a
=
[
CABasicAnimation
]()
var
tf
=
timingFunction
var
d
=
duration
var
w
:
CGFloat
=
s
.
bounds
.
width
var
h
:
CGFloat
=
s
.
bounds
.
height
for
v
in
animations
{
switch
v
{
case
let
.
width
(
width
):
w
=
width
case
let
.
height
(
height
):
h
=
height
case
let
.
size
(
width
,
height
):
w
=
width
h
=
height
default
:
break
}
}
var
px
:
CGFloat
=
s
.
position
.
x
var
py
:
CGFloat
=
s
.
position
.
y
for
v
in
animations
{
switch
v
{
case
let
.
x
(
x
):
px
=
x
+
w
/
2
case
let
.
y
(
y
):
py
=
y
+
h
/
2
case
let
.
point
(
x
,
y
):
px
=
x
+
w
/
2
py
=
y
+
h
/
2
default
:
break
}
}
for
v
in
animations
{
switch
v
{
case
let
.
timingFunction
(
timingFunction
):
tf
=
timingFunction
case
let
.
duration
(
duration
):
d
=
duration
case
let
.
custom
(
animation
):
a
.
append
(
animation
)
case
let
.
backgroundColor
(
color
):
a
.
append
(
MotionBasicAnimation
.
background
(
color
:
color
))
case
let
.
barTintColor
(
color
):
a
.
append
(
MotionBasicAnimation
.
barTint
(
color
:
color
))
case
let
.
borderColor
(
color
):
a
.
append
(
MotionBasicAnimation
.
border
(
color
:
color
))
case
let
.
borderWidth
(
width
):
a
.
append
(
MotionBasicAnimation
.
border
(
width
:
width
))
case
let
.
cornerRadius
(
radius
):
a
.
append
(
MotionBasicAnimation
.
corner
(
radius
:
radius
))
case
let
.
transform
(
transform
):
a
.
append
(
MotionBasicAnimation
.
transform
(
transform
:
transform
))
case
let
.
rotationAngle
(
angle
):
let
rotate
=
MotionBasicAnimation
.
rotation
(
angle
:
angle
)
a
.
append
(
rotate
)
case
let
.
rotationAngleX
(
angle
):
a
.
append
(
MotionBasicAnimation
.
rotationX
(
angle
:
angle
))
case
let
.
rotationAngleY
(
angle
):
a
.
append
(
MotionBasicAnimation
.
rotationY
(
angle
:
angle
))
case
let
.
rotationAngleZ
(
angle
):
a
.
append
(
MotionBasicAnimation
.
rotationZ
(
angle
:
angle
))
case
let
.
spin
(
rotations
):
a
.
append
(
MotionBasicAnimation
.
spin
(
rotations
:
rotations
))
case
let
.
spinX
(
rotations
):
a
.
append
(
MotionBasicAnimation
.
spinX
(
rotations
:
rotations
))
case
let
.
spinY
(
rotations
):
a
.
append
(
MotionBasicAnimation
.
spinY
(
rotations
:
rotations
))
case
let
.
spinZ
(
rotations
):
a
.
append
(
MotionBasicAnimation
.
spinZ
(
rotations
:
rotations
))
case
let
.
scale
(
to
):
a
.
append
(
MotionBasicAnimation
.
scale
(
to
:
to
))
case
let
.
scaleX
(
to
):
a
.
append
(
MotionBasicAnimation
.
scaleX
(
to
:
to
))
case
let
.
scaleY
(
to
):
a
.
append
(
MotionBasicAnimation
.
scaleY
(
to
:
to
))
case
let
.
scaleZ
(
to
):
a
.
append
(
MotionBasicAnimation
.
scaleZ
(
to
:
to
))
case
let
.
translate
(
x
,
y
):
a
.
append
(
MotionBasicAnimation
.
translate
(
to
:
CGPoint
(
x
:
x
,
y
:
y
)))
case
let
.
translateX
(
to
):
a
.
append
(
MotionBasicAnimation
.
translateX
(
to
:
to
))
case
let
.
translateY
(
to
):
a
.
append
(
MotionBasicAnimation
.
translateY
(
to
:
to
))
case
let
.
translateZ
(
to
):
a
.
append
(
MotionBasicAnimation
.
translateZ
(
to
:
to
))
case
.
x
(
_
),
.
y
(
_
),
.
point
(
_
,
_
):
let
position
=
MotionBasicAnimation
.
position
(
to
:
CGPoint
(
x
:
px
,
y
:
py
))
a
.
append
(
position
)
case
let
.
position
(
x
,
y
):
a
.
append
(
MotionBasicAnimation
.
position
(
to
:
CGPoint
(
x
:
x
,
y
:
y
)))
case
let
.
fade
(
opacity
):
let
fade
=
MotionBasicAnimation
.
fade
(
opacity
:
opacity
)
fade
.
fromValue
=
s
.
value
(
forKey
:
MotionAnimationKeyPath
.
opacity
.
rawValue
)
??
NSNumber
(
floatLiteral
:
1
)
a
.
append
(
fade
)
case
let
.
zPosition
(
index
):
let
zPosition
=
MotionBasicAnimation
.
zPosition
(
index
:
index
)
zPosition
.
fromValue
=
s
.
value
(
forKey
:
MotionAnimationKeyPath
.
zPosition
.
rawValue
)
??
NSNumber
(
integerLiteral
:
0
)
a
.
append
(
zPosition
)
case
.
width
(
_
),
.
height
(
_
),
.
size
(
_
,
_
):
a
.
append
(
MotionBasicAnimation
.
size
(
CGSize
(
width
:
w
,
height
:
h
)))
case
let
.
shadowPath
(
path
):
let
shadowPath
=
MotionBasicAnimation
.
shadow
(
path
:
path
)
shadowPath
.
fromValue
=
s
.
shadowPath
a
.
append
(
shadowPath
)
case
let
.
shadowOffset
(
offset
):
let
shadowOffset
=
MotionBasicAnimation
.
shadow
(
offset
:
offset
)
shadowOffset
.
fromValue
=
s
.
shadowOffset
a
.
append
(
shadowOffset
)
case
let
.
shadowOpacity
(
opacity
):
let
shadowOpacity
=
MotionBasicAnimation
.
shadow
(
opacity
:
opacity
)
shadowOpacity
.
fromValue
=
s
.
shadowOpacity
a
.
append
(
shadowOpacity
)
case
let
.
shadowRadius
(
radius
):
let
shadowRadius
=
MotionBasicAnimation
.
shadow
(
radius
:
radius
)
shadowRadius
.
fromValue
=
s
.
shadowRadius
a
.
append
(
shadowRadius
)
case
let
.
depth
(
offset
,
opacity
,
radius
):
if
let
path
=
s
.
shadowPath
{
let
shadowPath
=
MotionBasicAnimation
.
shadow
(
path
:
path
)
shadowPath
.
fromValue
=
s
.
shadowPath
a
.
append
(
shadowPath
)
}
let
shadowOffset
=
MotionBasicAnimation
.
shadow
(
offset
:
offset
)
shadowOffset
.
fromValue
=
s
.
shadowOffset
a
.
append
(
shadowOffset
)
let
shadowOpacity
=
MotionBasicAnimation
.
shadow
(
opacity
:
opacity
)
shadowOpacity
.
fromValue
=
s
.
shadowOpacity
a
.
append
(
shadowOpacity
)
let
shadowRadius
=
MotionBasicAnimation
.
shadow
(
radius
:
radius
)
shadowRadius
.
fromValue
=
s
.
shadowRadius
a
.
append
(
shadowRadius
)
default
:
break
}
}
let
g
=
Motion
.
animate
(
group
:
a
,
duration
:
d
)
g
.
fillMode
=
MotionAnimationFillModeToValue
(
mode
:
.
forwards
)
g
.
isRemovedOnCompletion
=
false
g
.
timingFunction
=
MotionAnimationTimingFunctionToValue
(
timingFunction
:
tf
)
s
.
animate
(
g
)
guard
let
execute
=
completion
else
{
return
}
Motion
.
delay
(
d
,
execute
:
execute
)
}
}
}
extension
UIView
{
/// Computes the rotation of the view.
open
var
motionRotationAngle
:
CGFloat
{
get
{
return
CGFloat
(
atan2f
(
Float
(
transform
.
b
),
Float
(
transform
.
a
)))
*
180
/
CGFloat
(
Double
.
pi
)
}
set
(
value
)
{
transform
=
CGAffineTransform
(
rotationAngle
:
CGFloat
(
Double
.
pi
)
*
value
/
180
)
}
}
/// The global position of a view.
open
var
motionPosition
:
CGPoint
{
return
superview
?
.
convert
(
layer
.
position
,
to
:
nil
)
??
layer
.
position
}
/// The layer.transform of a view.
open
var
motionTransform
:
CATransform3D
{
get
{
return
layer
.
transform
}
set
(
value
)
{
layer
.
transform
=
value
}
}
/// Computes the scale X axis value of the view.
open
var
motionScaleX
:
CGFloat
{
return
transform
.
a
}
/// Computes the scale Y axis value of the view.
open
var
motionScaleY
:
CGFloat
{
return
transform
.
b
}
/**
A function that accepts CAAnimation objects and executes them on the
view's backing layer.
- Parameter animations: A list of CAAnimations.
*/
open
func
animate
(
_
animations
:
CAAnimation
...
)
{
layer
.
animate
(
animations
)
}
/**
A function that accepts an Array of CAAnimation objects and executes
them on the view's backing layer.
- Parameter animations: An Array of CAAnimations.
*/
open
func
animate
(
_
animations
:
[
CAAnimation
])
{
layer
.
animate
(
animations
)
}
/**
A function that accepts a list of MotionAnimation values and executes
them on the view's backing layer.
- Parameter animations: A list of MotionAnimation values.
*/
open
func
motion
(
_
animations
:
MotionAnimation
...
)
{
layer
.
motion
(
animations
)
}
/**
A function that accepts an Array of MotionAnimation values and executes
them on the view's backing layer.
- Parameter animations: An Array of MotionAnimation values.
- Parameter completion: An optional completion block.
*/
open
func
motion
(
_
animations
:
[
MotionAnimation
],
completion
:
(()
->
Void
)?
=
nil
)
{
layer
.
motion
(
animations
,
completion
:
completion
)
}
}
extension
CABasicAnimation
{
/**
A convenience initializer that takes a given MotionAnimationKeyPath.
- Parameter keyPath: An MotionAnimationKeyPath.
*/
public
convenience
init
(
keyPath
:
MotionAnimationKeyPath
)
{
self
.
init
(
keyPath
:
keyPath
.
rawValue
)
}
}
extension
CAKeyframeAnimation
{
/**
A convenience initializer that takes a given MotionAnimationKeyPath.
- Parameter keyPath: An MotionAnimationKeyPath.
*/
public
convenience
init
(
keyPath
:
MotionAnimationKeyPath
)
{
self
.
init
(
keyPath
:
keyPath
.
rawValue
)
}
}
public
struct
MotionKeyframeAnimation
{
/**
Creates a CABasicAnimation for the backgroundColor key path.
- Parameter color: A UIColor.
- Returns: A CABasicAnimation.
*/
public
static
func
background
(
color
:
UIColor
)
->
CAKeyframeAnimation
{
let
animation
=
CAKeyframeAnimation
(
keyPath
:
.
backgroundColor
)
animation
.
values
=
[
color
.
cgColor
]
return
animation
}
}
public
struct
MotionBasicAnimation
{
/**
Creates a CABasicAnimation for the backgroundColor key path.
- Parameter color: A UIColor.
- Returns: A CABasicAnimation.
*/
public
static
func
background
(
color
:
UIColor
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
backgroundColor
)
animation
.
toValue
=
color
.
cgColor
return
animation
}
/**
Creates a CABasicAnimation for the barTintColor key path.
- Parameter color: A UIColor.
- Returns: A CABasicAnimation.
*/
public
static
func
barTint
(
color
:
UIColor
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
barTintColor
)
animation
.
toValue
=
color
.
cgColor
return
animation
}
/**
Creates a CABasicAnimation for the borderColor key path.
- Parameter color: A UIColor.
- Returns: A CABasicAnimation.
*/
public
static
func
border
(
color
:
UIColor
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
borderColor
)
animation
.
toValue
=
color
.
cgColor
return
animation
}
/**
Creates a CABasicAnimation for the borderWidth key path.
- Parameter width: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
border
(
width
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
borderWidth
)
animation
.
toValue
=
width
return
animation
}
/**
Creates a CABasicAnimation for the cornerRadius key path.
- Parameter radius: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
corner
(
radius
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
cornerRadius
)
animation
.
toValue
=
radius
return
animation
}
/**
Creates a CABasicAnimation for the transform key path.
- Parameter transform: A CATransform3D object.
- Returns: A CABasicAnimation.
*/
public
static
func
transform
(
transform
:
CATransform3D
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
transform
)
animation
.
toValue
=
NSValue
(
caTransform3D
:
transform
)
return
animation
}
/**
Creates a CABasicAnimation for the transform.rotation key path.
- Parameter angle: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
rotation
(
angle
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
rotation
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
CGFloat
(
Double
.
pi
)
*
angle
/
180
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.rotation.x key path.
- Parameter angle: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
rotationX
(
angle
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
rotationX
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
CGFloat
(
Double
.
pi
)
*
angle
/
180
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.rotation.y key path.
- Parameter angle: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
rotationY
(
angle
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
rotationY
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
CGFloat
(
Double
.
pi
)
*
angle
/
180
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.rotation.z key path.
- Parameter angle: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
rotationZ
(
angle
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
rotationZ
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
CGFloat
(
Double
.
pi
)
*
angle
/
180
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.rotation key path.
- Parameter rotations: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
spin
(
rotations
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
rotation
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
CGFloat
(
Double
.
pi
)
*
2
*
rotations
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.rotation.x key path.
- Parameter rotations: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
spinX
(
rotations
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
rotationX
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
CGFloat
(
Double
.
pi
)
*
2
*
rotations
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.rotation.y key path.
- Parameter rotations: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
spinY
(
rotations
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
rotationY
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
CGFloat
(
Double
.
pi
)
*
2
*
rotations
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.rotation.z key path.
- Parameter rotations: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
spinZ
(
rotations
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
rotationZ
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
CGFloat
(
Double
.
pi
)
*
2
*
rotations
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.scale key path.
- Parameter to scale: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
scale
(
to
scale
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
scale
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
scale
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.scale.x key path.
- Parameter to scale: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
scaleX
(
to
scale
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
scaleX
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
scale
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.scale.y key path.
- Parameter to scale: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
scaleY
(
to
scale
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
scaleY
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
scale
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.scale.z key path.
- Parameter to scale: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
scaleZ
(
to
scale
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
scaleZ
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
scale
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.translation key path.
- Parameter point: A CGPoint.
- Returns: A CABasicAnimation.
*/
public
static
func
translate
(
to
point
:
CGPoint
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
translation
)
animation
.
toValue
=
NSValue
(
cgPoint
:
point
)
return
animation
}
/**
Creates a CABasicAnimation for the transform.translation.x key path.
- Parameter to translation: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
translateX
(
to
translation
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
translationX
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
translation
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.translation.y key path.
- Parameter to translation: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
translateY
(
to
translation
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
translationY
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
translation
))
return
animation
}
/**
Creates a CABasicAnimation for the transform.translation.z key path.
- Parameter to translation: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
translateZ
(
to
translation
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
translationZ
)
animation
.
toValue
=
NSNumber
(
value
:
Double
(
translation
))
return
animation
}
/**
Creates a CABasicAnimation for the position key path.
- Parameter x: A CGFloat.
- Parameter y: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
position
(
x
:
CGFloat
,
y
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
position
)
animation
.
toValue
=
NSValue
(
cgPoint
:
CGPoint
(
x
:
x
,
y
:
y
))
return
animation
}
/**
Creates a CABasicAnimation for the position key path.
- Parameter to point: A CGPoint.
- Returns: A CABasicAnimation.
*/
public
static
func
position
(
to
point
:
CGPoint
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
position
)
animation
.
toValue
=
NSValue
(
cgPoint
:
point
)
return
animation
}
/**
Creates a CABasicAnimation for the opacity key path.
- Parameter opacity: A Double.
- Returns: A CABasicAnimation.
*/
public
static
func
fade
(
opacity
:
Double
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
opacity
)
animation
.
toValue
=
NSNumber
(
floatLiteral
:
opacity
)
return
animation
}
/**
Creates a CABasicaAnimation for the zPosition key path.
- Parameter index: An Int.
- Returns: A CABasicAnimation.
*/
public
static
func
zPosition
(
index
:
Int
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
zPosition
)
animation
.
toValue
=
NSNumber
(
integerLiteral
:
index
)
return
animation
}
/**
Creates a CABasicaAnimation for the width key path.
- Parameter width: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
width
(
_
width
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
width
)
animation
.
toValue
=
NSNumber
(
floatLiteral
:
Double
(
width
))
return
animation
}
/**
Creates a CABasicaAnimation for the height key path.
- Parameter height: A CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
height
(
_
height
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
height
)
animation
.
toValue
=
NSNumber
(
floatLiteral
:
Double
(
height
))
return
animation
}
/**
Creates a CABasicaAnimation for the height key path.
- Parameter size: A CGSize.
- Returns: A CABasicAnimation.
*/
public
static
func
size
(
_
size
:
CGSize
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
size
)
animation
.
toValue
=
NSValue
(
cgSize
:
size
)
return
animation
}
/**
Creates a CABasicAnimation for the shadowPath key path.
- Parameter path: A CGPath.
- Returns: A CABasicAnimation.
*/
public
static
func
shadow
(
path
:
CGPath
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
shadowPath
)
animation
.
toValue
=
path
return
animation
}
/**
Creates a CABasicAnimation for the shadowOffset key path.
- Parameter offset: CGSize.
- Returns: A CABasicAnimation.
*/
public
static
func
shadow
(
offset
:
CGSize
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
shadowOffset
)
animation
.
toValue
=
NSValue
(
cgSize
:
offset
)
return
animation
}
/**
Creates a CABasicAnimation for the shadowOpacity key path.
- Parameter opacity: Float.
- Returns: A CABasicAnimation.
*/
public
static
func
shadow
(
opacity
:
Float
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
shadowOpacity
)
animation
.
toValue
=
NSNumber
(
floatLiteral
:
Double
(
opacity
))
return
animation
}
/**
Creates a CABasicAnimation for the shadowRadius key path.
- Parameter radius: CGFloat.
- Returns: A CABasicAnimation.
*/
public
static
func
shadow
(
radius
:
CGFloat
)
->
CABasicAnimation
{
let
animation
=
CABasicAnimation
(
keyPath
:
.
shadowRadius
)
animation
.
toValue
=
NSNumber
(
floatLiteral
:
Double
(
radius
))
return
animation
}
}
Sources/MotionContext.swift
View file @
d6904f6a
...
...
@@ -31,5 +31,64 @@
import
UIKit
open
class
MotionContext
{
/// A reference to the transition container.
fileprivate
var
transitionContainer
:
UIView
/// An index source of identifiers to their corresponding view.
fileprivate
var
transitionSourceIdentifierToView
=
[
String
:
UIView
]()
/// An index of destination identifiers to their corresponding view.
fileprivate
var
transitionDestinationIdentifierToView
=
[
String
:
UIView
]()
/// An index of views to their corresponding snapshot view.
fileprivate
var
transitionSnapshotToView
=
[
UIView
:
UIView
]()
/// A reference to the transition from views.
fileprivate
var
fromViews
:
[
UIView
]
!
/// A reference to the transition to views.
fileprivate
var
toViews
:
[
UIView
]
!
/**
An initializer that accepts a given transition container view.
- Parameter transitionContainer: A UIView.
*/
init
(
transitionContainer
:
UIView
)
{
self
.
transitionContainer
=
transitionContainer
}
}
extension
MotionContext
{
/**
Prepares the source views to their identifiers.
- Parameter views: An Array of UIViews.
- Parameter identifierIndex: An Dictionary of identifiers to UIViews.
*/
fileprivate
func
prepare
(
views
:
[
UIView
],
identifierIndex
:
inout
[
String
:
UIView
])
{
for
v
in
views
{
v
.
layer
.
removeAllAnimations
()
guard
transitionContainer
.
convert
(
v
.
bounds
,
from
:
v
)
.
intersects
(
transitionContainer
.
bounds
)
else
{
return
}
if
let
i
=
v
.
motionIdentifier
{
identifierIndex
[
i
]
=
v
}
}
}
}
extension
MotionContext
{
/**
Sets the views that will transition from one state to another.
- Parameter fromViews: An Array of UIViews.
- Parameter toViews: An Array of UIViews.
*/
fileprivate
func
set
(
fromViews
:
[
UIView
],
toViews
:
[
UIView
])
{
self
.
fromViews
=
fromViews
self
.
toViews
=
toViews
prepare
(
views
:
fromViews
,
identifierIndex
:
&
transitionSourceIdentifierToView
)
prepare
(
views
:
toViews
,
identifierIndex
:
&
transitionDestinationIdentifierToView
)
}
}
Sources/MotionController.swift
View file @
d6904f6a
...
...
@@ -116,6 +116,33 @@ open class MotionController: NSObject, MotionSubscriber {
extension
MotionController
{
/**
Retrieves all the subviews of a given view.
- Parameter of view: A UIView.
- Returns: An Array of UIViews.
*/
fileprivate
func
subviews
(
of
view
:
UIView
)
->
[
UIView
]
{
var
views
:
[
UIView
]
=
[]
subviews
(
of
:
view
,
views
:
&
views
)
return
views
}
/**
Populates an Array of UIViews with the subviews of a given view.
- Parameter of view: A UIView.
- Returns: An Array of UIViews.
*/
fileprivate
func
subviews
(
of
view
:
UIView
,
views
:
inout
[
UIView
])
{
for
v
in
view
.
subviews
{
if
nil
!=
v
.
motionIdentifier
{
views
.
append
(
v
)
}
subviews
(
of
:
v
,
views
:
&
views
)
}
}
}
extension
MotionController
{
/**
Handles the animation update for the display link.
- Parameter displayLink: A CADisplayLink.animation
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment