UILabel is not appearing in delayed sequence as expected - ios

I want to make my UILabels appear in a delayed sequence. Thus one after another.
The code below works when I make them fade in using the alpha value but it doesn't do what I want it to do when I use the .hidden property of the UILabels.
The code makes my UILabels appear at all the same time instead of sum1TimeLabel first after 5 seconds, sum2TimeLabel second after 30 seconds and finally sum3TimeLabel third after 60 seconds. What am I doing wrong?
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
UIView.animateWithDuration(5.0, animations: {
self.sum1TimeLabel!.hidden = false;
})
UIView.animateWithDuration(30.0, animations: {
self.sum2TimeLabel!.hidden = false;
})
UIView.animateWithDuration(60.0, animations: {
self.sum3TimeLabel!.hidden = false;
})
}

As explained by SO user #matt in this answer, you can simply create a delay function with Grand Central Dispatch instead of using animateWithDuration, as animateWithDuration is usually meant for animating things over a period of time, and since the value you are animating is a Bool, it can't animate it.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
delay(5.0) {
self.sum1TimeLabel?.hidden = false
}
delay(30.0) {
self.sum2TimeLabel?.hidden = false
}
delay(60.0) {
self.sum3TimeLabel?.hidden = false
}
}
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}

// Try this
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.sum1TimeLabel!.hidden = true;
self.sum2TimeLabel!.hidden = true;
self.sum3TimeLabel!.hidden = true;
UIView.animateWithDuration(5.0, animations: {
}, completion: {
self.sum1TimeLabel!.hidden = false;
})
UIView.animateWithDuration(30.0, animations: {
}, completion: {
self.sum2TimeLabel!.hidden = false;
})
UIView.animateWithDuration(60.0, animations: {
}, completion: {
self.sum3TimeLabel!.hidden = false;
})
}

class ViewController: UIViewController {
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label1.alpha = 0
label2.alpha = 0
label3.alpha = 0
UIView.animateWithDuration(5.0, animations: { () -> Void in
print("animation on label1")
self.label1.alpha = 0.2
}) { (b) -> Void in
print("complete on label1")
self.label1.alpha = 1
UIView.animateWithDuration(5.0, animations: { () -> Void in
print("animation on label2")
self.label2.alpha = 0.2
}, completion: { (b) -> Void in
print("complete on label2")
self.label2.alpha = 1
UIView.animateWithDuration(5.0, animations: { () -> Void in
print("animation on label3")
self.label3.alpha = 0.2
}, completion: { (b) -> Void in
print("complete on label3")
self.label3.alpha = 1
})
})
}
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You are NOT able to animate hidden property. there is two states only. how to change hidden continuously with time? instead you can animate alpha :-). try change the alpha value inside animation block to 0, or to 1.

Related

UIViewPropertyAnimator not respecting duration and delay parameters

I have a very strange issue in my app. I'm using UIViewPropertyAnimator to animate changing images inside UIImageView.
Sounds like trivial task but for some reaso my view ends up changing the image instantly so I end up with images flashing at light speed instead of given duration and delay parameters.
Here's the code:
private lazy var images: [UIImage?] = [
UIImage(named: "widgethint"),
UIImage(named: "shortcuthint"),
UIImage(named: "spotlighthint")
]
private var imageIndex = 0
private var animator: UIViewPropertyAnimator?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
animator = repeatingAnimator()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
animator?.stopAnimation(true)
}
private func repeatingAnimator() -> UIViewPropertyAnimator {
return .runningPropertyAnimator(withDuration: 2, delay: 6, options: [], animations: {
self.imageView.image = self.images[self.imageIndex]
}, completion: { pos in
if self.imageIndex >= self.images.count - 1 {
self.imageIndex = 0
} else {
self.imageIndex += 1
}
self.animator = self.repeatingAnimator()
})
}
So, according to the code animation should take 2 seconds to complete and start after 6 seconds delay, but it starts immediately and takes milliseconds to complete so I end up with horrible slideshow. Why could it be happening?
I also tried using the UIViewPropertyAnimator(duration: 2, curve: .linear) and calling repeatingAnimator().startAnimation(afterDelay: 6) but the result is the same.
Okay, I've figured it out, but it's still a bit annoying that such simple task cannot be done using the "Modern" animation API.
So, animating images inside UIImageView is apparently not supported by UIViewPropertyAnimator, during debugging I tried animating view's background color and it was working as expected. So I had to use Timer instead and old UIView.transition(with:) method
Here's the working code:
private let animationDelay: TimeInterval = 2.4
private var animationTimer: Timer!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setAnimation(true)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
setAnimation(false)
}
private func setAnimation(_ enabled: Bool) {
guard enabled else {
animationTimer?.invalidate()
animationTimer = nil
return
}
animationTimer = Timer.scheduledTimer(withTimeInterval: animationDelay, repeats: true) { _ in
UIView.transition(with: self.imageView, duration: 0.5, options: [.curveLinear, .transitionCrossDissolve], animations: {
self.imageView.image = self.images[self.imageIndex]
}, completion: { succ in
guard succ else {
return
}
if self.imageIndex >= self.images.count - 1 {
self.imageIndex = 0
} else {
self.imageIndex += 1
}
})
}
}
Hopefully it will save someone some headaches in the future

