Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit acaa9b6

Browse files
author
Łukasz Śliwiński
committed
Adds ability to setup the glow effect position in the view.
Improve animation of the glow fade in/out in case of focused/unfocused state.
1 parent ca1c5b6 commit acaa9b6

File tree

5 files changed

+61
-37
lines changed

5 files changed

+61
-37
lines changed

Example-tvOS/ParallaxViewExample/CollectionViewCell.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class CollectionViewCell: ParallaxCollectionViewCell {
2020
parallaxEffectOptions.parallaxMotionEffect.viewingAngleX = CGFloat(Double.pi/4/30)
2121
parallaxEffectOptions.parallaxMotionEffect.viewingAngleY = CGFloat(Double.pi/4/30)
2222
parallaxEffectOptions.parallaxMotionEffect.panValue = CGFloat(10)
23+
parallaxEffectOptions.glowPosition = .center
2324

2425
// You can customise parallax view standard behaviours using parallaxViewActions property.
2526
// Do not forget to use weak self if needed to void retain cycle

Sources/Extensions/UIView+ParallaxEffect.swift

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@ extension UIView: AnyParallaxableView {
3030
if let glowContainerView = options.glowContainerView {
3131
// Need to clip to bounds because of the glow effect
3232
glowContainerView.clipsToBounds = true
33-
3433
// Configure glow image
3534
let glowImageView = options.glowImageView
35+
UIView.performWithoutAnimation {
36+
glowImageView.alpha = 0
37+
}
3638
glowImageView.alpha = CGFloat(options.glowAlpha)
3739
glowContainerView.addSubview(glowImageView)
3840

3941
// Configure frame of the glow effect without animation
4042
UIView.performWithoutAnimation {
41-
let maxSize = max(glowContainerView.frame.width, glowContainerView.frame.height)*1.7
42-
// Make glow a litte bit bigger than the superview
43-
glowImageView.frame = CGRect(x: 0, y: 0, width: maxSize, height: maxSize)
44-
// Position in the middle and under the top edge of the superview
45-
glowImageView.center = CGPoint(x: glowContainerView.frame.width/2, y: -glowImageView.frame.height)
43+
options.glowPosition.layout(glowContainerView, glowImageView)
4644
}
4745

4846
// Configure pan motion effect for the glow
@@ -59,8 +57,10 @@ extension UIView: AnyParallaxableView {
5957
horizontalGlowEffect.decorateWithSkipFirstOffset(),
6058
verticalGlowEffect.decorateWithSkipFirstOffset()
6159
]
62-
63-
glowImageView.addMotionEffect(glowMotionGroup)
60+
61+
UIView.performWithoutAnimation {
62+
glowImageView.addMotionEffect(glowMotionGroup.decorateWithSkipFirstOffset())
63+
}
6464
}
6565

6666
let motionGroup = UIMotionEffectGroup()
@@ -132,7 +132,17 @@ extension UIView: AnyParallaxableView {
132132
}
133133

134134
options?.glowImageView.motionEffects.removeAll()
135-
options?.glowImageView.removeFromSuperview()
135+
options?.glowImageView.alpha = 0
136+
137+
UIView.animate(
138+
withDuration: UIView.inheritedAnimationDuration,
139+
animations: {
140+
options?.glowImageView.transform = .init(scaleX: 1.01, y: 1.01)
141+
}, completion: { _ in
142+
options?.glowImageView.transform = .identity
143+
guard !self.isFocused else { return }
144+
options?.glowImageView.removeFromSuperview()
145+
})
136146
}
137147

138148
}

