Skip to content

Commit

Permalink
fix: bug #149 animation stopped when modal is presented
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanpe committed Aug 26, 2019
1 parent 15764de commit 2f2e542
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Example/TableView/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<userDefinedRuntimeAttribute type="boolean" keyPath="isSkeletonable" value="YES"/>
</userDefinedRuntimeAttributes>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="15" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VhU-1t-AaI">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VhU-1t-AaI">
<rect key="frame" x="118" y="29" width="237" height="20.5"/>
<constraints>
<constraint firstAttribute="height" relation="lessThanOrEqual" constant="71" id="HRL-cI-ieC"/>
Expand Down
4 changes: 0 additions & 4 deletions Example/TableView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ class ViewController: UIViewController {
super.viewDidLoad()
tableview.isSkeletonable = true
}

override func viewDidLayoutSubviews() {
view.layoutSkeletonIfNeeded()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
Expand Down
10 changes: 8 additions & 2 deletions SkeletonView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
F54CF5E52024CEB000330B0D /* UITableView+CollectionSkeleton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F54CF5E22024CEAF00330B0D /* UITableView+CollectionSkeleton.swift */; };
F54CF5E62024CEB000330B0D /* UICollectionView+CollectionSkeleton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F54CF5E32024CEB000330B0D /* UICollectionView+CollectionSkeleton.swift */; };
F56B94461FAE20AF0095662F /* PrepareForSkeletonProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F56B94451FAE20AF0095662F /* PrepareForSkeletonProtocol.swift */; };
F570ABF42314629700390248 /* Swizzling.swift in Sources */ = {isa = PBXBuildFile; fileRef = F570ABF32314629700390248 /* Swizzling.swift */; };
F570ABF52314629700390248 /* Swizzling.swift in Sources */ = {isa = PBXBuildFile; fileRef = F570ABF32314629700390248 /* Swizzling.swift */; };
F5804772230ECD0000066D02 /* UIView+IBInspectable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5804771230ECD0000066D02 /* UIView+IBInspectable.swift */; };
F5804773230ECD0000066D02 /* UIView+IBInspectable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5804771230ECD0000066D02 /* UIView+IBInspectable.swift */; };
F587FB84202CBFC8002DB5FE /* SkeletonFlow.swift in Sources */ = {isa = PBXBuildFile; fileRef = F587FB83202CBFC8002DB5FE /* SkeletonFlow.swift */; };
Expand Down Expand Up @@ -185,6 +187,7 @@
F54CF5E22024CEAF00330B0D /* UITableView+CollectionSkeleton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UITableView+CollectionSkeleton.swift"; sourceTree = "<group>"; };
F54CF5E32024CEB000330B0D /* UICollectionView+CollectionSkeleton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UICollectionView+CollectionSkeleton.swift"; sourceTree = "<group>"; };
F56B94451FAE20AF0095662F /* PrepareForSkeletonProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrepareForSkeletonProtocol.swift; sourceTree = "<group>"; };
F570ABF32314629700390248 /* Swizzling.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Swizzling.swift; sourceTree = "<group>"; };
F5804771230ECD0000066D02 /* UIView+IBInspectable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+IBInspectable.swift"; sourceTree = "<group>"; };
F587FB83202CBFC8002DB5FE /* SkeletonFlow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkeletonFlow.swift; sourceTree = "<group>"; };
F587FB85202CEC95002DB5FE /* UIView+UIApplicationDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+UIApplicationDelegate.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -437,6 +440,7 @@
F5F899D11FAB9630002E8FDA /* AssociationPolicy.swift */,
F56B94451FAE20AF0095662F /* PrepareForSkeletonProtocol.swift */,
F5307E311FB0F42F00EE67C5 /* RecursiveProtocol.swift */,
F570ABF32314629700390248 /* Swizzling.swift */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -684,6 +688,7 @@
17DD0E0C207FB28900C56334 /* UICollectionView+CollectionSkeleton.swift in Sources */,
F5804773230ECD0000066D02 /* UIView+IBInspectable.swift in Sources */,
17DD0E13207FB28C00C56334 /* UIView+UIApplicationDelegate.swift in Sources */,
F570ABF52314629700390248 /* Swizzling.swift in Sources */,
F5D3FB0C209DCAA300003FCF /* SubviewsSkeletonables.swift in Sources */,
17DD0E14207FB28F00C56334 /* SkeletonAnimationBuilder.swift in Sources */,
17DD0E11207FB28C00C56334 /* UIView+Frame.swift in Sources */,
Expand Down Expand Up @@ -740,6 +745,7 @@
F5F622411FAC6E31007C062A /* UIColor+Skeleton.swift in Sources */,
F5804772230ECD0000066D02 /* UIView+IBInspectable.swift in Sources */,
F587FB84202CBFC8002DB5FE /* SkeletonFlow.swift in Sources */,
F570ABF42314629700390248 /* Swizzling.swift in Sources */,
F5D3FB0B209DCAA300003FCF /* SubviewsSkeletonables.swift in Sources */,
F5F899D21FAB9630002E8FDA /* AssociationPolicy.swift in Sources */,
F54CF5E62024CEB000330B0D /* UICollectionView+CollectionSkeleton.swift in Sources */,
Expand Down Expand Up @@ -1038,7 +1044,7 @@
52D6D9911BEFF229002C0205 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
APPLICATION_EXTENSION_API_ONLY = NO;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
Expand All @@ -1063,7 +1069,7 @@
52D6D9921BEFF229002C0205 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
APPLICATION_EXTENSION_API_ONLY = NO;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
Expand Down
16 changes: 9 additions & 7 deletions Sources/Extensions/CALayer+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,18 @@ public extension CALayer {
animGroup.duration = 1.5
animGroup.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeIn)
animGroup.repeatCount = .infinity

