UIView is not hidden when condition is met in if else statement - swift

I am trying to create an animation and hide it 5 seconds after the task has been completed.
For some reason overlayView is not hidden off the screen when counter == 5.
var counter = 0
var timer = Timer()
let paymentLogo = UIImage(named: "paymentImage")
var imageLogo:UIImageView!
var overlayView = UIView()
var logoAppeared:Bool? = false
let labelLogo = UILabel()
override func viewDidLayoutSubviews() {
//move picture off screen
animateDidLayout()
}
override func viewDidAppear(_ animated: Bool) {
//move picture on screen and adjust view
animateDidAppear()
}
//called by timer every 1 seconds
func startCounting() {
if counter == 5 {
self.timer.invalidate()
self.overlayView.isHidden = true //it is not hidden
self.imageLogo.isHidden = true
self.labelLogo.isHidden = true
self.logoAppeared = true
print("counter in if \(counter)")
} else {
counter += 1
print("counter in else \(counter)")
}
}
//animate the image before it appears on screen
func animateDidLayout(){
guard let appeared = self.logoAppeared, appeared == false else{
print("appeared is true in viewDidLayoutSubviews, in else")
return
}
print("appeared value after else in ViewDidLayoutSubvies \(appeared)")
//animate overlayView
self.overlayView = UIView(frame: self.view.frame)
self.overlayView.backgroundColor = UIColor.black
self.overlayView.alpha = 0.4
self.view.addSubview(self.overlayView)
//animate imageLogo
self.imageLogo = UIImageView(image:paymentLogo)
imageLogo.frame = CGRect(x: 0, y: 0, width: 100,
height: 100)
imageLogo.center.x -= 400
self.view.addSubview(imageLogo)
//animate labelLogo
self.labelLogo.frame =
CGRect(x: 0, y: 0, width: 200, height: 21)
self.labelLogo.center.x -= 400
self.labelLogo.text = "Your booking is confirmed!"
self.labelLogo.textAlignment = .center
self.view.addSubview(labelLogo)
}//end of animateDidLayout
//animate the logo when the view has appeared
//call it in ViewDidAppear
func animateDidAppear() {
guard let appeared = self.logoAppeared, appeared == false else{
print("appeared is true in viewDidAppear, in else")
return
}
print("appeared value in viewDidAppear after else \(appeared)")
UIView.animate(withDuration: 1.0, delay: 0.1, options: [],
animations: {
//animate overlayView
// self.overlayView = UIView(frame: self.view.frame)
//self.overlayView.backgroundColor = UIColor.black
//self.overlayView.alpha = 0.4
//animate labelLogo
self.labelLogo.frame = CGRect(x: self.view.center.x - 100,
y: 340, width: 200, height: 21)
self.labelLogo.text = "Your booking is confirmed!"
self.labelLogo.textColor = .white
self.labelLogo.textAlignment = .center
self.labelLogo.sizeToFit()
//animate imageLogo
self.imageLogo.frame =
CGRect(x: self.view.center.x - 50,y: 250,width: 100,height: 90)
}) { finished in
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self,
selector: #selector(ThirteenthViewController.startCounting), userInfo: nil,
repeats: true)
}
} //end of animateDidAppear

