Skip to content

Commit

Permalink
added a switch to enable/disable animations
Browse files Browse the repository at this point in the history
  • Loading branch information
icanzilb committed Sep 22, 2017
1 parent a0831fe commit ac3e33b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 18 deletions.
58 changes: 41 additions & 17 deletions Example/RxAnimated/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@
<nil key="highlightedColor"/>
</label>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="42a-os-wxE">
<rect key="frame" x="87" y="142" width="200" height="128"/>
<rect key="frame" x="26" y="142" width="100" height="100"/>
<constraints>
<constraint firstAttribute="height" constant="128" id="YFq-2U-Gqm"/>
<constraint firstAttribute="height" constant="100" id="YFq-2U-Gqm"/>
<constraint firstAttribute="width" constant="100" id="ZMI-6p-EI9"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Image + custom" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="q5e-On-hCR">
<rect key="frame" x="87" y="249" width="200" height="21"/>
<rect key="frame" x="26" y="221" width="100" height="21"/>
<constraints>
<constraint firstAttribute="height" constant="21" id="WG8-t7-1CP"/>
</constraints>
Expand All @@ -76,8 +77,17 @@
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="ANIMATING AUTO LAYOUT" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="8Qo-1B-TWh">
<rect key="frame" x="16" y="252" width="350" height="34"/>
<constraints>
<constraint firstAttribute="height" constant="34" id="3cm-3o-tLh"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="ANIMATING PROPERTIES" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="6LA-eY-irz">
<rect key="frame" x="16" y="382" width="343" height="34"/>
<rect key="frame" x="16" y="354" width="343" height="34"/>
<constraints>
<constraint firstAttribute="height" constant="34" id="tfU-8m-zHU"/>
</constraints>
Expand All @@ -86,14 +96,14 @@
<nil key="highlightedColor"/>
</label>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="adorable2" translatesAutoresizingMaskIntoConstraints="NO" id="5Sz-yW-4xM">
<rect key="frame" x="204.5" y="424" width="100" height="100"/>
<rect key="frame" x="204.5" y="396" width="100" height="100"/>
<constraints>
<constraint firstAttribute="height" constant="100" id="HXK-ce-gil"/>
<constraint firstAttribute="width" constant="100" id="c3K-af-l4O"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="isHidden" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="waM-h0-fUY">
<rect key="frame" x="71" y="532" width="100" height="21"/>
<rect key="frame" x="71" y="504" width="100" height="21"/>
<constraints>
<constraint firstAttribute="width" constant="100" id="FAV-wI-lJI"/>
<constraint firstAttribute="height" constant="21" id="pqE-uo-YK7"/>
Expand All @@ -103,7 +113,7 @@
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="alpha" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qYx-jF-3MP">
<rect key="frame" x="205" y="532" width="100" height="21"/>
<rect key="frame" x="205" y="504" width="100" height="21"/>
<constraints>
<constraint firstAttribute="height" constant="21" id="Hyg-j4-DXY"/>
<constraint firstAttribute="width" constant="100" id="xNR-0u-2yV"/>
Expand All @@ -113,21 +123,21 @@
<nil key="highlightedColor"/>
</label>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="adorable1" translatesAutoresizingMaskIntoConstraints="NO" id="RPV-i3-c0r">
<rect key="frame" x="70.5" y="424" width="100" height="100"/>
<rect key="frame" x="70.5" y="396" width="100" height="100"/>
<constraints>
<constraint firstAttribute="height" constant="100" id="Dtf-Qo-H4U"/>
<constraint firstAttribute="width" constant="100" id="ipq-SO-Vk3"/>
</constraints>
</imageView>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="adorable1" translatesAutoresizingMaskIntoConstraints="NO" id="QOH-EK-CIF">
<rect key="frame" x="23" y="324" width="48" height="48"/>
<rect key="frame" x="23" y="296" width="48" height="48"/>
<constraints>
<constraint firstAttribute="width" constant="48" id="EKI-fx-Dsz"/>
<constraint firstAttribute="height" constant="48" id="SnI-cR-5HK"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="AL constraint" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="QFG-ih-W6q">
<rect key="frame" x="211" y="337.5" width="118" height="21"/>
<rect key="frame" x="211" y="309.5" width="118" height="21"/>
<constraints>
<constraint firstAttribute="height" constant="21" id="HhZ-MN-kww"/>
<constraint firstAttribute="width" constant="118" id="ouF-Kq-QoQ"/>
Expand All @@ -136,13 +146,22 @@
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="ANIMATING AUTO LAYOUT" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="8Qo-1B-TWh">
<rect key="frame" x="16" y="280" width="350" height="34"/>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="Kc2-hx-U2o">
<rect key="frame" x="242" y="142" width="100" height="100"/>
<constraints>
<constraint firstAttribute="height" constant="34" id="3cm-3o-tLh"/>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="100" id="16B-Tf-L01"/>
<constraint firstAttribute="height" constant="100" id="9Cm-Ov-VwS"/>
<constraint firstAttribute="width" constant="100" id="P6q-Bo-2Fy"/>
<constraint firstAttribute="height" relation="greaterThanOrEqual" constant="100" id="PVU-Qn-AkR"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Image + block" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="OBM-E6-CLw">
<rect key="frame" x="242" y="217" width="100" height="21"/>
<constraints>
<constraint firstAttribute="height" constant="21" id="5PW-aE-GMK"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
Expand All @@ -167,26 +186,31 @@
<constraint firstAttribute="trailing" secondItem="QFG-ih-W6q" secondAttribute="trailing" priority="500" constant="16" id="K0e-3P-do6"/>
<constraint firstItem="lSa-S8-ghM" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" id="QJI-xr-hTf"/>
<constraint firstItem="6LA-eY-irz" firstAttribute="trailing" secondItem="kh9-bI-dsS" secondAttribute="trailingMargin" id="Sad-e3-XCj"/>
<constraint firstItem="OBM-E6-CLw" firstAttribute="top" secondItem="Kc2-hx-U2o" secondAttribute="bottom" constant="-25" id="TZ8-G8-nBc"/>
<constraint firstItem="DyO-6c-TOr" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" id="Wyg-zs-1b7"/>
<constraint firstItem="RPV-i3-c0r" firstAttribute="top" secondItem="6LA-eY-irz" secondAttribute="bottom" constant="8" id="XlP-HC-AHY"/>
<constraint firstItem="Ckk-uk-wac" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="36" id="YJI-da-WaA"/>
<constraint firstItem="wTB-c8-XC1" firstAttribute="centerY" secondItem="Ckk-uk-wac" secondAttribute="centerY" id="a8u-xR-Seg"/>
<constraint firstItem="lSa-S8-ghM" firstAttribute="trailing" secondItem="kh9-bI-dsS" secondAttribute="trailingMargin" id="c0c-wm-rv7"/>
<constraint firstItem="OBM-E6-CLw" firstAttribute="leading" secondItem="Kc2-hx-U2o" secondAttribute="leading" id="gid-IK-oF7"/>
<constraint firstItem="waM-h0-fUY" firstAttribute="centerX" secondItem="RPV-i3-c0r" secondAttribute="centerX" id="gpe-rb-yqh"/>
<constraint firstItem="OBM-E6-CLw" firstAttribute="trailing" secondItem="Kc2-hx-U2o" secondAttribute="trailing" id="jf8-dN-7Bc"/>
<constraint firstItem="6LA-eY-irz" firstAttribute="top" secondItem="QOH-EK-CIF" secondAttribute="bottom" constant="10" id="kUd-Gc-GSb"/>
<constraint firstItem="q5e-On-hCR" firstAttribute="trailing" secondItem="42a-os-wxE" secondAttribute="trailing" id="o0n-dT-fL7"/>
<constraint firstItem="6LA-eY-irz" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" id="o4T-C1-79x"/>
<constraint firstItem="Kc2-hx-U2o" firstAttribute="top" secondItem="lSa-S8-ghM" secondAttribute="bottom" constant="10" id="o5Z-2v-k5P"/>
<constraint firstItem="DyO-6c-TOr" firstAttribute="trailing" secondItem="kh9-bI-dsS" secondAttribute="trailingMargin" id="p7G-w0-tv2"/>
<constraint firstItem="Ckk-uk-wac" firstAttribute="trailing" secondItem="kh9-bI-dsS" secondAttribute="trailingMargin" id="s9G-e7-3QF"/>
<constraint firstAttribute="trailingMargin" secondItem="42a-os-wxE" secondAttribute="trailing" constant="72" id="sct-CW-JJK"/>
<constraint firstItem="q5e-On-hCR" firstAttribute="top" secondItem="42a-os-wxE" secondAttribute="bottom" constant="-21" id="tqT-8w-o4Y"/>
<constraint firstItem="qYx-jF-3MP" firstAttribute="centerX" secondItem="5Sz-yW-4xM" secondAttribute="centerX" id="uGz-iB-JYX"/>
<constraint firstItem="waM-h0-fUY" firstAttribute="top" secondItem="RPV-i3-c0r" secondAttribute="bottom" constant="8" id="uU2-Ax-sUt"/>
<constraint firstItem="42a-os-wxE" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" constant="71" id="xoe-yV-epb"/>
<constraint firstAttribute="trailingMargin" secondItem="Kc2-hx-U2o" secondAttribute="trailing" constant="17" id="vH0-ve-Tf4"/>
<constraint firstItem="42a-os-wxE" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" constant="10" id="xoe-yV-epb"/>
</constraints>
</view>
<connections>
<outlet property="imageAlpha" destination="5Sz-yW-4xM" id="1Yt-Q1-S0l"/>
<outlet property="imageBlock" destination="Kc2-hx-U2o" id="aLl-MZ-F2x"/>
<outlet property="imageFlip" destination="42a-os-wxE" id="5cL-aE-dDd"/>
<outlet property="imageIsHidden" destination="RPV-i3-c0r" id="dao-TU-LDv"/>
<outlet property="labelAlpha" destination="qYx-jF-3MP" id="pTL-sX-KFJ"/>
Expand Down
23 changes: 22 additions & 1 deletion Example/RxAnimated/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class ViewController: UIViewController {
@IBOutlet var labelFlip: UILabel!
@IBOutlet var labelCustom: UILabel!
@IBOutlet var imageFlip: UIImageView!

@IBOutlet var imageBlock: UIImageView!

@IBOutlet var labelAlpha: UILabel!
@IBOutlet var imageAlpha: UIImageView!

Expand Down Expand Up @@ -67,6 +68,21 @@ class ViewController: UIViewController {
.bind(animated: imageFlip.rx.animated.tick(.right, duration: 1.0).image)
.disposed(by: bag)

var angle: CGFloat = 0.0
// Animate `image` with a custom block
timer
.scan("adorable1") { _, count in
return count % 2 == 0 ? "adorable1" : "adorable2"
}
.map { name in
return UIImage(named: name)!
}
.bind(animated: imageBlock.rx.animated.animation(duration: 0.5, animations: { [weak self] in
angle += 0.2
self?.imageBlock.transform = CGAffineTransform(rotationAngle: angle)
}).image )
.disposed(by: bag)

// Animate layout constraint
timer
.scan(0) { acc, _ in
Expand Down Expand Up @@ -112,6 +128,11 @@ class ViewController: UIViewController {
.bind(to: labelIsHidden.rx.text)
.disposed(by: bag)


//disable animations
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: {
RxAnimated.areAnimationsEnabled.value = false
})
}

}
5 changes: 5 additions & 0 deletions RxAnimated/Core/RxAnimated+animations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ extension AnimatedSink where Base: UIView {
})
return AnimatedSink<Base>(base: self.base, type: type)
}