Cannot animate UIImage with timing delay in Swift

I am attempting to animate the alpha of a UIImage after 3 seconds has passed. By default the alpha is set to 0 and after 3 seconds, the alpha should change to 1, thus displaying the image to the user. My code I wrote for my animation does set the alpha to 0, but I am unable to change the the alpha to 1 after 3 seconds. I am newer to swift and not sure where I am going wrong with this. Here is my code.
import UIKit
class WelcomeOneViewController: UIViewController {
#IBOutlet weak var swipeImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
swipeImageView.alpha = 0
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
displaySwipeView()
}
func displaySwipeView() {
UIView.animate(withDuration: 1.0, delay: 3.0, options: .beginFromCurrentState, animations: {
DispatchQueue.main.async { [weak self] in
self?.swipeImageView.alpha = 1
}
}, completion: nil)
}
}
Try it with this code:
import UIKit
class WelcomeOneViewController: UIViewController {
#IBOutlet weak var swipeImageView: UIImageView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
swipeImageView.alpha = 0
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
displaySwipeView()
}
func displaySwipeView() {
swipeImageView.alpha = 0
UIView.animate(withDuration: 1.0, delay: 3.0, options: [], animations: {
self.swipeImageView.alpha = 1
}, completion: nil)
}
}
Try doing it on main queue like this:
func displaySwipeView() {
UIView.animate(withDuration: 1.0, delay: 3.0, options: .beginFromCurrentState, animations: {
DispatchQueue.main.async { [weak self] in
self?.swipeImageView.alpha = 1
}
}, completion: nil)
}
Hope it helps!!

How would i go about fading in and out from an array of different words or sentences?

So i built a fade in fade out for a label animation in Swift already. But I want to fade in and out from an array of different sentences, so each time fade in and fade out, it would be different staff.
override func viewDidLoad()
{
super.viewDidLoad()
label.alpha = 0
animatedText()
}
func animatedText(){
UIView.animateWithDuration(2.0, animations: {
self.label.alpha = 1.0
}, completion: {
(Completed: Bool) -> Void in
UIView.animateWithDuration(1.0, delay: 2.0, options: UIViewAnimationOptions.CurveLinear, animations: {
self.label.alpha = 0
}, completion: {
(Completed: Bool) -> Void in
self.animatedText()
})
})
}
Try this function:
class ViewController: UIViewController {
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
var sentences = ["The cat sat.", "The rhino ate.", "The monkey laughed", "The zebra ran.", "The fish swam", "The potato grew."]
override func viewDidLoad() {
super.viewDidLoad()
label1.text = sentences[0]
label2.text = sentences[1]
label1.alpha = 1.0
label2.alpha = 0.0
}
var counter = 1
#IBAction func animateBtnPressed(_ sender: UIButton) {
fadeText(counter: &counter, duration: 2.0)
}
func fadeText(counter: inout Int, duration: TimeInterval) {
if counter < sentences.count {
if counter % 2 != 0 {
UIView.animate(withDuration: duration, animations: {
self.label1.alpha = 0.0
self.label2.alpha = 1.0
}, completion: { finished in
if finished {
counter += 1
if counter <= (self.sentences.count - 1) {
self.label1.text = self.sentences[counter]
} else {
return
}
self.fadeText(counter: &counter, duration: duration)
}
})
} else {
UIView.animate(withDuration: duration, animations: {
self.label1.alpha = 1.0
self.label2.alpha = 0.0
}, completion: { finished in
if finished {
counter += 1
if counter <= (self.sentences.count - 1) {
self.label2.text = self.sentences[counter]
} else {
return
}
self.fadeText(counter: &counter, duration: duration)
}
})
}
}
}
}
Assumptions:
There is an array named sentences (you can alter this) on the ViewController that contains Strings.
There is a countervariable that starts at 1
The UILabels are overlapping in the storyboard.
Other than that you just have to call the function with the counter variable with an & operator in front, because for fadeText to alter the counter variable it needs the address, in memory, of the counter variable, not the value (notice the inout in the parameters of fadeText).
If you have any questions feel free to ask!
I would advise creating an int variable outside the scope of the animatedText() function and simply adding 1 to this variable each time your completion block is called. You can then update the label's text to the string in the array using this int variable as the array's index each time the function is called.

