Swift Infinite Fade in and Out Loop - xcode

How can I make a effect in swift similar to this:
I want the animation to loop forever.

For iOS
UIViewAnimationOptions set provides different handy options to achieve a combination of beautiful and complex animations. For your particular scenario you will require two of the options.
UIViewAnimationOptions.Repeat
UIViewAnimationOptions.AutoReverse
Check out the code below for implementation.
Code:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.backgroundColor = UIColor.blueColor()
self.view.addSubview(view)
UIView.animateWithDuration(1,
delay: 0,
options: [UIViewAnimationOptions.Autoreverse, UIViewAnimationOptions.Repeat],
animations: {
view.backgroundColor = UIColor.clearColor()
},
completion: nil)
}
}
Explanation:
I have created a view with a specific frame for demo purpose.
The part you are interested in is the UIView.animateWithDuration method. Notice that I have provided an array [UIViewAnimationOptions.AutoReverse, UIViewAnimationOptions.Repeat] in the options parameter.
These two options are enough to achieve a smooth and forever looping animation like below.
https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/6Iow7n7WiWf6Naz/autoReverse.gif
If you don't want to reverse the animation, just remove UIViewAnimationOptions.AutoReverse from the array in the options parameter. You will get an animation like this.
https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/8fyRUlzqNHSQI47/noreverse.gif

For iOS
let viewSquare be the name of the blue square in your question.
UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat,.autoreverse], animations: {
viewSquare.alpha = 0.0
}, completion: nil)

Swift 5.1
let duration = 0.5
func fadeIn(finished: Bool) {
UIView.animate(withDuration: self.duration, delay: 0,
options: [.curveEaseInOut],
animations: { self.tftMap.alpha = 1 }, completion: self.fadeOut)
}
func fadeOut(finished: Bool) {
UIView.animate(withDuration: self.duration, delay: 0,
options: [.curveEaseInOut],
animations: { self.tftMap.alpha = 0 }, completion: self.fadeIn)
}

I assume you are programming for iOS.
Play around with the duration to see what suits you best:
class ViewController: UIViewController {
#IBOutlet weak var myView: UIView!
let duration = 0.5
override func viewDidLoad() {
super.viewDidLoad()
self.fadeOut(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func fadeIn(finished: Bool) {
UIView.animateWithDuration(self.duration, delay: 0, options: [.CurveEaseInOut], animations: { self.myView.alpha = 1 } , completion: self.fadeOut)
}
func fadeOut(finished: Bool) {
UIView.animateWithDuration(self.duration, delay: 0, options: [.CurveEaseInOut], animations: { self.myView.alpha = 0 } , completion: self.fadeIn)
}
}

Swift 5
This worked well for me. I wanted to animate a continuous fade in and out of a label, which I placed inside the "cardHeaderView" UIView.
#IBOutlet weak var cardHeaderView: UIView!
Place this inside the "viewDidAppear". I went with a delay of zero so the animation would start right away.
fadeViewInThenOut(view: cardHeaderView, delay: 0)
Here is the function.
func fadeViewInThenOut(view : UIView, delay: TimeInterval) {
let animationDuration = 1.5
UIView.animate(withDuration: animationDuration, delay: delay, options: [UIView.AnimationOptions.autoreverse, UIView.AnimationOptions.repeat], animations: {
view.alpha = 0
}, completion: nil)
}

Swift 5
Fade in 2 seconds
pause 2 seconds,
fade out 2 seconds
Repeat
func startAnimation() {
UIView.animateKeyframes(withDuration: 6.0,
delay: 0,
options: [.repeat, .autoreverse, .calculationModeLinear]) {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.165) { [weak self] in
self?.view1.alpha = 0.0
}
UIView.addKeyframe(withRelativeStartTime: 0.165, relativeDuration: 0.165) { [weak self] in
self?.view2.alpha = 1.0
}
UIView.addKeyframe(withRelativeStartTime: 0.66, relativeDuration: 0.165) { [weak self] in
self?.view2.alpha = 0.0
}
ctartTime: 0.825, relativeDuration: 0.165) { [weak self] in
self?.view1.alpha = 1.0
}
}
}
Please note that when your animation is at Alpha == 0.0 the item is not interactable! You will have to add to .allowUserInteraction as an option

