-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtension.swift
More file actions
64 lines (52 loc) · 2.16 KB
/
Extension.swift
File metadata and controls
64 lines (52 loc) · 2.16 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
//
// Extension.swift
// SomeKindOfMonster
//
// Created by Baby on 3/5/18.
// Copyright © 2018 Baby. All rights reserved.
//
import UIKit
extension UIImageView {
func setRounded(_ isRounded: Bool) {
self.layer.cornerRadius = isRounded ? self.frame.height / 2.0 : 0.0
self.clipsToBounds = isRounded ? true : false
}
}
extension UIView {
func becomeInFocusWith(animationCoordinator: UIFocusAnimationCoordinator) {
// To match system timing and ensure smooth transition
animationCoordinator.addCoordinatedAnimations({
self.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
}, completion: nil)
}
func resignFocus(animationCoordinator: UIFocusAnimationCoordinator) {
animationCoordinator.addCoordinatedUnfocusingAnimations({ _ in
self.transform = .identity
}, completion: {
self.removeParallaxEffect()
})
}
func addParallaxMotionEffect(tilt: CGFloat, pan: CGFloat) {
func toRadians(_ degrees: Double) -> Double {
return (degrees * (.pi / 2)) / 180
}
let shiftX = UIInterpolatingMotionEffect(keyPath: "center.x", type: .tiltAlongHorizontalAxis)
shiftX.minimumRelativeValue = -pan
shiftX.maximumRelativeValue = pan
let shiftY = UIInterpolatingMotionEffect(keyPath: "center.y", type: .tiltAlongVerticalAxis)
shiftY.minimumRelativeValue = -pan
shiftY.maximumRelativeValue = pan
let tiltX = UIInterpolatingMotionEffect(keyPath: "layer.transform.rotation.y", type: .tiltAlongVerticalAxis)
tiltX.minimumRelativeValue = toRadians(Double(-tilt))
tiltX.maximumRelativeValue = toRadians(Double(tilt))
let tiltY = UIInterpolatingMotionEffect(keyPath: "layer.transform.rotation.x", type: .tiltAlongHorizontalAxis)
tiltY.minimumRelativeValue = toRadians(Double(-tilt))
tiltY.maximumRelativeValue = toRadians(Double(tilt))
let motionGroup = UIMotionEffectGroup()
motionGroup.motionEffects = [tiltX, tiltY, shiftX, shiftY]
addMotionEffect(motionGroup)
}
private func removeParallaxEffect() {
self.motionEffects = []
}
}