I would either use a delay or use another animation with a delay to remove hide the views. I would not rely on the timer. Example with animation to remove using your code and removing the timer.
class ViewController: UIViewController {
var counter = 0
let paymentLogo = UIImage(named: "paymentImage")
var imageLogo:UIImageView!
var overlayView = UIView()
var logoAppeared:Bool? = false
let labelLogo = UILabel()
override func viewDidLayoutSubviews() {
//move picture off screen
animateDidLayout()
}
override func viewDidAppear(_ animated: Bool) {
//move picture on screen and adjust view
animateDidAppear()
}
//animate the image before it appears on screen
func animateDidLayout(){
guard let appeared = self.logoAppeared, appeared == false else{
print("appeared is true in viewDidLayoutSubviews, in else")
return
}
print("appeared value after else in ViewDidLayoutSubvies \(appeared)")
//animate overlayView
if self.view.subviews.contains(overlayView) != true{
self.overlayView = UIView(frame: self.view.frame)
self.view.addSubview(self.overlayView)
}
self.overlayView.backgroundColor = UIColor.black
self.overlayView.layer.opacity = 0.4
self.overlayView.alpha = 0.4
//animate imageLogo
if self.view.subviews.contains(imageLogo) != true{
self.imageLogo = UIImageView(image:paymentLogo)
self.view.addSubview(imageLogo)
}
imageLogo.frame = CGRect(x: 0, y: 0, width: 100,
height: 100)
imageLogo.center.x -= 400
//animate labelLogo
self.labelLogo.frame =
CGRect(x: 0, y: 0, width: 200, height: 21)
self.labelLogo.center.x -= 400
self.labelLogo.text = "Your booking is confirmed!"
self.labelLogo.textAlignment = .center
if self.view.subviews.contains(labelLogo) != true{
self.view.addSubview(labelLogo)
}
}//end of animateDidLayout
//animate the logo when the view has appeared
//call it in ViewDidAppear
func animateDidAppear() {
guard let appeared = self.logoAppeared, appeared == false else{
print("appeared is true in viewDidAppear, in else")
return
}
print("appeared value in viewDidAppear after else \(appeared)")
UIView.animate(withDuration: 1.0, delay: 0.1, options: [],
animations: {
//animate overlayView
// self.overlayView = UIView(frame: self.view.frame)
//self.overlayView.backgroundColor = UIColor.black
//self.overlayView.alpha = 0.4
//animate labelLogo
self.labelLogo.frame = CGRect(x: self.view.center.x - 100,
y: 340, width: 200, height: 21)
self.labelLogo.text = "Your booking is confirmed!"
self.labelLogo.textColor = .white
self.labelLogo.textAlignment = .center
self.labelLogo.sizeToFit()
//animate imageLogo
self.imageLogo.frame =
CGRect(x: self.view.center.x - 50,y: 250,width: 100,height: 90)
}) { finished in
}
UIView.animate(withDuration: 0.5, delay: 5.0, options: .curveEaseInOut, animations: {
self.overlayView.alpha = 0
self.imageLogo.alpha = 0
self.labelLogo.alpha = 0
}, completion: {
finished in
self.overlayView.isHidden = true //it is hidden :)
self.imageLogo.isHidden = true
self.labelLogo.isHidden = true
self.logoAppeared = true
})
} //end of animateDidAppear}
If you prefer to wait 5 seconds and hide without animation you could use
let when = DispatchTime.now() + 5
DispatchQueue.main.asyncAfter(deadline: when) {// Your code with delay}
Or as a really fun alternative using your code and the delay
class ViewController: UIViewController {
var counter = 0
let paymentLogo = UIImage(named: "paymentImage")
var imageLogo:UIImageView!
var overlayView = UIView()
var logoAppeared:Bool? = false
let labelLogo = UILabel()
override func viewDidLayoutSubviews() {
//move picture off screen
animateDidLayout()
}
override func viewDidAppear(_ animated: Bool) {
//move picture on screen and adjust view
animateDidAppear()
}
//animate the image before it appears on screen
func animateDidLayout(){
guard let appeared = self.logoAppeared, appeared == false else{
print("appeared is true in viewDidLayoutSubviews, in else")
return
}
print("appeared value after else in ViewDidLayoutSubvies \(appeared)")
//animate overlayView
if self.view.subviews.contains(overlayView) != true{
self.overlayView = UIView(frame: self.view.frame)
self.view.addSubview(self.overlayView)
}
self.overlayView.backgroundColor = UIColor.black
self.overlayView.layer.opacity = 0.4
self.overlayView.alpha = 0.4
//animate imageLogo
if self.view.subviews.contains(imageLogo) != true{
self.imageLogo = UIImageView(image:paymentLogo)
self.view.addSubview(imageLogo)
}
imageLogo.frame = CGRect(x: 0, y: 0, width: 100,
height: 100)
imageLogo.center.x -= 400
//animate labelLogo
self.labelLogo.frame =
CGRect(x: 0, y: 0, width: 200, height: 21)
self.labelLogo.center.x -= 400
self.labelLogo.text = "Your booking is confirmed!"
self.labelLogo.textAlignment = .center
if self.view.subviews.contains(labelLogo) != true{
self.view.addSubview(labelLogo)
}
}//end of animateDidLayout
//animate the logo when the view has appeared
//call it in ViewDidAppear
func animateDidAppear() {
guard let appeared = self.logoAppeared, appeared == false else{
print("appeared is true in viewDidAppear, in else")
return
}
print("appeared value in viewDidAppear after else \(appeared)")
UIView.animate(withDuration: 1.0, delay: 0.1, options: [],
animations: {
//animate overlayView
// self.overlayView = UIView(frame: self.view.frame)
//self.overlayView.backgroundColor = UIColor.black
//self.overlayView.alpha = 0.4
//animate labelLogo
self.labelLogo.frame = CGRect(x: self.view.center.x - 100,
y: 340, width: 200, height: 21)
self.labelLogo.text = "Your booking is confirmed!"
self.labelLogo.textColor = .white
self.labelLogo.textAlignment = .center
self.labelLogo.sizeToFit()
//animate imageLogo
self.imageLogo.frame =
CGRect(x: self.view.center.x - 50,y: 250,width: 100,height: 90)
}) { finished in
}
let when = DispatchTime.now() + 5
DispatchQueue.main.asyncAfter(deadline: when) {
let transition = CATransition()
transition.duration = 0.5
transition.type = "suckEffect"
self.view.layer.add(transition, forKey: nil)
self.overlayView.isHidden = true //it is hidden :)
self.imageLogo.isHidden = true
self.labelLogo.isHidden = true
self.logoAppeared = true
}
} //end of animateDidAppear}

Related

CustomAlert Message overWrite

my model :
struct Model : Codable {
let title : String
var target : Int
var read : Int
let mean : String
let useful : String
}
and I create custom alert messages model:
class MyAlert {
struct Constants {
static let backgroundAlphaTo : CGFloat = 0.6
}
private var backgroundView : UIView = {
let backgroundView = UIView()
backgroundView.backgroundColor = .black
backgroundView.alpha = 0
return backgroundView
}()
private let alertView : UIView = {
let alertView = UIView()
alertView.backgroundColor = .white
alertView.layer.masksToBounds = true
alertView.layer.cornerRadius = 12
return alertView
}()
private var myTargetView : UIView?
func showAlert(with title :String , message : String , on ViewController : UIViewController){
guard let targetView = ViewController.view else {
return
}
myTargetView = targetView
backgroundView.frame = targetView.bounds
targetView.addSubview(backgroundView)
targetView.addSubview(alertView)
alertView.frame = CGRect(
x: 40, y: -300, width: targetView.frame.size.width-80, height: 300
)
let titleLabel = UILabel(frame: CGRect(
x: 0,
y: 0,
width: alertView.frame.size.width,
height: 80))
titleLabel.text = title
titleLabel.textAlignment = .center
alertView.addSubview(titleLabel)
let messageLabel = UILabel(frame: CGRect(
x: 0,
y: 80,
width: alertView.frame.size.width,
height: 170))
messageLabel.numberOfLines = 0
messageLabel.text = message
messageLabel.textAlignment = .center
alertView.addSubview(messageLabel)
let button = UIButton(frame: CGRect(
x: 0,
y: alertView.frame.size.height-50,
width: alertView.frame.size.width,
height: 50))
alertView.addSubview(button)
button.setTitle("Kapat", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(dissmissAlert), for: .touchUpInside)
UIView.animate(withDuration: 0.25) {
self.backgroundView.alpha = Constants.backgroundAlphaTo
} completion: { (done) in
if done {
UIView.animate(withDuration: 0.25) {
self.alertView.center = targetView.center
}
}
}
}
#objc func dissmissAlert() {
guard let targetView = myTargetView else {
return
}
UIView.animate(withDuration: 0.25, animations: {
self.alertView.frame = CGRect(
x: 40, y: targetView.frame.size.height, width: targetView.frame.size.width-80, height: 300
)}, completion: {done in
if done {
UIView.animate(withDuration: 0.25, animations: {
self.backgroundView.alpha = 0
}, completion: {done in
if done {
self.alertView.removeFromSuperview()
self.backgroundView.removeFromSuperview()
}
})
}
}
)
}
}
and I have segmentController :
#objc func ButtonTapped( _ sender : UISegmentedControl) {
if sender.selectedSegmentIndex == 1 {
customAlert.showAlert(with: zikirs.title, message: zikirs.mean, on: self)
} else if sender.selectedSegmentIndex == 2 {
customAlert.showAlert(with: zikirs.title, message: zikirs.useful, on: self)
}
}
private func dismissAlert(){
customAlert.dissmissAlert()
}
the problem here is the first message is normal but the second message overwrites the other.
how can I overcome this problem. I think this is from the inheritance property of the Classes. but I wanted to do my CustomAlert model with Struct because #objc can be only classes
The problem is that you don't remove the labels and the button from alertView but add new ones on the next call.
My suggestion is to assign tags to the views and create them only the first time
class MyAlert {
struct Constants {
static let backgroundAlphaTo : CGFloat = 0.6
}
private var backgroundView : UIView = {
let backgroundView = UIView()
backgroundView.backgroundColor = .black
backgroundView.alpha = 0
return backgroundView
}()
private let alertView : UIView = {
let alertView = UIView()
alertView.backgroundColor = .white
alertView.layer.masksToBounds = true
alertView.layer.cornerRadius = 12
return alertView
}()
private var myTargetView : UIView?
func showAlert(with title :String , message : String , on ViewController : UIViewController){
guard let targetView = ViewController.view else {
return
}
myTargetView = targetView
backgroundView.frame = targetView.bounds
targetView.addSubview(backgroundView)
targetView.addSubview(alertView)
alertView.frame = CGRect(
x: 40, y: -300, width: targetView.frame.size.width-80, height: 300
)
if let titleLabel = alertView.viewWithTag(100) as? UILabel {
titleLabel.text = title
} else {
let titleLabel = UILabel(frame: CGRect(
x: 0,
y: 0,
width: alertView.frame.size.width,
height: 80))
titleLabel.tag = 100
titleLabel.text = title
titleLabel.textAlignment = .center
alertView.addSubview(titleLabel)
}
if let messageLabel = alertView.viewWithTag(101) as? UILabel {
messageLabel.text = message
} else {
let messageLabel = UILabel(frame: CGRect(
x: 0,
y: 80,
width: alertView.frame.size.width,
height: 170))
messageLabel.tag = 101
messageLabel.numberOfLines = 0
messageLabel.text = message
messageLabel.textAlignment = .center
alertView.addSubview(messageLabel)
}
if alertView.viewWithTag(102) == nil {
let button = UIButton(frame: CGRect(
x: 0,
y: alertView.frame.size.height-50,
width: alertView.frame.size.width,
height: 50))
button.tag = 102
alertView.addSubview(button)
button.setTitle("Kapat", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(dissmissAlert), for: .touchUpInside)
}
UIView.animate(withDuration: 0.25) {
self.backgroundView.alpha = Constants.backgroundAlphaTo
} completion: { (done) in
if done {
UIView.animate(withDuration: 0.25) {
self.alertView.center = targetView.center
}
}
}
}
#objc func dissmissAlert() {
guard let targetView = myTargetView else {
return
}
UIView.animate(withDuration: 0.25, animations: {
self.alertView.frame = CGRect(
x: 40, y: targetView.frame.size.height, width: targetView.frame.size.width-80, height: 300
)}, completion: {done in
if done {
UIView.animate(withDuration: 0.25, animations: {
self.backgroundView.alpha = 0
}, completion: {done in
if done {
self.alertView.removeFromSuperview()
self.backgroundView.removeFromSuperview()
}
})
}
}
)
}
}

