From a4165b0edd9c9c923a1d6e3e4c9a807302a1a475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20S=CC=81liwin=CC=81ski?= Date: Tue, 4 Aug 2020 08:43:39 +0200 Subject: [PATCH] Format the code and add documentation comments --- .../ParallaxableView+Extensions.swift | 112 +++++++++++------- .../Extensions/UIView+ParallaxEffect.swift | 11 +- Sources/Other/ParallaxEffectOptions.swift | 5 +- Sources/Other/ParallaxableView.swift | 6 +- ...FirstOffsetInterpolatingMotionEffect.swift | 5 +- Sources/Other/SubviewsParallaxMode.swift | 1 + .../Views/ParallaxCollectionViewCell.swift | 2 +- Sources/Views/ParallaxView.swift | 2 +- 8 files changed, 88 insertions(+), 56 deletions(-) diff --git a/Sources/Extensions/ParallaxableView+Extensions.swift b/Sources/Extensions/ParallaxableView+Extensions.swift index a8513f1..ef317ca 100644 --- a/Sources/Extensions/ParallaxableView+Extensions.swift +++ b/Sources/Extensions/ParallaxableView+Extensions.swift @@ -8,7 +8,8 @@ import UIKit -open class ParallaxViewActions where T:ParallaxableView { +/// Actions for a parallax view +open class ParallaxViewActions where T: ParallaxableView { /// Closure will be called in animation block by ParallaxableView when view should change its appearance to the focused state open var setupUnfocusedState: ((T) -> Void)? @@ -27,20 +28,27 @@ open class ParallaxViewActions where T:ParallaxableView { /// Default implementation of the press ended animation for the ParallaxableView open var animatePressOut: ((T, _ presses: Set, _ event: UIPressesEvent?) -> Void)? + /// Creates actions for parallax view with default behaviours public init() { becomeFocused = { [weak self] (view: T, context, coordinator) in self?.beforeBecomeFocusedAnimation?(view) if #available(tvOS 11.0, *) { - coordinator.addCoordinatedFocusingAnimations({ (context) in - self?.setupFocusedState?(view) - view.addParallaxMotionEffects(with: &view.parallaxEffectOptions) - }, completion: nil) + coordinator.addCoordinatedFocusingAnimations( + { (context) in + self?.setupFocusedState?(view) + view.addParallaxMotionEffects(with: &view.parallaxEffectOptions) + }, + completion: nil + ) } else { - coordinator.addCoordinatedAnimations({ - view.addParallaxMotionEffects(with: &view.parallaxEffectOptions) - self?.setupFocusedState?(view) - }, completion: nil) + coordinator.addCoordinatedAnimations( + { + view.addParallaxMotionEffects(with: &view.parallaxEffectOptions) + self?.setupFocusedState?(view) + }, + completion: nil + ) } } @@ -48,24 +56,33 @@ open class ParallaxViewActions where T:ParallaxableView { self?.beforeResignFocusAnimation?(view) if #available(tvOS 11.0, *) { - coordinator.addCoordinatedUnfocusingAnimations({ (context) in - view.removeParallaxMotionEffects(with: view.parallaxEffectOptions) - self?.setupUnfocusedState?(view) - }, completion: nil) + coordinator.addCoordinatedUnfocusingAnimations( + { (context) in + view.removeParallaxMotionEffects(with: view.parallaxEffectOptions) + self?.setupUnfocusedState?(view) + }, + completion: nil + ) } else { - coordinator.addCoordinatedAnimations({ - view.removeParallaxMotionEffects(with: view.parallaxEffectOptions) - self?.setupUnfocusedState?(view) - }, completion: nil) + coordinator.addCoordinatedAnimations( + { + view.removeParallaxMotionEffects(with: view.parallaxEffectOptions) + self?.setupUnfocusedState?(view) + }, + completion: nil + ) } } animatePressIn = { (view: T, presses, event) in for press in presses { if case .select = press.type { - UIView.animate(withDuration: 0.12, animations: { - view.transform = CGAffineTransform(scaleX: 0.95, y: 0.95) - }) + UIView.animate( + withDuration: 0.12, + animations: { + view.transform = CGAffineTransform(scaleX: 0.95, y: 0.95) + } + ) } } } @@ -73,15 +90,18 @@ open class ParallaxViewActions where T:ParallaxableView { animatePressOut = { [weak self] (view: T, presses, event) in for press in presses { if case .select = press.type { - UIView.animate(withDuration: 0.12, animations: { - if view.isFocused { - view.transform = CGAffineTransform.identity - self?.setupFocusedState?(view) - } else { - view.transform = CGAffineTransform.identity - self?.setupUnfocusedState?(view) + UIView.animate( + withDuration: 0.12, + animations: { + if view.isFocused { + view.transform = CGAffineTransform.identity + self?.setupFocusedState?(view) + } else { + view.transform = CGAffineTransform.identity + self?.setupUnfocusedState?(view) + } } - }) + ) } } } @@ -89,12 +109,12 @@ open class ParallaxViewActions where T:ParallaxableView { } -public extension ParallaxableView where Self: UIView { +extension ParallaxableView where Self: UIView { // MARK: Properties - /// Configure radius for parallaxView and glow effect if needed - var cornerRadius: CGFloat { + /// The corner radius for the parallax view and if applicable also applied to the glow effect + public var cornerRadius: CGFloat { get { return self.layer.cornerRadius } @@ -102,7 +122,9 @@ public extension ParallaxableView where Self: UIView { self.layer.cornerRadius = newValue // Change the glowEffectContainerView corner radius only if it is a direct subview of the parallax view - if let glowEffectContainerView = parallaxEffectOptions.glowContainerView , self.subviews.contains(glowEffectContainerView) { + if let glowEffectContainerView = parallaxEffectOptions.glowContainerView, + self.subviews.contains(glowEffectContainerView) + { glowEffectContainerView.layer.cornerRadius = newValue } } @@ -110,19 +132,19 @@ public extension ParallaxableView where Self: UIView { // MARK: ParallaxableView - /** - Get the glow image view that can be used to create the glow effect - - - returns: Image with radial gradient/shadow to imitate glow - */ - func getGlowImageView() -> UIImageView? { - return parallaxEffectOptions.glowContainerView?.subviews.filter({ (view) -> Bool in - if let glowImageView = view as? UIImageView, - let glowImage = glowImageView.image , glowImage.accessibilityIdentifier == glowImageAccessibilityIdentifier { - return true - } - return false - }).first as? UIImageView + /// Get the glow image view that can be used to create the glow effect + /// - Returns: Image with radial gradient/shadow to imitate glow + public func getGlowImageView() -> UIImageView? { + return + parallaxEffectOptions.glowContainerView?.subviews.filter({ (view) -> Bool in + if let glowImageView = view as? UIImageView, + let glowImage = glowImageView.image, + glowImage.accessibilityIdentifier == glowImageAccessibilityIdentifier + { + return true + } + return false + }).first as? UIImageView } } diff --git a/Sources/Extensions/UIView+ParallaxEffect.swift b/Sources/Extensions/UIView+ParallaxEffect.swift index 913a910..55ae250 100644 --- a/Sources/Extensions/UIView+ParallaxEffect.swift +++ b/Sources/Extensions/UIView+ParallaxEffect.swift @@ -8,6 +8,7 @@ import UIKit +/// A type of a view that can have parallax effect public protocol AnyParallaxableView { func addParallaxMotionEffects() func addParallaxMotionEffects(with options: inout ParallaxEffectOptions) @@ -15,12 +16,15 @@ public protocol AnyParallaxableView { } extension UIView: AnyParallaxableView { - + + /// Adds parallax motion effect to the view with default motion effect options public func addParallaxMotionEffects() { var options = ParallaxEffectOptions() addParallaxMotionEffects(with: &options) } + /// Adds parallax motion effect to the view with passed options + /// - Parameter options: Parallax motion effect options to customize the effect public func addParallaxMotionEffects(with options: inout ParallaxEffectOptions) { // If glow have to be visible and glowContainerView is not given then set it to self if options.glowContainerView == nil && options.glowAlpha > 0.0 { @@ -122,7 +126,10 @@ extension UIView: AnyParallaxableView { } } } - + + /// Removes parallax motion effect from the view + /// - Parameter options: If parallax motion effect was added with custom options, the same options should be + /// passed in this paramter to properly remove the effect public func removeParallaxMotionEffects(with options: ParallaxEffectOptions? = nil) { motionEffects.removeAll() (options?.parallaxSubviewsContainer ?? self).subviews diff --git a/Sources/Other/ParallaxEffectOptions.swift b/Sources/Other/ParallaxEffectOptions.swift index 378a975..b8affa4 100644 --- a/Sources/Other/ParallaxEffectOptions.swift +++ b/Sources/Other/ParallaxEffectOptions.swift @@ -8,10 +8,10 @@ import UIKit -/// A type that allows to customize parallax effet +/// A type that allows to customize parallax effect public struct ParallaxEffectOptions { - /// Property allow to customize parallax effect (pan, angles, etc.) + /// A property to allow customize parallax effect (pan, angles, etc.) /// /// - seealso: /// [ParallaxMotionEffect](ParallaxMotionEffect) @@ -74,6 +74,7 @@ extension ParallaxEffectOptions { glowImageView.center = CGPoint(x: glowEffectContainerView.frame.width/2, y: -glowImageView.frame.height/2) }) } + } internal let glowImageAccessibilityIdentifier = "com.pgs-soft.parallaxview.gloweffect" diff --git a/Sources/Other/ParallaxableView.swift b/Sources/Other/ParallaxableView.swift index defa236..c69002d 100644 --- a/Sources/Other/ParallaxableView.swift +++ b/Sources/Other/ParallaxableView.swift @@ -7,10 +7,10 @@ import UIKit +/// A type that can have parallax view representation public protocol ParallaxableView: class { - + /// Parallax effects options var parallaxEffectOptions: ParallaxEffectOptions { get set } - + /// The radius to use when var cornerRadius: CGFloat { get set } - } diff --git a/Sources/Other/SkipFirstOffsetInterpolatingMotionEffect.swift b/Sources/Other/SkipFirstOffsetInterpolatingMotionEffect.swift index 31831a1..3cd5ddf 100644 --- a/Sources/Other/SkipFirstOffsetInterpolatingMotionEffect.swift +++ b/Sources/Other/SkipFirstOffsetInterpolatingMotionEffect.swift @@ -7,7 +7,7 @@ import UIKit -final class SkipFirstOffsetInterpolatingMotionEffectDecorator: UIMotionEffect { +internal final class SkipFirstOffsetInterpolatingMotionEffectDecorator: UIMotionEffect { private let decoratee: UIMotionEffect private var motionDidNotStart: Bool = true @@ -27,11 +27,12 @@ final class SkipFirstOffsetInterpolatingMotionEffectDecorator: UIMotionEffect { if motionDidNotStart, viewerOffset.horizontal + viewerOffset.vertical != 0 { return nil } + return decoratee.keyPathsAndRelativeValues(forViewerOffset: viewerOffset) } } -extension UIMotionEffect { +internal extension UIMotionEffect { func decorateWithSkipFirstOffset() -> UIMotionEffect { return SkipFirstOffsetInterpolatingMotionEffectDecorator(decoratee: self) diff --git a/Sources/Other/SubviewsParallaxMode.swift b/Sources/Other/SubviewsParallaxMode.swift index 88e65de..77e9572 100644 --- a/Sources/Other/SubviewsParallaxMode.swift +++ b/Sources/Other/SubviewsParallaxMode.swift @@ -7,6 +7,7 @@ import Foundation +/// Modes of parallax motion effect that will be used for the subviews of the parallax view public enum SubviewsParallaxMode { /// maxParallaxOffset will be divided by the index of the subview inside `ParallaxView`. /// So view that is the last subview of the `ParallaxView` will be have the biggest offset equal to `maxParallaxOffset` diff --git a/Sources/Views/ParallaxCollectionViewCell.swift b/Sources/Views/ParallaxCollectionViewCell.swift index fc69a59..10134da 100644 --- a/Sources/Views/ParallaxCollectionViewCell.swift +++ b/Sources/Views/ParallaxCollectionViewCell.swift @@ -7,7 +7,7 @@ import UIKit -/// It provides default implementation of the parallax effect for the `UICollectionViewCell`. +/// An object that provides default implementation of the parallax effect for the `UICollectionViewCell`. /// Most of the time you will subclass this class as you would with `UICollectionViewCell` to provide custom content. /// If you will override `init` method it is important to provide default setup for the unfocused state of the view /// e.g. `parallaxViewActions.setupUnfocusedState?(self)` diff --git a/Sources/Views/ParallaxView.swift b/Sources/Views/ParallaxView.swift index 85e2361..6861443 100644 --- a/Sources/Views/ParallaxView.swift +++ b/Sources/Views/ParallaxView.swift @@ -7,7 +7,7 @@ import UIKit -/// It provides default implementation of the parallax effect for the `UIView`. +/// An object that provides default implementation of the parallax effect for the `UIView`. /// If you will override `init` method it is important to provide default setup for the unfocused state of the view /// e.g. `parallaxViewActions.setupUnfocusedState?(self)` open class ParallaxView: UIView, ParallaxableView {