Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace UIView animation to CABasicAnimation #16

Merged
merged 5 commits into from
Nov 3, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace UIView animation to CABasicAnimation
  • Loading branch information
leo150 committed Oct 25, 2016
commit bc5b3f39ca14f4f3f1d067d18806b4861589c64c
47 changes: 38 additions & 9 deletions Source/ARSLineProgressHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,51 @@ func ars_presentLoader(_ loader: ARSLoader, onView view: UIView?, completionBloc
ars_window()!.addSubview(emptyView)
}

emptyView.alpha = 0.1
UIView.animate(withDuration: ars_config.backgroundViewPresentAnimationDuration, delay: 0.0, options: .curveEaseOut, animations: {
emptyView.alpha = 1.0
}, completion: { _ in completionBlock?() })
CATransaction.begin()
CATransaction.setCompletionBlock(completionBlock)

let alphaAnimation = CABasicAnimation(keyPath: "opacity")
alphaAnimation.fromValue = 0
alphaAnimation.toValue = 1
alphaAnimation.duration = ars_config.backgroundViewPresentAnimationDuration

emptyView.layer.removeAnimation(forKey: "alpha")
emptyView.layer.add(alphaAnimation, forKey: "alpha")

CATransaction.commit()
}
}

func ars_hideLoader(_ loader: ARSLoader?, withCompletionBlock block: (() -> Void)?) {
guard let loader = loader else { return }

ars_dispatchOnMainQueue {
UIView.animate(withDuration: ars_config.backgroundViewDismissAnimationDuration, delay: 0.0, options: .curveEaseOut, animations: {
loader.emptyView.alpha = 0.0
loader.backgroundView.transform = CGAffineTransform(scaleX: ars_config.backgroundViewDismissTransformScale,
y: ars_config.backgroundViewDismissTransformScale)
}, completion: { _ in block?() })
let currentLayer = loader.emptyView.layer.presentation()

let alpha = Double(currentLayer?.opacity ?? 0)
let fixedTime = alpha * ars_config.backgroundViewPresentAnimationDuration

CATransaction.begin()
CATransaction.setCompletionBlock(block)
let alphaAnimation = CABasicAnimation(keyPath: "opacity")
alphaAnimation.fromValue = alpha
alphaAnimation.toValue = 0
alphaAnimation.duration = fixedTime
alphaAnimation.isRemovedOnCompletion = true

loader.emptyView.layer.removeAnimation(forKey: "alpha")
loader.emptyView.alpha = 0
loader.emptyView.layer.add(alphaAnimation, forKey: "alpha")

let scaleAnimation = CABasicAnimation(keyPath: "transform")
scaleAnimation.fromValue = CGAffineTransform(scaleX: 1, y: 1)
scaleAnimation.toValue = CGAffineTransform(scaleX: ars_config.backgroundViewDismissTransformScale,
y: ars_config.backgroundViewDismissTransformScale)
scaleAnimation.duration = fixedTime
scaleAnimation.isRemovedOnCompletion = true

loader.backgroundView.layer.removeAnimation(forKey: "transform")
loader.backgroundView.layer.add(scaleAnimation, forKey: "transform")
}

ars_dispatchAfter(ars_config.backgroundViewDismissAnimationDuration) {
Expand Down