diff --git a/Example/JellyAnimationView.xcodeproj/project.pbxproj b/Example/JellyAnimationView.xcodeproj/project.pbxproj index c9a3217..aa134b2 100644 --- a/Example/JellyAnimationView.xcodeproj/project.pbxproj +++ b/Example/JellyAnimationView.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDC1AFB9204008FA782 /* Images.xcassets */; }; 607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; }; 9C44F0C47E14B3ABF68D7D12 /* Pods_JellyAnimationView_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B392A0E110C38F48FFA4F7E6 /* Pods_JellyAnimationView_Example.framework */; }; + ED727A461F739DC70068A182 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED727A451F739DC70068A182 /* ViewController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -30,6 +31,7 @@ C041CE0516521808A0456310 /* Pods-JellyAnimationView_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JellyAnimationView_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-JellyAnimationView_Example/Pods-JellyAnimationView_Example.debug.xcconfig"; sourceTree = ""; }; D1D710976E9922E774CE559A /* Pods-JellyAnimationView_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JellyAnimationView_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-JellyAnimationView_Tests/Pods-JellyAnimationView_Tests.release.xcconfig"; sourceTree = ""; }; EB1CD7121E3AC59761741B1A /* Pods-JellyAnimationView_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JellyAnimationView_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-JellyAnimationView_Tests/Pods-JellyAnimationView_Tests.debug.xcconfig"; sourceTree = ""; }; + ED727A451F739DC70068A182 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -76,6 +78,7 @@ isa = PBXGroup; children = ( 607FACD51AFB9204008FA782 /* AppDelegate.swift */, + ED727A451F739DC70068A182 /* ViewController.swift */, 607FACD91AFB9204008FA782 /* Main.storyboard */, 607FACDC1AFB9204008FA782 /* Images.xcassets */, 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */, @@ -243,6 +246,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + ED727A461F739DC70068A182 /* ViewController.swift in Sources */, 607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Example/JellyAnimationView/Base.lproj/Main.storyboard b/Example/JellyAnimationView/Base.lproj/Main.storyboard index 747efb3..3c9ca24 100644 --- a/Example/JellyAnimationView/Base.lproj/Main.storyboard +++ b/Example/JellyAnimationView/Base.lproj/Main.storyboard @@ -12,7 +12,7 @@ - + @@ -32,15 +32,45 @@ + + + + + + + + + + + + + + + + + + + diff --git a/Example/JellyAnimationView/ViewController.swift b/Example/JellyAnimationView/ViewController.swift new file mode 100644 index 0000000..5025ebb --- /dev/null +++ b/Example/JellyAnimationView/ViewController.swift @@ -0,0 +1,23 @@ +// +// ViewController.swift +// JellyAnimationView_Example +// +// Created by Koji Murata on 2017/09/21. +// Copyright © 2017年 CocoaPods. All rights reserved. +// + +import UIKit +import JellyAnimationView + +final class ViewController: UIViewController { + @IBOutlet private weak var animationView: JellyAnimationView! + @IBOutlet private weak var immediatelySwitch: UISwitch! + + @IBAction private func animationToggle(_ sender: UIButton) { + if animationView.isAnimating { + animationView.stopAnimation(immediately: immediatelySwitch.isOn) + } else { + animationView.startAnimation() + } + } +} diff --git a/JellyAnimationView/Classes/JellyAnimationView.swift b/JellyAnimationView/Classes/JellyAnimationView.swift index 93c3845..0949867 100644 --- a/JellyAnimationView/Classes/JellyAnimationView.swift +++ b/JellyAnimationView/Classes/JellyAnimationView.swift @@ -9,14 +9,28 @@ import UIKit open class JellyAnimationView: UIView, CAAnimationDelegate { open func startAnimation() { + isAnimating = true + immediately = false startVerticalAnimation() } + open func stopAnimation(immediately: Bool = false) { + isAnimating = false + self.immediately = immediately + if immediately { + layer.removeAllAnimations() + } + } + @IBInspectable open var startWhenAwakeFromNib: Bool = true @IBInspectable open var duration: Double = 1 @IBInspectable open var jumpHeight: CGFloat = 20 @IBInspectable open var initialVelocity: CGFloat = 1 + open private(set) var isAnimating = false + + private var immediately = false + private func startJellyWidthAnimation() { let animation = CASpringAnimation(keyPath: "transform.scale.x") animation.duration = 1 @@ -26,6 +40,7 @@ open class JellyAnimationView: UIView, CAAnimationDelegate { animation.initialVelocity = jumpHeight * 100 * initialVelocity animation.damping = 14 animation.stiffness = 300 + animation.isRemovedOnCompletion = true layer.add(animation, forKey: nil) } @@ -38,6 +53,7 @@ open class JellyAnimationView: UIView, CAAnimationDelegate { animation.initialVelocity = jumpHeight * 100 * initialVelocity animation.damping = 10 animation.stiffness = 300 + animation.isRemovedOnCompletion = true layer.add(animation, forKey: nil) } @@ -60,13 +76,20 @@ open class JellyAnimationView: UIView, CAAnimationDelegate { verticalAnimation.keyTimes = keyTimes verticalAnimation.values = values verticalAnimation.delegate = self + verticalAnimation.isRemovedOnCompletion = true layer.add(verticalAnimation, forKey: nil) } open func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { - startJellyAnimation() - startVerticalAnimation() + if isAnimating { + startJellyAnimation() + startVerticalAnimation() + } else { + if !immediately { + startJellyAnimation() + } + } } override open func awakeFromNib() {