If you want repeatable fade animation you can do that by using CABasicAnimation like below :
###First create handy UIView extension :
extension UIView {
enum AnimationKeyPath: String {
case opacity = "opacity"
}
func flash(animation: AnimationKeyPath ,withDuration duration: TimeInterval = 0.5, repeatCount: Float = 5){
let flash = CABasicAnimation(keyPath: AnimationKeyPath.opacity.rawValue)
flash.duration = duration
flash.fromValue = 1 // alpha
flash.toValue = 0 // alpha
flash.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
flash.autoreverses = true
flash.repeatCount = repeatCount
layer.add(flash, forKey: nil)
}
}
###How to use it:
// You can use it with all kind of UIViews e.g. UIButton, UILabel, UIImage, UIImageView, ...
imageView.flash(animation: .opacity, withDuration: 1, repeatCount: 5)
titleLabel.flash(animation: .opacity, withDuration: 1, repeatCount: 5)

Related

Fading Text in with Duration in SwiftUI

I have been using the following UIView extension for quite a while to fade text in and out. I have been trying to figure out how to implement this with SwiftUI but so far haven't found anything that exactly addresses this for me. Any help would be greatly appreciated.
extension UIView {
func fadeKey() {
// Move our fade out code from earlier
UIView.animate(withDuration: 3.0, delay: 2.0, options: UIView.AnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0 // Instead of a specific instance of, say, birdTypeLabel, we simply set [thisInstance] (ie, self)'s alpha
}, completion: nil)
}
func fadeIn1() {
// Move our fade out code from earlier
UIView.animate(withDuration: 1.5, delay: 0.5, options: UIView.AnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0 // Instead of a specific instance of, say, birdTypeLabel, we simply set [thisInstance] (ie, self)'s alpha
}, completion: nil)
}
I assume this is what you wanted. Try this below code:
struct FadeView: View {
#State var isClicked = false
#State var text = "Faded Text"
var body: some View {
VStack {
Text(text) //fade in and fade out
.opacity(isClicked ? 0 : 1)
Button("Click to animate") {
withAnimation {
isClicked.toggle()
}
}
}
}
}
You could use withAnimation function and manipulate the duration, options and delay
struct ContentView: View {
#State var isActive = false
var body: some View {
VStack {
Button("Fade") {
withAnimation(.easeIn(duration: 3.0).delay(2)){
isActive.toggle()
}
}
Rectangle()
.frame(width: 222.0, height: 222.0)
.opacity(isActive ? 0 : 1)
}
}
}

How to hide the tab bar with animation in iOS 13?