I want to make an if statement that each image would be full screen when the user is tap

here is the code that I check if they images are tapped but only the 3 image is apply full screen. As you can see I set the image is tapped to statusImageView to call the function of zoomImage So I want fixed for all images.
func imageTapped() {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageZoom(tapGestureRecognizer:)))
if detailsImage[0].tag == 0 {
detailsImage[0].isUserInteractionEnabled = true
detailsImage[0].addGestureRecognizer(tapGestureRecognizer)
self.statusImageView = detailsImage[0]
}
let tapGestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(imageZoom(tapGestureRecognizer:)))
detailsImage[1].isUserInteractionEnabled = true
detailsImage[1].addGestureRecognizer(tapGestureRecognizer1)
self.statusImageView = detailsImage[1]
let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(imageZoom(tapGestureRecognizer:)))
detailsImage[2].isUserInteractionEnabled = true
detailsImage[2].addGestureRecognizer(tapGestureRecognizer2)
self.statusImageView = detailsImage[2]
}
let blackBackgroundColor = UIView()
let tappedImage = UIImageView()
var statusImageView: UIImageView?
let navigationBarView = UIView()
#objc func imageZoom(tapGestureRecognizer: UITapGestureRecognizer) {
annimationImage(detailsImage: statusImageView!)
}
And then I call the annihilationImage function which is takes the statusImageView as input
func annimationImage(detailsImage: UIImageView) {
if let startingFrame = statusImageView?.superview?.convert(statusImageView!.frame, to: nil) {
statusImageView!.alpha = 0
blackBackgroundColor.frame = self.view.frame
blackBackgroundColor.backgroundColor = UIColor.black
blackBackgroundColor.alpha = 0
view.addSubview(blackBackgroundColor)
navigationBarView.frame = CGRect(x: 0, y: 0, width: 1000, height: 100)
navigationBarView.backgroundColor = UIColor.black
navigationBarView.alpha = 0
if let keyWindow = UIApplication.shared.keyWindow {
keyWindow.addSubview(navigationBarView)
}
let tappedImage = UIImageView()
tappedImage.backgroundColor = .gray
tappedImage.frame = startingFrame
tappedImage.contentMode = .scaleToFill
tappedImage.isUserInteractionEnabled = true
tappedImage.image = statusImageView?.image
tappedImage.clipsToBounds = true
view.addSubview(tappedImage)
UIView.animate(withDuration: 0.75, animations: {
tappedImage.frame = CGRect(x: 0, y: y, width: self.view.frame.size.width, height: 300)
})
}
}
I want to make an if statement that while check which image is tapped
You can use tapGestureRecognizer.view for getting a view that is assigned a tap gesture. And by this, you can also get the view tag (your view is corresponding to the image view in your code)
#objc func imageZoom(tapGestureRecognizer: UITapGestureRecognizer) {
let senderView = tapGestureRecognizer.view // Here you get a view that is associated with the gesture
let tag = senderView?.tag // Here you get a tag that is assigned to all image
// Add your condition here by tag
annimationImage(detailsImage: statusImageView!)
}