public func animation(duration: TimeInterval, options: UIViewAnimationOptions = [], animations: @escaping ()->Void) -> AnimatedSink<Base> {
let type = AnimationType<Base>(type: RxAnimationType.animation, duration: duration, animations: { _ in animations() })
return AnimatedSink<Base>(base: self.base, type: type)
}
}

extension AnimatedSink where Base: NSLayoutConstraint {
Expand Down
26 changes: 26 additions & 0 deletions RxAnimated/Core/RxAnimated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
import RxSwift
import RxCocoa

public struct RxAnimated {
public static let areAnimationsEnabled = Variable(true)
fileprivate static var areDefaultHeuristicsEnabled = false
public static func enableDefaultPerformanceHeuristics() {
areDefaultHeuristicsEnabled = true
}
}

// MARK: - basic animation types

/**
Expand Down Expand Up @@ -58,6 +66,12 @@ public struct AnimationType<Base> {
setup?(view)

DispatchQueue.main.async {
if (RxAnimated.areDefaultHeuristicsEnabled && self.shouldDisableAnimationsViaDefaultHeuristics) || !RxAnimated.areAnimationsEnabled.value {
binding?()
self.animations?(view)
return
}

switch self.type {
case .animation:
UIView.animate(withDuration: self.duration, delay: 0, options: self.options, animations: {
Expand All @@ -77,6 +91,18 @@ public struct AnimationType<Base> {
}
}
}

private var shouldDisableAnimationsViaDefaultHeuristics: Bool {
if #available(iOS 11, *) {
return ProcessInfo.processInfo.isLowPowerModeEnabled
|| ProcessInfo.processInfo.thermalState == .serious
|| ProcessInfo.processInfo.thermalState == .critical
|| UIAccessibilityIsReduceMotionEnabled()
} else {
return ProcessInfo.processInfo.isLowPowerModeEnabled
|| UIAccessibilityIsReduceMotionEnabled()
}
}
}

// MARK: - animated reactive extensions
Expand Down

0 comments on commit ac3e33b

Please sign in to comment.