In my tabBarController, I use these two methods to hide/show the tabBar:
func showTabBar() {
tabBar.isHidden = false
UIView.animate(withDuration: 0.3) {
self.tabBar.transform = .identity
}
}
func hideTabBar() {
UIView.animate(withDuration: 0.3, animations: {
self.tabBar.transform = CGAffineTransform(translationX: 0, y: self.tabBar.frame.height)
}, completion: { _ in
self.tabBar.isHidden = true
})
}
This has worked well in iOS 12 and below, but in iOS 13, when the transforms are applied, the whole tabBar is broken (see picture). I know that you can hide the tabBar with frame animations, but applying transforms is much easier and I don't understand why this is broken and what changed in iOS 13 that prevents this from working.
'transform' property of tabbar is not working in iOS 13 for now, maybe it'll work later.
while hiding tabbar you just need to hold the last frame of your tabbar.
To show tabbar: -
func showTabbar(_ tabBarController: UITabBarController?, _ lastTabBarFrame: CGRect?) {
if #available(iOS 13, *) {
UIView.animate(withDuration: 0.3, animations: {
tabBarController?.tabBar.frame = lastTabBarFrame ?? .zero
}, completion: { (_) in
})
} else {
UIView.animate(withDuration: 0.3, animations: {
tabBarController?.tabBar.transform = .identity
}, completion: { (_) in
})
}
}
To hide tabbar: -
func hideTabbar(_ tabBarController: UITabBarController?, _ lastTabBarFrame: CGRect?, _ view: UIView) {
if #available(iOS 13, *) {
UIView.animate(withDuration: 0.3, animations: {
tabBarController?.tabBar.frame = CGRect(x: 0, y: view.bounds.height + (lastTabBarFrame?.height ?? 0.0) + 20, width: lastTabBarFrame?.width ?? 0.0, height: lastTabBarFrame?.height ?? 0.0)
}) { (_) in
}
} else {
UIView.animate(withDuration: 0.3, animations: {
tabBarController?.tabBar.transform = CGAffineTransform(translationX: 0, y: tabBarController?.tabBar.frame.height ?? 0.0)
}) { (_) in
}
}
}
How to use: -
var lastTabBarFrame: CGRect?
self.lastTabBarFrame = self.tabBarController?.tabBar.frame
hideTabbar(self.tabBarController, self.lastTabBarFrame, self.view)
showTabbar(self.tabBarController, self.lastTabBarFrame)

NSLayoutConstraint not animating

Everything animates fine from this code except for the mainViewConstraint. I am trying to make the mainView slide in from the top as it appears by transforming the mainViewConstraint coordinate from -195 to 0. Unfortunately, it is not moving from -195 to 0. It just starts to appear at 0.
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var bgImage: UIImageView!
#IBOutlet weak var mainView: UIView!
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var findButton: UIButton!
#IBOutlet weak var mainViewConstraint: NSLayoutConstraint!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
mainViewConstraint.constant = -195
for i in [mainView, titleLabel, findButton] {
i?.alpha = 0
}
UIView.animate(withDuration: 1, animations: {
}) { (true) in
self.animateView()
}
}
func animateView() {
UIView.animate(withDuration: 2, animations: {
self.mainView.alpha = 1
self.mainViewConstraint.constant = 0
self.view.layoutIfNeeded()
}) { (true) in
self.animateLbl()
}
}
func animateLbl() {
UIView.animate(withDuration: 1, animations: {
self.titleLabel.alpha = 1
}) { (true) in
self.animateBtn()
}
}
func animateBtn() {
UIView.animate(withDuration: 1) {
self.findButton.alpha = 1
}
}
}
In your code, you're calling animateView() inside an animation block, which seems to be the problem.
Moving it out of the animation block should fix the problem.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
mainViewConstraint.constant = -195
for i in [mainView, titleLabel, findButton] {
i?.alpha = 0
}
self.animateView()
}
To fix this, I changed the location of the mainViewConstraint to -195 in Storyboards and made the following changes to the code:
mainViewConstraint.constant = 0 //-195
for i in [mainView, titleLbl, findBtn] {
i?.alpha = 0
}
UIView.animate(withDuration: 1, animations: {
self.bgImage.alpha = 1
}) { (true) in
self.animateView()
}
}
func animateView() {
UIView.animate(withDuration: 2, animations: {
self.mainView.alpha = 1
//self.mainViewConstraint.constant = 0
self.view.layoutIfNeeded()

UIView Animations seem to speed up after first time, Xcode 6.1 swift

For some reason after my first call of a serious of animation blocks, the animation seems to be faster, not sure if this is a bug or something i've done wrong but i'm sure someone call tell me.. i've made a UIView subclass to handle this.
import UIKit
import QuartzCore
class GBPopupController: UIView {
var originalContainerCenterY = CGFloat()
#IBOutlet var continerConstraintCenterY: NSLayoutConstraint!
#IBOutlet var containerConstraintCenterX: NSLayoutConstraint!
var startingCenter = CGPoint()
#IBOutlet var contentView: UIView!
#IBOutlet var button: UIButton!
override func layoutSubviews() {
super.layoutSubviews()
contentView.layer.cornerRadius = 10
button.layer.cornerRadius = 10
}
override func didMoveToSuperview() {
self.beginViewAnimations()
}
func animatePopupIn() {
UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.7, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.contentView.layer.transform = CATransform3DIdentity
}, completion: {finished in
})
}
func beginViewAnimations() {
var transform = CATransform3DIdentity;
transform = CATransform3DMakeTranslation(0, -self.frame.size.height, 0)
self.contentView.layer.transform = transform
UIView.animateWithDuration(0.4, delay: 0.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
self.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
}, completion: {finished in
self.animatePopupIn()
})
}
func removeViewFromSuperView() {
UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
var transform = CATransform3DIdentity;
transform = CATransform3DMakeTranslation(0, -self.frame.size.height, 0)
self.contentView.layer.transform = transform
}, completion: {finished in
UIView.animateWithDuration(0.4, delay: 0.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
self.backgroundColor = UIColor.clearColor()
}, completion: {finished in
self.removeViewAnimation()
})
})
}
func removeViewAnimation() {
self.removeFromSuperview()
}
My comment is too long so I answer here. I don't know if it can help you but here's what I did.
I use autolayout and so in the viewdidload(), dimensions are not yet initialized. They are based on a 600x600 size screen (meaning any width, any height). Screen size is picked up once the first action is done (like pushing a button in my case).
This is why I call my animation for the first time at this first action, just to put my frame at the good place. Then the rest is the same. But it kind of initialize my view with the right frame (size and position). After that, I don't have this problem and my animation is also good.
It's like I was calling it twice the first time, one to initialize and one just to play the animation.
I made some research and I found that it could be a problem with the layer which is different from the frame and which is initialized with a different animation from the one you choose. That's why I didn't have the same animation the first time.
Hope it will help you.

