I am trying to create an animation where two line are faded out when the user drags a UIView and faded back in when user releases dragging.
Therefore I have two functions undrawLines (called at pan gesture start) and redrawLines (called at pan gesture end) which are called by my UIPanGestureRecognizer action handler.
func undrawLines() {
line1.opacity = 0.0
line2.opacity = 0.0
line1.removeAllAnimations()
line2.removeAllAnimations()
let opacityLine = CABasicAnimation(keyPath: "opacity")
opacityLine.fromValue = 1.0
opacityLine.toValue = 0.0
opacityLine.duration = 0.15
line1.add(opacityLine, forKey: "disappearLine1")
line2.add(opacityLine, forKey: "disappearLine2")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
mill.line1.removeFromSuperlayer()
mill.line2.removeFromSuperlayer()
})
}
func redrawLines() {
line1.opacity = 1.0
line2.opacity = 1.0
print("redraw")
line1.removeAllAnimations()
line2.removeAllAnimations()
self.layer.addSublayer(line1)
self.layer.addSublayer(line2)
let opacityLine = CABasicAnimation(keyPath: "opacity")
opacityLine.fromValue = 0.0
opacityLine.toValue = 1.0
opacityLine.duration = 0.15
line1.add(opacityMill, forKey: "appearLine1")
line2.add(opacityMill, forKey: "appearLine2")
}
The problem is that when redrawLines gets called while the undrawLines animation is still running, the lines show a strange behavior and opacity is 0.
Here is a demo, the first part shows how it should be, the second one shows the bug:
I believe your issue here is a race condition with your completion handler:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
mill.line1.removeFromSuperlayer()
mill.line2.removeFromSuperlayer()
})
If your user releases, and therefore redrawLines gets called before the 0.3 second timeout, this still gets called and removes the lines.
You probably want to keep a state flag that indicates the current intent and then check it in the asynchronous callback:
func undrawLines() {
self.linesHidden = true // update state
line1.opacity = 0.0
line2.opacity = 0.0
line1.removeAllAnimations()
line2.removeAllAnimations()
let opacityLine = CABasicAnimation(keyPath: "opacity")
opacityLine.fromValue = 1.0
opacityLine.toValue = 0.0
opacityLine.duration = 0.15
line1.add(opacityLine, forKey: "disappearLine1")
line2.add(opacityLine, forKey: "disappearLine2")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: { [weak self] in
if self?.linesHidden == true { // check this is still what we want to do
mill.line1.removeFromSuperlayer()
mill.line2.removeFromSuperlayer()
}
})
}
func redrawLines() {
self.linesHidden = false // update state
line1.opacity = 1.0
line2.opacity = 1.0
print("redraw")
line1.removeAllAnimations()
line2.removeAllAnimations()
self.layer.addSublayer(line1)
self.layer.addSublayer(line2)
let opacityLine = CABasicAnimation(keyPath: "opacity")
opacityLine.fromValue = 0.0
opacityLine.toValue = 1.0
opacityLine.duration = 0.15
line1.add(opacityMill, forKey: "appearLine1")
line2.add(opacityMill, forKey: "appearLine2")
}
You'll clearly need to add the instance var linesHidden to the class for this to work too :)
Related
I have a number of plots, which depict (x,y) data at different time intervals, and wish to plot them in a sequence one after the other(like a gif file). My approach was generate all the plots, use a Timer.scheduledTimer and initially hide all plots, unhiding current plot and hiding previous plot at each fired schedule. The time between each plot hiding/unhiding shows a blank graph for more time than the plots are shown. Each plot has 32x32 data points. How can I speed this up, so I never see a blank graph? Another approach was to fade out one plot, whilst introducing the next, but I see the same effect.
#objc func tapAnimationButton(_ sender: Any) {
isAnimating = !isAnimating
plotSpacesUserInteraction = !plotSpacesUserInteraction
if let _animationButton = animationButton {
_animationButton.isSelected = isAnimating
previousAnimatedPlot = nil
if isAnimating {
animationCounter = 0
for i in 0..<plotDetails.count {
// fieldsplots[i].isHidden = true
fieldsplots[i].opacity = 0.0
}
animationTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(animateGraph(_:)), userInfo: nil, repeats: true)
if let _animationTimer = animationTimer {
animateGraph(_animationTimer)
}
}
else {
animationTimer?.invalidate()
animationTimer = nil
}
}
}
#objc func animateGraph(_ timer: Timer) {
if animationSeconds > 120.0 {
timer.invalidate()
self.animationTimer = nil
animationSeconds = 0.0
animationCounter = 0
previousAnimatedPlot = nil
}
else {
if let currentAnimatedPlot = self.graph.plot(at: animationCounter) {
// previousAnimatedPlot?.isHidden = true
// currentAnimatedPlot.isHidden = false
previousAnimatedPlot?.opacity = 1.0
let fadeOutAnimation = CABasicAnimation(keyPath: "opacity")
fadeOutAnimation.duration = 0.1
fadeOutAnimation.isRemovedOnCompletion = false
fadeOutAnimation.fillMode = CAMediaTimingFillMode.forwards
fadeOutAnimation.toValue = Float(0.0)
previousAnimatedPlot?.add(fadeOutAnimation, forKey: "animateOpacity")
currentAnimatedPlot.opacity = 0.0
let fadeInAnimation = CABasicAnimation(keyPath: "opacity")
fadeInAnimation.duration = 0.1
fadeInAnimation.isRemovedOnCompletion = false
fadeInAnimation.fillMode = CAMediaTimingFillMode.forwards
fadeInAnimation.toValue = Float(1.0)
currentAnimatedPlot.add(fadeInAnimation, forKey: "animateOpacity")
previousAnimatedPlot = currentAnimatedPlot
}
animationSeconds += 0.5
animationCounter += 1;
if animationCounter >= plotDetails.count {
animationCounter = 0
}
}
}
The fade in/out method actually works...not sure how I missed that.
As an add-on to answer, in order to produce a gif image one needs to hide/unhide the plots in sequence
for currentPlot in self.graph.allPlots() {
currentPlot.isHidden = true
}
var images: [UIImage] = []
var previousPlot: CPTPlot?
for currentPlot in self.graph.allPlots() {
if let _previousPlot = previousPlot {
_previousPlot.isHidden = true
}
currentPlot.isHidden = false
if let image = graph.imageOfLayer() {
images.append(image)
}
previousPlot = currentPlot
}
for currentPlot in self.graph.allPlots() {
currentPlot.isHidden = false
}
if images.count > 2 {
TwoDPlot_Utilities.GIFExport(with: images, plot: thisplot, plotIndex: plotIndex, frameDelay: 0.5)
}
I am calling users "Weight" from firebase database in a label (which is stored under 'profile/uid') which has two categories "kg" and "lbs". I would like the label to be able to show user's weight in kg and then fade out to lbs and then back to kg again so on and so fourth. So far i have written the following code which is constantly fading in and out the weight in lbs and i'm unsure how to get it to display the weight in kg once lbs fades out:
func weight() {
let userRf = self.firDatabaseRef.child("profile").child("\(User!.uid)").observe(FIRDataEventType.value, with: { (snapshot) in
if snapshot.exists() {
if let weight = snapshot.childSnapshot(forPath: "weight").value as? [String: AnyObject] {
if let kg = weight["kg"] as? Float, let lbs = weight["lbs"] as? Int {
let animation: CATransition = CATransition()
animation.duration = 2.0
animation.type = kCATransitionFade
animation.repeatDuration = .infinity
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
self.displayWeight.layer.add(animation, forKey: "changeTextTransition")
self.displayWeight.text = "\(lbs) lbs"
}
}
}
}
}
One way is to use a repeating UIVIew animation along with a Timer:
// set up some helpful constants
let kgsText = "\(kgs) kgs"
let lbsText = "\(lbs) lbs"
let fadeDuration = 2.0
// keep track of which unit is being displayed
var isDisplayingKgs = true
// animate the fade in and out, repeating
UIView.animate(withDuration: fadeDuration, delay: 0, options: [.autoreverse, .repeat], animations: {
label.alpha = 0
}, completion: nil)
// start the timer to switch the text after a short delay
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + fadeDuration) {
self.displayWeight.text = lbsText
isDisplayingKgs = false
Timer.scheduledTimer(withTimeInterval: fadeDuration * 2, repeats: true, block: { timer in
self.displayWeight.text = isDisplayingKgs ? lbsText : kgsText
isDisplayingKgs = !isDisplayingKgs
})
}
This is assuming that your label starts out visible (alpha = 1). The dispatch delay is to ensure that the first transition occurs when the label is faded out the first time, but before the timer kicks in.
Here is what the animation looks like in action:
I'm trying to execute 2 different animations, after the first complete, using isAnimating.
but, I see only the first animation...
if anims[0] == 1{
startAnimation(image : #imageLiteral(resourceName: "first"))
}
if anims[1] == 2{
while myView.isAnimating {
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.05))
}
}
startAnimation(image : #imageLiteral(resourceName: "second") , time : Int)
spriteSheet returns UIImage array after cropping..
func startAnimation(image : UIImage , time : Int){
myView.animationImages = image.spriteSheet(cols: 19, rows: 1)
myView.animationDuration = 1
myView.animationRepeatCount = time
myView.startAnimating()
}
You can always chain animations like
UIView.animate(withDuration: 1, animations: {
//do your animation here
}) { (state) in
UIView.animate(withDuration: 1, animations: {
//do your second animation
})
}
If you are using CABasicAnimations then you can use beginTime and duration to chain them up :)
var totalDuration = 0
let baseicAnim1 = CABasicAnimation()
baseicAnim1.beginTime = CACurrentMediaTime()
totalDuration += 10
baseicAnim1.duration = CFTimeInterval(totalDuration)
let basicAnim2 = CABasicAnimation()
basicAnim2.beginTime = CACurrentMediaTime() + CFTimeInterval(totalDuration)
totalDuration += 10
basicAnim2.duration = CFTimeInterval(totalDuration)
EDIT
Using while loop to keep checking if animation has completed its execution or not is never a suggested approach
EDIT :
Try this,
func startAnimation(image : UIImage , time : Int,completionBlock : (()->())?){
let animationDuration = 1
myView.animationImages = image.spriteSheet(cols: 19, rows: 1)
myView.animationDuration = animationDuration
myView.animationRepeatCount = time
myView.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + animationDuration, execute: {
if let block = completionBlock {
block()
}
})
}
Now your startAnimation function takes completion block as its parameter and executes the completion block after animationDuration. So you can get to know when animation ends :)
to chain simply call
self.startAnimation(image: #imageLiteral(resourceName: "first"), time: 1) {
self.startAnimation(image: #imageLiteral(resourceName: "second"), time: 1, completionBlock: nil)
}
Hope it helps
SITUTATION:
I have created a pulsating animation and it works.
Using the repeatForever property, I was able to make it continuous as I wanted.
But one issue remains that I wasn't able to fix after doing A LOT of research.
EXPLANATION:
The only problem is that I don't know how to add one more animation that makes the Image go back to it's original size before pulsating again.
The challenge for me resides in the fact that if I declare it after the other one, it will not be called as it will not be included in the repeatForeverloop.
Therefore, the animation I have right now is not smooth as once it completes, the image instantly goes back to it's original size before repeating itself.
WHAT HAPPENS:
1) Image pulsates to 1.2 of current size thx to animation.
THIS IS NOT SMOOTH:
2) Animation finishes at 1.2 and Image instantly "warps" back to 1.0
3) Animation repeats itself. Image pulsates to 1.2 again.
PROBLEM: How can I change my pulsatingAnimation so that my image goes back to it's original size in a smooth way before pulsating again ?
CODE:
import Foundation
import pop
class AnimationEngine {
func pulsate(object: UIImageView) {
let pulsateAnim = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
pulsateAnim.velocity = NSValue(CGSize: CGSizeMake(0.1, 0.1))
pulsateAnim.toValue = NSValue(CGSize: CGSizeMake(1.2, 1.2))
pulsateAnim.springBounciness = 18
pulsateAnim.dynamicsFriction = 20
pulsateAnim.springSpeed = 1.0
pulsateAnim.repeatForever = true
// pulsateAnim.autoreverses = true
object.layer.pop_addAnimation(pulsateAnim, forKey: "layerScaleSpringAnimation")
}
}
I found the Solution.
SOLUTION:
func pulsate(object: UIImageView) {
let pulsateAnim = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
let returnToSizeAnim = POPBasicAnimation(propertyNamed: kPOPLayerScaleXY)
object.layer.pop_addAnimation(pulsateAnim, forKey: "layerScaleSpringAnimation")
pulsateAnim.velocity = NSValue(CGSize: CGSizeMake(0.1, 0.1))
pulsateAnim.toValue = NSValue(CGSize: CGSizeMake(1.2, 1.2))
pulsateAnim.springBounciness = 30
pulsateAnim.dynamicsFriction = 20
pulsateAnim.springSpeed = 1.0
pulsateAnim.completionBlock = {(animation, finished) in
object.layer.pop_addAnimation(returnToSizeAnim, forKey: "layerScaleSpringAnimation")
}
returnToSizeAnim.toValue = NSValue(CGSize: CGSizeMake(1.0, 1.0))
returnToSizeAnim.duration = 0.5
returnToSizeAnim.completionBlock = {(animation, finished) in
object.layer.pop_addAnimation(pulsateAnim, forKey: "layerScaleSpringAnimation")
}
}
func pulsate2(object: UIImageView) {
let pulsateAnim = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
let returnToSizeAnim = POPBasicAnimation(propertyNamed: kPOPLayerScaleXY)
object.layer.pop_addAnimation(pulsateAnim, forKey: "layerScaleSpringAnimation")
pulsateAnim.velocity = NSValue(CGSize: CGSizeMake(0.1, 0.1))
pulsateAnim.toValue = NSValue(CGSize: CGSizeMake(1.2, 1.2))
pulsateAnim.springBounciness = 30
pulsateAnim.dynamicsFriction = 20
pulsateAnim.springSpeed = 1.0
pulsateAnim.completionBlock = {(animation, finished) in
object.layer.pop_addAnimation(returnToSizeAnim, forKey: "layerScaleSpringAnimation")
}
returnToSizeAnim.toValue = NSValue(CGSize: CGSizeMake(1.0, 1.0))
returnToSizeAnim.duration = 0.5
returnToSizeAnim.completionBlock = {(animation, finished) in
object.layer.pop_addAnimation(pulsateAnim, forKey: "layerScaleSpringAnimation")
}
}
N.B.:
I need to declare one pulsate function for each object I want to use it on. If I don't do that, the second time I call the function, it won't work properly because an instance of the animations is already running.
I'm a huge ReactiveCocoa nerd so I built a class around POPAnimation so I could deal with animations in a more functional manner.
import Foundation
import ReactiveCocoa
import pop
import Result
/**
`UIView` wrapper for performing reactive-based pop animations.
*/
public struct ReactivePOPView<View: UIView> {
/// View object
private weak var view: View?
/// `SignalProducer` object when the wrapper is about to deallocate.
private let willDealloc: SignalProducer<Void, NoError>
/// Wrapper on the view property.
private var animator: View? { return self.view }
/**
Create an animation wrapper with a view.
- parameter view: `UIView` object.
*/
public init(_ view: View) {
self.view = view
self.willDealloc = view
.willDeallocSignalProducer()
}
}
/**
Binds a `Signal` that emits an animation object to a `ReactivePOPView`.
- parameter view: `ReactivePOPView` object.
- parameter signal: `Signal` object.
- returns: `Disposable` object.
*/
public func <~ <T, Animation: POPAnimation>(view: ReactivePOPView<T>, signal: Signal<Animation, NoError>) -> Disposable {
let disposable = CompositeDisposable()
let viewDisposable = view.willDealloc.startWithCompleted {
disposable.dispose()
}
disposable.addDisposable(viewDisposable)
let signalDisposable = signal.observe(Observer(next: {
view.animator?.pop_addAnimation($0, forKey: $0.name ?? "")
}, completed: {
disposable.dispose()
}))
disposable.addDisposable(signalDisposable)
return disposable
}
/**
Binds a `SignalProducer` that emits an animation object to a `ReactivePOPView`.
- parameter view: `ReactivePOPView` object.
- parameter producer: `SignalProducer` object.
- returns: `Disposable` object.
*/
public func <~ <T, Animation: POPAnimation>(view: ReactivePOPView<T>, producer: SignalProducer<Animation, NoError>) -> Disposable {
var disposable: Disposable!
producer.startWithSignal { signal, signalDisposable in
view <~ signal
disposable = signalDisposable
view.willDealloc.startWithCompleted {
signalDisposable.dispose()
}
}
return disposable
}
/**
Bind a reactive animation property to a `ReactivePOPView` property.
- parameter destinationProperty: `ReactivePOPView` property.
- parameter sourceProperty: Animation property.
- returns: `Disposable` object.
*/
public func <~ <T, P: PropertyType where P.Value == POPAnimation>(destinationProperty: ReactivePOPView<T>, sourceProperty: P) -> Disposable {
return destinationProperty <~ sourceProperty.producer
}
With this class, you can do things like:
ReactivePOPView<UIButton>(self.loginButton) <~ self.someSignal.signal
.flatMap(.Concat) { _ in SignalProducer<POPBasicAnimation, NoError> in
let animation ...
// construct animation here
return SignalProducer(value: animation)
}
.repeat(0)
I also have a wrapper class for constraints if you want that.
I dont know Facebook pop , so I speak only about the analysis and logic to how to implement this feature (pulse effect)
As I've write in comments, what you want to do seems exactly what happened many times in SpriteKit where you , to make a specific animation (SKAction), have to build a series of actions.
Just to make a SpriteKit example, this is what I do to make a specific pulse effect:
let titleForward = runTitleForward()
let wait = SKAction.waitForDuration(5.0)
let titleBackward = runTitleBackward()
let titleAnim = SKAction.repeatActionForever((SKAction.sequence([titleForward, wait, titleBackward,wait ])))
title.runAction(titleAnim, withKey:"titleAnimation")
func runTitleForward()-> SKAction {
let atlascTexturesArray = self.myManager.atlascTexturesDictionary["title-anim"]
let texturesAction = SKAction.animateWithTextures(atlascTexturesArray!,timePerFrame: 0.09)
return texturesAction
}
func runTitleBackward()-> SKAction {
let texturesAction = runTitleForward()
let texturesReverseAction = SKAction.reversedAction(texturesAction)
return texturesReverseAction()
}
Hope you help to do the same thing in UIKit.
UPDATE: (I don't test it..)
func delayAnimation(duration:NSTimeInterval)-> CABasicAnimation {
let animation : CABasicAnimation = CABasicAnimation(keyPath: "opacity");
animation.delegate = self
animation.fromValue = 1
animation.toValue = 1 // fake animation, just to obtain duration delay..
animation.duration = duration
return animation
}
func pulseElements () -> (POPSpringAnimation,POPSpringAnimation) {
// zoom in
let pulsateInAnim = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
pulsateInAnim.velocity = NSValue(CGSize: CGSizeMake(0.1, 0.1))
pulsateInAnim.toValue = NSValue(CGSize: CGSizeMake(1.2, 1.2))
pulsateInAnim.springBounciness = 18
pulsateInAnim.dynamicsFriction = 20
pulsateInAnim.springSpeed = 1.0
// zoom out
let pulsateOutAnim = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
...
return (pulsateInAnim,pulsateOutAnim)
}
func pulseAnimation() {
let (pulsateInAnim,pulsateOutAnim) = pulseElements()
let wait = delayAnimation(1.0)
var pulseAnimationGroup: CAAnimationGroup = CAAnimationGroup()
pulseAnimationGroup.animations = [pulsateInAnim, wait, pulsateOutAnim, wait]
pulseAnimationGroup.duration = 0.5
pulseAnimationGroup.repeatCount = Float.infinity
object.layer.addAnimation(pulseAnimationGroup, forKey: layerScaleSpringAnimation)
}
I have some problems with my version of this loadingOverlay singleton.
What's supposed to happen, is it comes onto the screen, with a view and a label that has the text, "Loading, please wait." or something like that. then if loading is longer than 2 seconds (i've changed it to 10 for debugging) the text changes to a random cute phrase.
first of all the animation that should change the text doesn't seem to happen. instead, the text just instantly changes.
more importantly, If, for some reason, my asynchronous call block is executed multiple times, I only want the most recent call to it to run, and I want the previous instances of it to terminate before running.
I was reading about callbacks and promises, which look promising. Is that a swifty pattern to follow?
by the way, as I'm learning swift and iOS, I've been experimenting, and I tried [unowned self] and now i'm experimenting with [weak self], but I'm not really certain which is most appropriate here.
// from http://stackoverflow.com/questions/33064908/adding-removing-a-view-overlay-in-swift/33064946#33064946
import UIKit
class LoadingOverlay{
static let sharedInstance = LoadingOverlay()
//above swifty singleton syntax from http://krakendev.io/blog/the-right-way-to-write-a-singleton
var overlayView = UIView()
var spring: CASpringAnimation!
var springAway: CASpringAnimation!
var hidden = false
private init() {} //This line prevents others from using the default () initializer for this class
func setupSpringAnimation(startY: CGFloat, finishY: CGFloat) {
overlayView.layer.position.y = startY
spring = CASpringAnimation(keyPath: "position.y")
spring.damping = 10
spring.fromValue = startY
spring.toValue = finishY
spring.duration = 1.0
spring.fillMode = kCAFillModeBackwards
}
func showOverlay() {
print("show overlay")
overlayView.alpha = 1
hidden = false
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate,
let window = appDelegate.window {
setupSpringAnimation(-window.frame.height / 2, finishY: window.frame.height / 2)
let overlayViewFramesize = 0.65 * min(window.frame.height, window.frame.width)
overlayView.frame = CGRectMake(0, 0, overlayViewFramesize, overlayViewFramesize)
overlayView.center = window.center
overlayView.backgroundColor = UIColor.greenColor()
overlayView.clipsToBounds = true
overlayView.layer.cornerRadius = overlayViewFramesize / 8
let label = UILabel(frame: CGRectMake(0,0,overlayViewFramesize * 0.8 , overlayViewFramesize))
label.text = " \nLoading, please wait\n "
label.tag = 12
overlayView.addSubview(label)
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.numberOfLines = 0 //as many as needed
label.sizeToFit()
label.textAlignment = NSTextAlignment.Center
label.center = CGPointMake(overlayViewFramesize / 2, overlayViewFramesize / 2)
overlayView.bringSubviewToFront(label)
window.addSubview(overlayView)
overlayView.layer.addAnimation(spring, forKey: nil)
RunAfterDelay(10.0) {
if self.hidden == true { return }
//strongSelf boilerplate code technique from https://www.raywenderlich.com/133102/swift-style-guide-april-2016-update?utm_source=raywenderlich.com+Weekly&utm_campaign=ea47726fdd-raywenderlich_com_Weekly4_26_2016&utm_medium=email&utm_term=0_83b6edc87f-ea47726fdd-415681129
UIView.animateWithDuration(2, delay: 0, options: [UIViewAnimationOptions.CurveEaseInOut, UIViewAnimationOptions.BeginFromCurrentState, UIViewAnimationOptions.TransitionCrossDissolve], animations: { [weak self] in
guard let strongSelf = self else { return }
(strongSelf.overlayView.viewWithTag(12) as! UILabel).text = randomPhrase()
(strongSelf.overlayView.viewWithTag(12) as! UILabel).sizeToFit()
print ((strongSelf.overlayView.viewWithTag(12) as! UILabel).bounds.width)
(strongSelf.overlayView.viewWithTag(12) as! UILabel).center = CGPointMake(overlayViewFramesize / 2, overlayViewFramesize / 2)
}, completion: { (finished: Bool)in
print ("animation to change label occured")})
}
}
}
func hideOverlayView() {
hidden = true
UIView.animateWithDuration(1.0, delay: 0.0, options: [UIViewAnimationOptions.BeginFromCurrentState], animations: { [unowned self] in
//I know this is clunky... what's the right way?
(self.overlayView.viewWithTag(12) as! UILabel).text = ""
self.overlayView.alpha = 0
}) { [unowned self] _ in
//I know this is clunky. what's the right way?
for view in self.overlayView.subviews {
view.removeFromSuperview()
}
self.overlayView.removeFromSuperview()
print("overlayView after removing:", self.overlayView.description)
}
//here i have to deinitialize stuff to prepare for the next use
}
deinit {
print("Loading Overlay deinit")
}
}
What I basically wanted, was to be able to delay a block of code, and possibly cancel it before it executes. I found the answer here:
GCD and Delayed Invoking