swipe between UIView with segemented control programmatically

hi fellow developer I want to swipe between uiview with scrollView while changing the the index and small indicator move left and right. but is not responding while I try to swipe. this is my picture and my setup to scroll between uiview. can you tell me where I am missing?
let segmentedControl = UISegmentedControl()
let containerView = UIView()
let indicatorView = UIView()
let leftView = UIView()
let rightView = UIView()
let scrollView = UIScrollView()
var slides = [UIView]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
slides.append(leftView)
slides.append(rightView)
segmentedControl.insertSegment(withTitle: "Harian", at: 0, animated: true)
segmentedControl.insertSegment(withTitle: "Mata Pelajaran", at: 1, animated: true)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.addTarget(self, action: #selector(handleTap), for: .valueChanged)
setupSlideScrollView()
}
func setupSlideScrollView() {
view.addSubview(scrollView)
scrollView.isPagingEnabled = true
scrollView.translatesAutoresizingMaskIntoConstraints = false
for i in 0..<slides.count {
slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(slides[i])
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x / view.frame.width)
segmentedControl.selectedSegmentIndex = Int(pageIndex)
}
#objc func handleTap(_ sender: UISegmentedControl) {
indicatorView.frame.origin.x = (segmentedControl.frame.width / CGFloat(segmentedControl.numberOfSegments)) * CGFloat(segmentedControl.selectedSegmentIndex)
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
self.indicatorView.transform = CGAffineTransform(scaleX: 0.7, y: 1)
}) { (finish) in
UIView.animate(withDuration: 0.4, animations: {
self.indicatorView.transform = CGAffineTransform.identity
})
}
}