return animGroup
}

func playAnimation(_ anim: SkeletonLayerAnimation, key: String) {
func playAnimation(_ anim: SkeletonLayerAnimation, key: String, completion: (() -> Void)? = nil) {
skeletonSublayers.recursiveSearch(leafBlock: {
DispatchQueue.main.async { CATransaction.begin() }
DispatchQueue.main.async { CATransaction.setCompletionBlock(completion) }
add(anim(self), forKey: key)
DispatchQueue.main.async { CATransaction.commit() }
}) {
$0.playAnimation(anim, key: key)
$0.playAnimation(anim, key: key, completion: completion)
}
}

Expand All @@ -140,15 +143,14 @@ public extension CALayer {

extension CALayer {
func setOpacity(from: Int, to: Int, duration: TimeInterval, completion: (() -> Void)?) {
CATransaction.begin()
DispatchQueue.main.async { CATransaction.begin() }
let animation = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
animation.fromValue = from
animation.toValue = to
animation.duration = duration
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
// layer.contentLayer.opacity = 1
CATransaction.setCompletionBlock(completion)
DispatchQueue.main.async { CATransaction.setCompletionBlock(completion) }
add(animation, forKey: "setOpacityAnimation")
CATransaction.commit()
DispatchQueue.main.async { CATransaction.commit() }
}
}
2 changes: 1 addition & 1 deletion Sources/Extensions/UIView+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension UIView {
set { ao_set(newValue ?? .off, pkey: &ViewAssociatedKeys.status) }
}

var skeletonIsAnimated: Bool! {
var isSkeletonAnimated: Bool! {
get { return ao_get(pkey: &ViewAssociatedKeys.isSkeletonAnimated) as? Bool ?? false }
set { ao_set(newValue ?? false, pkey: &ViewAssociatedKeys.isSkeletonAnimated) }
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Extensions/UIView+IBInspectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public extension UIView {
return status == .on || (subviewsSkeletonables.first(where: { $0.isSkeletonActive }) != nil)
}

fileprivate var skeletonable: Bool! {
private var skeletonable: Bool! {
get { return ao_get(pkey: &ViewAssociatedKeys.skeletonable) as? Bool ?? false }
set { ao_set(newValue ?? false, pkey: &ViewAssociatedKeys.skeletonable) }
}
Expand Down
27 changes: 27 additions & 0 deletions Sources/Helpers/Swizzling.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright © 2019 SkeletonView. All rights reserved.

import Foundation

extension DispatchQueue {
private static var _onceTracker = [String]()

class func once(token: String, block:()->Void) {
objc_sync_enter(self); defer { objc_sync_exit(self) }
guard !_onceTracker.contains(token) else { return }

_onceTracker.append(token)
block()
}
}

func swizzle(selector originalSelector: Selector, with swizzledSelector: Selector, inClass: AnyClass, usingClass: AnyClass) {
guard let originalMethod = class_getInstanceMethod(inClass, originalSelector),
let swizzledMethod = class_getInstanceMethod(usingClass, swizzledSelector)
else { return }

if (class_addMethod(inClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))) {
class_replaceMethod(inClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
1 change: 0 additions & 1 deletion Sources/SkeletonFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ protocol SkeletonFlowDelegate {
}

class SkeletonFlowHandler: SkeletonFlowDelegate {

func willBeginShowingSkeletons(rootView: UIView) {
rootView.addAppNotificationsObservers()
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SkeletonLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ struct SkeletonLayer {
}

extension SkeletonLayer {
func start(_ anim: SkeletonLayerAnimation? = nil) {
func start(_ anim: SkeletonLayerAnimation? = nil, completion: (() -> Void)? = nil) {
let animation = anim ?? type.layerAnimation
contentLayer.playAnimation(animation, key: "skeletonAnimation")
contentLayer.playAnimation(animation, key: "skeletonAnimation", completion: completion)
}

func stopAnimation() {
Expand Down
Loading

0 comments on commit 2f2e542

Please sign in to comment.