Sources/Other/ParallaxEffectOptions.swift

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ public struct ParallaxEffectOptions {
2222
/// Custom container view that will be usead to apply subviews parallax effect
2323
public var parallaxSubviewsContainer: UIView?
2424
/// A view that will be a container for the glow effect
25-
public weak var glowContainerView: UIView?
25+
public weak var glowContainerView: UIView? {
26+
didSet { oldValue?.removeFromSuperview() }
27+
}
2628
/// Minimum vertical value at the most top position can be adjusted by this multipler
2729
public var minVerticalPanGlowMultipler: Double = 0.2
2830
/// Maximum vertical value at the most bottom position can be adjusted by this multipler
2931
public var maxVerticalPanGlowMultipler: Double = 1.55
3032
/// Alpha of the glow image view
31-
public var glowAlpha: Double = 1.0
33+
public var glowAlpha: Double = 1.0 {
34+
didSet { self.glowImageView.alpha = CGFloat(glowAlpha) }
35+
}
3236
/// Glow effect image view
3337
public var glowImageView: UIImageView = ParallaxEffectOptions.defaultGlowImageView()
38+
/// Glow position
39+
public var glowPosition: GlowPosition = .top
3440

3541
/// Constructor
3642
///
@@ -44,6 +50,31 @@ public struct ParallaxEffectOptions {
4450

4551
}
4652

53+
extension ParallaxEffectOptions {
54+
55+
public struct GlowPosition {
56+
let layout: (UIView, UIImageView) -> Void
57+
58+
public static let top: GlowPosition = .init(layout: { (glowEffectContainerView, glowImageView) in
59+
if let glowSuperView = glowEffectContainerView.superview {
60+
glowEffectContainerView.frame = glowSuperView.bounds
61+
}
62+
63+
// Make glow a litte bit bigger than the superview
64+
let maxSize = max(glowEffectContainerView.frame.width, glowEffectContainerView.frame.height)*1.7
65+
glowImageView.frame = CGRect(x: 0, y: 0, width: maxSize, height: maxSize)
66+
67+
// Position in the middle and under the top edge of the superview
68+
glowImageView.center = CGPoint(x: glowEffectContainerView.frame.width/2, y: -glowImageView.frame.height)
69+
})
70+
71+
public static let center: GlowPosition = .init(layout: { (glowEffectContainerView, glowImageView) in
72+
GlowPosition.top.layout(glowEffectContainerView, glowImageView)
73+
glowImageView.center = CGPoint(x: glowEffectContainerView.frame.width/2, y: -glowImageView.frame.height/2)
74+
})
75+
}
76+
}
77+
4778
internal let glowImageAccessibilityIdentifier = "com.pgs-soft.parallaxview.gloweffect"
4879

4980
extension ParallaxEffectOptions {

Sources/Views/ParallaxCollectionViewCell.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,11 @@ open class ParallaxCollectionViewCell: UICollectionViewCell, ParallaxableView {
7979
open override func layoutSubviews() {
8080
super.layoutSubviews()
8181

82-
guard let glowEffectContainerView = parallaxEffectOptions.glowContainerView else { return }
82+
guard let glowEffectContainerView = parallaxEffectOptions.glowContainerView,
83+
let glowImageView = getGlowImageView()
84+
else { return }
8385

84-
if glowEffectContainerView != self, let glowSuperView = glowEffectContainerView.superview {
85-
glowEffectContainerView.frame = glowSuperView.bounds
86-
}
87-
88-
let maxSize = max(glowEffectContainerView.frame.width, glowEffectContainerView.frame.height)*1.7
89-
// Make glow a litte bit bigger than the superview
90-
91-
guard let glowImageView = getGlowImageView() else { return }
92-
93-
glowImageView.frame = CGRect(x: 0, y: 0, width: maxSize, height: maxSize)
94-
// Position in the middle and under the top edge of the superview
95-
glowImageView.center = CGPoint(x: glowEffectContainerView.frame.width/2, y: -glowImageView.frame.height)
86+
parallaxEffectOptions.glowPosition.layout(glowEffectContainerView, glowImageView)
9687
}
9788

9889
// MARK: UIResponder

Sources/Views/ParallaxView.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,11 @@ open class ParallaxView: UIView, ParallaxableView {
5656
open override func layoutSubviews() {
5757
super.layoutSubviews()
5858

59-
guard let glowEffectContainerView = parallaxEffectOptions.glowContainerView else { return }
59+
guard let glowEffectContainerView = parallaxEffectOptions.glowContainerView,
60+
let glowImageView = getGlowImageView()
61+
else { return }
6062

61-
if glowEffectContainerView != self, let glowSuperView = glowEffectContainerView.superview {
62-
glowEffectContainerView.frame = glowSuperView.bounds
63-
}
64-
65-
let maxSize = max(glowEffectContainerView.frame.width, glowEffectContainerView.frame.height)*1.7
66-
// Make glow a litte bit bigger than the superview
67-
68-
guard let glowImageView = getGlowImageView() else { return }
69-
70-
glowImageView.frame = CGRect(x: 0, y: 0, width: maxSize, height: maxSize)
71-
// Position in the middle and under the top edge of the superview
72-
glowImageView.center = CGPoint(x: glowEffectContainerView.frame.width/2, y: -glowImageView.frame.height)
63+
parallaxEffectOptions.glowPosition.layout(glowEffectContainerView, glowImageView)
7364
}
7465

7566
// MARK: UIResponder

0 commit comments

Comments
 (0)