Subview Not Removing after Several "Remove View" Methods

I have this nice function that adds an activity indicator view, with a regular view behind it. When I add it, it adds fine. The problem is, when I try to remove it, nothing happens. I've tried:
.removeFromSuperview,
.isHidden = true and putting these methods in the main queue:
DispatchQueue.main.async() {
alertView.alpha = 0
alertView.removeFromSuperview()
activityIndicator.removeFromSuperview()
alertView.isHidden = true
activityIndicator.isHidden = true
}
I don't know what other methods to try...It seems all the other questions like this have one of the methods that I have as a solution. The function uses a boolean to determine whether or not to stop the activityIndicator. Here is my code:
static func showLoadingView(inViewController: UIViewController, turning: Bool){
let activityIndicator = UIActivityIndicatorView()
let alertView = UIView(frame: CGRect(x: activityIndicator.frame.origin.x, y: activityIndicator.frame.origin.y , width: 35, height: 35))
if(turning){
alertView.backgroundColor = UIColor(displayP3Red: 230, green: 230, blue: 230, alpha: 0.8)
alertView.layer.cornerRadius = 5
activityIndicator.center = alertView.center
inViewController.view.addSubview(alertView)
activityIndicator.activityIndicatorViewStyle = .gray
activityIndicator.startAnimating()
alertView.alpha = 0
activityIndicator.backgroundColor = UIColor.lightGray
alertView.center = inViewController.view.center
alertView.addSubview(activityIndicator)
alertView.transform = CGAffineTransform.init(scaleX: 1.3,y: 1.3)
UIView.animate(withDuration: 0.4) {
alertView.alpha = 1
alertView.transform = CGAffineTransform.identity
}
}
///////
else {
activityIndicator.stopAnimating()
alertView.transform = CGAffineTransform.init(scaleX: 1.3, y:1.3)
DispatchQueue.main.async() {
alertView.alpha = 0
alertView.removeFromSuperview()
activityIndicator.removeFromSuperview()
alertView.isHidden = true
activityIndicator.isHidden = true
}
print("Done")
}
}
You declared alertView which is out of scope when you called false.As a result, your call could not identify the alertView instance.
You can solve this issue in tow ways:
Declare your alertView outside of the function as a static.
static let activityIndicator = UIActivityIndicatorView()
static let alertView = UIView(frame: CGRect(x: activityIndicator.frame.origin.x, y: activityIndicator.frame.origin.y , width: 35, height: 35))
static func showLoadingView(inViewController: UIViewController, turning: Bool){}
In false case: find the subview using restorationIdentifier and remove.
static func showLoadingView(inViewController: UIViewController, turning: Bool){
if(turning){
let activityIndicator = UIActivityIndicatorView()
let alertView = UIView(frame: CGRect(x: activityIndicator.frame.origin.x, y: activityIndicator.frame.origin.y , width: 35, height: 35))
alertView.restorationIdentifier = "myalert"
alertView.backgroundColor = UIColor(displayP3Red: 230, green: 230, blue: 230, alpha: 0.8)
alertView.layer.cornerRadius = 5
activityIndicator.center = alertView.center
inViewController.view.addSubview(alertView)
activityIndicator.activityIndicatorViewStyle = .gray
activityIndicator.startAnimating()
alertView.alpha = 0
activityIndicator.backgroundColor = UIColor.lightGray
alertView.center = inViewController.view.center
alertView.addSubview(activityIndicator)
alertView.transform = CGAffineTransform.init(scaleX: 1.3,y: 1.3)
UIView.animate(withDuration: 0.4) {
alertView.alpha = 1
alertView.transform = CGAffineTransform.identity
}
}
///////
else {
for view in inViewController.view.subviews {
if (view.restorationIdentifier == "myalert") {
print("I FIND IT");
(view as! UIView).removeFromSuperview();
}
}
}
}