How to create a Vertical progressbar in Cocoa?

I'm trying to create a vertical progress bar in my Cocoa app, i.e, the progress bar should grow from bottom to top. I'm using NSProgressIndicator, and I can't find a way to specify vertical or horizontal. Can anybody please tell me is it possible to do it?
Thanks,
Lee
You can set the transform of the control to rotate it pi/2 radians (90 degrees). That seems to be a common solution most people take.
import UIKit
class ViewController: UIViewController {
// THis is custom Progress view
var progessView:VerticalProgressView!
// We can also use default progress view given by UIKIT
var defaultProgressView:UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
// Custom Progress view
progessView = VerticalProgressView(frame: CGRect(x: 0, y: 160, width: 15, height: 200))
progessView.center.x = self.view.center.x - 80
self.view.addSubview(progessView)
//Default Progress view
defaultProgressView = UIProgressView(progressViewStyle: .bar)
self.view.addSubview(defaultProgressView)
}
override func viewDidLayoutSubviews() {
defaultProgressView.frame = CGRect(x: self.view.center.x + 30, y: 300, width: 100, height: 300)
defaultProgressView.progressTintColor = UIColor.green
defaultProgressView.backgroundColor = UIColor.clear
defaultProgressView.layer.borderWidth = 0.3
//defaultProgressView.layer.borderColor = [UIColor.redColor]
// Change the width of default Progress view
let customWidth = CGAffineTransform(scaleX: 5.0, y: 3.0)
// Transform from default horizontal to vertical
let rotate = CGAffineTransform(rotationAngle: (CGFloat.pi/2 + CGFloat.pi))
//Two transforms should be concated and applied
defaultProgressView.transform = rotate.concatenating(customWidth)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.progessView.setProgress(progress: 0.50, animated: true)
UIView.animate(withDuration: 0.95) {
self.defaultProgressView.setProgress(0.50, animated: true)
}
}
}

Resources