Skip to content

Commit

Permalink
stop animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Koji Murata committed Sep 21, 2017
1 parent d539b7d commit 1585dae
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Example/JellyAnimationView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
ED727A451F739DC70068A182 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -76,6 +78,7 @@
isa = PBXGroup;
children = (
607FACD51AFB9204008FA782 /* AppDelegate.swift */,
ED727A451F739DC70068A182 /* ViewController.swift */,
607FACD91AFB9204008FA782 /* Main.storyboard */,
607FACDC1AFB9204008FA782 /* Images.xcassets */,
607FACDE1AFB9204008FA782 /* LaunchScreen.xib */,
Expand Down Expand Up @@ -243,6 +246,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
ED727A461F739DC70068A182 /* ViewController.swift in Sources */,
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
32 changes: 31 additions & 1 deletion Example/JellyAnimationView/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<!--View Controller-->
<scene sceneID="ufC-wZ-h7g">
<objects>
<viewController id="vXZ-lx-hvc" sceneMemberID="viewController">
<viewController id="vXZ-lx-hvc" customClass="ViewController" customModule="JellyAnimationView_Example" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
Expand All @@ -32,15 +32,45 @@
<userDefinedRuntimeAttribute type="number" keyPath="jumpHeight">
<real key="value" value="100"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="startWhenAwakeFromNib" value="YES"/>
</userDefinedRuntimeAttributes>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="2vj-Mv-yFz">
<rect key="frame" x="163.5" y="537" width="47" height="30"/>
<state key="normal" title="Toggle"/>
<connections>
<action selector="animationToggle:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="tVa-fy-r9f"/>
</connections>
</button>
<stackView opaque="NO" contentMode="scaleToFill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="non-oC-XbD">
<rect key="frame" x="93.5" y="498" width="188" height="31"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="stop immediately" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="8yK-Y8-Uym">
<rect key="frame" x="0.0" y="0.0" width="131" height="31"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="htd-Zq-UhS">
<rect key="frame" x="139" y="0.0" width="51" height="31"/>
</switch>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="2vj-Mv-yFz" secondAttribute="bottom" constant="100" id="6gl-oB-VJB"/>
<constraint firstItem="non-oC-XbD" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="7kX-kp-dNt"/>
<constraint firstItem="2vj-Mv-yFz" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="FmF-rJ-hDV"/>
<constraint firstItem="BA1-SM-q4u" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" id="R4S-4V-cfl"/>
<constraint firstItem="2vj-Mv-yFz" firstAttribute="top" secondItem="non-oC-XbD" secondAttribute="bottom" constant="8" id="cmL-Of-Fxy"/>
<constraint firstItem="BA1-SM-q4u" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="ySY-dG-uWj"/>
</constraints>
</view>
<connections>
<outlet property="animationView" destination="BA1-SM-q4u" id="Dup-Py-Knp"/>
<outlet property="immediatelySwitch" destination="htd-Zq-UhS" id="zms-kp-VdF"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
Expand Down
23 changes: 23 additions & 0 deletions Example/JellyAnimationView/ViewController.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
27 changes: 25 additions & 2 deletions JellyAnimationView/Classes/JellyAnimationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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() {
Expand Down

0 comments on commit 1585dae

Please sign in to comment.