Cannot hide view after animation

I am creating an animation which brings an image and a label from the left to the centre of the view.
imageLogo.isHidden = true is not hidden when App is run
labelLogo is not shown at all on the view
I have been reading tutorials, but I just don't see what is wrong with my code.
let paymentLogo = UIImage(named: "paymentImage")
var imageLogo:UIImageView!
var overlayView = UIView()
var logoAppeared:Bool!
let labelLogo = UILabel()
override func viewDidLayoutSubviews() {
//move picture off the screen here
self.imageLogo = UIImageView(image:paymentLogo)
imageLogo.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
imageLogo.center.x -= 400
self.view.addSubview(imageLogo)
self.labelLogo.frame =
CGRect(x: 0, y: 0, width: 200, height: 21)
self.labelLogo.center.x -= 400
self.labelLogo.text = "Booking Completed"
self.labelLogo.textAlignment = .center
self.view.addSubview(labelLogo)
}
override func viewDidAppear(_ animated: Bool) {
UIView.animate(withDuration: 2.0, delay: 0.1, options: [], animations: {
//animate paymentCompletedLogo
self.overlayView = UIView(frame: self.view.frame)
self.overlayView.backgroundColor = UIColor.black
self.overlayView.alpha = 0.4
//animate labelLogo
self.labelLogo.frame = CGRect(x: self.view.center.x, y: 90, width: 200, height: 21)
self.labelLogo.backgroundColor = UIColor.gray
self.labelLogo.text = "Booking Completed"
self.labelLogo.textColor = .black
self.labelLogo.textAlignment = .center
//animate imageLogo
self.imageLogo.frame =
CGRect(x: self.view.center.x,y: self.view.center.y,width: 100,height: 100)
self.view.addSubview(self.overlayView)
self.view.addSubview(self.imageLogo)
self.view.addSubview(self.labelLogo)
}) { finished in
self.overlayView.isHidden = true
self.imageLogo.isHidden = true //it is not hidden in simulator
self.logoAppeared = true
}
}
I tried removing following your code from animation method it worked fine.
You are trying to add subview in animation method. imageLogo and labelLogo is all ready added in view.
self.view.addSubview(self.overlayView)
self.view.addSubview(self.imageLogo)
self.view.addSubview(self.labelLogo)