UITableViewCell setHighlighted, setSelected "animateAlongsideTransition" or similar

I want to be able to add custom animations to subclasses of UITableViewCell that would override methods such as:
override func setHighlighted(highlighted: Bool, animated: Bool) {
}
override func setSelected(selected: Bool, animated: Bool) {
}
and match the animation curve and animation duration for the default animations those methods perform.
In other words, how can I find the information about the current animations provided by Apple. I need this in order to add my own custom animations that perfectly matches the default ones
You can subclass your custom cell in your table view.
And here I create a simple example in Swift where I change the value of a label inside the cell:
import UIKit
class userTableViewCell: UITableViewCell {
#IBOutlet weak var userLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
self.highlighted = false
self.userLabel.alpha = 0.0
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
self.highlighted = true
} else {
self.highlighted = false
}
// Configure the view for the selected state
}
override var highlighted: Bool {
get {
return super.highlighted
}
set {
if newValue {
// you could put some animations here if you want
UIView.animateWithDuration(0.7, delay: 1.0, options: .CurveEaseOut, animations: {
self.userLabel.text = "select"
self.userLabel.alpha = 1.0
}, completion: { finished in
print("select")
})
}
else {
self.userLabel.text = "unselect"
}
super.highlighted = newValue
}
}
}
And with the storyboard what you must have:

Xcode ViewDidLoad animation Swift

I'm having some trouble with the animations in a tabbed app in Xcode.
I have the viewDidLoad and the viewDidAppear parts, the problem is that I have two labels label1 and label2. I would like label1 appearing only once when the application loads, and label2 appearing every time I go back to the FirstView.
So the logical thing to do would be:
override func viewDidLoad(animated: Bool) {
super.viewDidLoad()
self.label1.alpha = 0
self.label2.alpha = 0
//this is the animation
UIView.animateWithDuration(2.0, animations: { () -> Void in
self.label1.alpha = 1.0
//this is what happens after a delay
[DELAY CODE]
self.label1.alpha = 0.0
})
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
UIView.animateWithDuration(2.0, animations: { () -> Void in
self.label2.alpha = 1.0
}
Essentially what this should do is make label1 appear and disappear only once, and make label2 appear every time firstView shows up on the screen. The problem is that I have an error in the first line telling me "Method does not override any method from its superclass". So how can I do what I am trying to achieve?
You have to remove the animated:Bool from your viewDidLoad-method. there is no such parameter in this method.
So it should look like that:
override func viewDidLoad() {
Try This :
UIView.animateWithDuration(2.0,
animations: { () -> Void in
// Your animation method
self.label1.alpha = 1.0
}) { (Bool) -> Void in
// Call your delay method here.
// Delay - 3 seconds
NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(3),
target: self,
selector: "hideLabel",
userInfo: nil,
repeats: false)
}
//
func hideLabel {
UIView.animateWithDuration(2,
animations: { () -> Void in
self.label1.alpha = 0.0
})
}

Resources