Add animation cases + spring/damping physics

This commit is contained in:
CypherPoet 2019-01-29 17:22:02 -05:00
parent 2c8da9143a
commit 8e104c3a9f
2 changed files with 37 additions and 16 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina5_9" orientation="portrait">
<device id="retina6_1" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
@ -15,11 +15,11 @@
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="animation" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="812"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="LJW-yS-JE2">
<rect key="frame" x="164.66666666666666" y="661" width="46" height="48"/>
<rect key="frame" x="184" y="745" width="46" height="48"/>
<constraints>
<constraint firstAttribute="height" constant="48" id="dIN-1h-hw3"/>
</constraints>
@ -31,13 +31,13 @@
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="LJW-yS-JE2" secondAttribute="bottom" constant="69" id="IcL-IN-Vkd"/>
<constraint firstItem="LJW-yS-JE2" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="OSN-7C-RZ3"/>
<constraint firstItem="LJW-yS-JE2" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="IvM-e8-hp3"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="LJW-yS-JE2" secondAttribute="bottom" constant="69" id="uPk-QE-Nsu"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<connections>
<outlet property="triggerButton" destination="LJW-yS-JE2" id="Egn-6r-1kL"/>
<outlet property="triggerButton" destination="LJW-yS-JE2" id="97E-Es-jV8"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>

View File

@ -12,9 +12,9 @@ let sceneWidth = 1024
let sceneHeight = 768
class ViewController: UIViewController {
@IBOutlet weak var triggerButton: UIButton!
@IBOutlet var triggerButton: UIButton!
var currentAnimation = 0
var currentAnimationIndex = 0
var animationDuration = 1.0
var animationDelay = 0.0
var imageView: UIImageView!
@ -25,34 +25,55 @@ class ViewController: UIViewController {
imageView = UIImageView(image: UIImage(named: "penguin"))
imageView.center = view.center
imageView.layer.zPosition = 0
view.addSubview(imageView)
triggerButton.layer.zPosition = 1
}
@IBAction func triggerAnimation(_ sender: Any) {
triggerButton.isHidden = true
triggerButton.alpha = 0.0
UIView.animate(
withDuration: animationDuration,
delay: animationDelay,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 5,
options: [],
animations: { [unowned self] in
self.runCurrentAnimation()
},
completion: { [unowned self] (finished: Bool) in
self.triggerButton.alpha = 1.0
}
) { [unowned self] (finished: Bool) in
self.triggerButton.isHidden = false
}
)
self.currentAnimation += 1 % 7
currentAnimationIndex = (currentAnimationIndex + 1) % 8
}
func runCurrentAnimation() {
switch self.currentAnimation {
switch currentAnimationIndex {
case 0:
break
self.imageView.transform = CGAffineTransform(scaleX: 2, y: 2)
case 1:
self.imageView.transform = CGAffineTransform.identity
case 2:
self.imageView.transform = CGAffineTransform(translationX: -(view.center.x / 2), y: -(view.center.y / 2))
case 3:
self.imageView.transform = CGAffineTransform.identity
case 4:
self.imageView.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
case 5:
self.imageView.transform = CGAffineTransform.identity
case 6:
self.imageView.alpha = 0.1
self.imageView.backgroundColor = UIColor.purple
case 7:
self.imageView.alpha = 1.0
self.imageView.backgroundColor = UIColor.clear
default:
break
}