View does not animate - swift

I have a view, which shall move up like a drawer from the bottom of the screen. But it does not do anything. It just sits there =)
Can anyone please tell me, why it is doing that?
This is my code:
import UIKit
class InfoPopUpVC: UIViewController {
var superView: UIView!
var labelText: String!
let textLabel = UILabel()
let height = CGFloat(80)
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.view.center.y = 50
})
}
override func viewDidLoad() {
super.viewDidLoad()
setupTextLabel()
view.frame = CGRectMake(0, superView.frame.maxY-height, superView.frame.width, height)
AnimationHelper.blurBackgroundForView(view)
view.backgroundColor = .greenColor()
}
func setupTextLabel(){
textLabel.text = labelText
textLabel.frame = CGRectMake(0, 0, view.frame.width, view.frame.height)
textLabel.numberOfLines = 3
textLabel.textAlignment = .Center
textLabel.frame.inset(dx: 10, dy: 8)
textLabel.sizeToFit()
textLabel.font = UIFont(name: "HelveticaNeue-Light", size: 17)
textLabel.textColor = .whiteColor()
view.addSubview(textLabel)
}
}

Try to put your code as follow inside viewDidAppear or viewWillAppear and with dispatch async. Otherwise your animation might not work.
override func viewDidAppear(animated: Bool) {
dispatch_async(dispatch_get_main_queue(), {
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.view.center.y = 50
})
})
}

You cannot animate a frame or similar property, if the UIViewis constrained using autolayout.
You have two options:
Get rid of autolayout and animate the frames Directory
Use autolayout and animate the constraints (e.g. via outlets)
See the following links for examples:
How do I animate constraint-changes
IOS: ANIMATING AUTOLAYOUT CONSTRAINTS

Related

Swift Xib UiView BottomSheet being called multiple times

so i make this bottomsheet view with xib and theres nothing wrong with my code, its just i only want to show it once, i mean like everytime i click the button its get triggered. which is fine but if i rapidly click the button it will also load bunch of time according on how many times i click. i only want to show once i mean no matter how much you rapidly click it only gonna show the xib view once, until i dismiss the button on the xib and it will do the same thing.
here's some video to make it more clearly
https://drive.google.com/file/d/12pwGdTiP_1QZlYc8tV-BlIIQQ5yYrfto/view?usp=sharing
i put a gif on that gdrive link
for the code
Xib Controller :
OrderActionSheetView: UIViewController {
#IBOutlet weak var Text: UILabel!
#IBOutlet weak var vieww: UIView!
#IBOutlet weak var botView: UIView!
#IBAction func cobaLagiBTn(_ sender: Any) {
let closeView = screenSize.height
UIView.animate(withDuration: 0.3, animations: {
self.view.alpha = 0.0
self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
let frame = self.view.frame
self.view.frame = CGRect(x: 0, y: closeView, width: frame.width, height: frame.height)
})
}
let fullView: CGFloat = 0
let screenSize: CGRect = UIScreen.main.bounds
override func viewDidLoad() {
super.viewDidLoad()
vieww.layer.cornerRadius = 20
vieww.layer.borderWidth = 0.5
vieww.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor
vieww.clipsToBounds = true
botView.layer.masksToBounds = false
botView.layer.shadowColor = UIColor.black.cgColor
botView.layer.shadowOpacity = 0.14
botView.layer.shadowOffset = CGSize(width: 0, height: 0)
botView.layer.shadowRadius = 2.7
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 0.3) { [weak self] in
let frame = self?.view.frame
let yComponent = self?.fullView
self?.view.frame = CGRect(x: 0, y: yComponent!, width: frame!.width, height: frame!.height)
}
}
func prepareBackgroundView(){
let blurEffect = UIBlurEffect.init(style: .light)
let visualEffect = UIVisualEffectView.init(effect: blurEffect)
let bluredView = UIVisualEffectView.init(effect: blurEffect)
bluredView.contentView.addSubview(visualEffect)
visualEffect.frame = UIScreen.main.bounds
bluredView.frame = UIScreen.main.bounds
view.insertSubview(bluredView, at: 0)
}
func Show(){
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0
UIView.animate(withDuration: 0.25, animations: {
self.view.alpha = 1.0
self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
})
}
}
View Controller :
class BottomSheetViewController: UIViewController {
#IBAction func Button(_ sender: Any) {
let text = "Connection Failed"
addBottomSheetView(text: text)
}
override func viewDidLoad() {
super.viewDidLoad()
}
func addBottomSheetView(text : String) {
// 1- Init bottomSheetVC
let bottomSheetVC = OrderActionSheetView()
// 2- Add bottomSheetVC as a child view
self.addChild(bottomSheetVC)
self.view.addSubview(bottomSheetVC.view)
bottomSheetVC.didMove(toParent: self)
// 3- Adjust bottomSheet frame and initial position.
let height = view.frame.height
let width = view.frame.width
bottomSheetVC.view.frame = CGRect(x: 0, y: self.view.frame.maxY, width: width, height: height)
bottomSheetVC.Text.text = text
}
}
i just need to know how to stop popping up twice, cause its kinda really some big bugs.....
like i said earlier i just want the xib view to shown only once no matter how many times you rapidly click the button.
Thanks guys :)
As here you add a new instance every click
func addBottomSheetView(text : String) {
// 1- Init bottomSheetVC
let bottomSheetVC = OrderActionSheetView()
// 2- Add bottomSheetVC as a child view
self.addChild(bottomSheetVC)
self.view.addSubview(bottomSheetVC.view)
bottomSheetVC.didMove(toParent: self)
So either
1-You need to add a bool variable like
var isShown = false
and in beginning of method add this code
guard !isShown else { return }
isShown = true
and when you remove the view make
isShown = false
Or
2- create an instance variable like
var bottomSheetVC:OrderActionSheetView?
and in beginning of method
guard bottomSheetVC == nil else { return }
bottomSheetVC = OrderActionSheetView()
and when you remove it do
bottomSheetVC.view.removeFromSuperview()
bottomSheetVC = nil

UITextField underline width issue

I have a UITextField & UIButton placed inside a vertical UIStackView. Programatically, I've added a UIView to serve as an underline of the UITextField.
The problem comes when I compile the code. The line is longer than the text field. I've printed the width of all of the objects above and it says 335.
I call this function in viewWillAppear and the line is longer. If I call it in viewDidAppear, the line is the exact width of the UITextField but you can see the button and text field flash very briefly before updating the view. What am I doing wrong?
override func viewDidLoad() {
super.viewDidLoad()
observeKeyboardNotifications()
textFieldDelegate()
setupRegisterButton()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setupTextFields()
}
func setupTextFields() {
emailTextField.leftViewMode = .always
let emailIconView = UIImageView()
emailIconView.frame = CGRect(x: 0, y: 0, width: 23, height: 17)
emailIconView.image = UIImage(named: "icon_email")
let bgIconView = UIView(frame: CGRect(x: 0, y: 0, width: 28, height: 17))
bgIconView.addSubview(emailIconView)
emailTextField.leftView = bgIconView
emailTextField.tintColor = .white
emailTextField.textColor = .white
emailTextField.backgroundColor = .red
emailTextField.borderStyle = .roundedRect
let bottomLineEmail = CALayer()
bottomLineEmail.frame = CGRect(x: 0, y: 29, width: emailTextField.frame.width, height: 1)
print("Email width", emailTextField.frame.width)
print("button width", retrievePasswordButton.frame.width)
print("line width", bottomLineEmail.frame.width)
bottomLineEmail.backgroundColor = UIColor.white.cgColor
emailTextField.layer.addSublayer(bottomLineEmail)
//emailTextField.backgroundColor = .clear
emailTextField.attributedPlaceholder = NSAttributedString(string : emailTextField.placeholder!,
attributes : [NSAttributedStringKey.foregroundColor: Colors.authTextFieldPlaceholderColor])
retrievePasswordButton.isEnabled = false
handleTextFields()
}
I checked it personally
it works in ViewWillLayoutSubviews
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews();
self.setupTextFields()
}
and viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.setupTextFields();
}

How do I place my activityIndicator programmatically?

I want to place my activityIndicator where I want it programmatically, but I don´t know how.
I know how to put it in the center of the screen:
activityIndicator.center = self.view.center
But I want something like this:
activityIndicator.activityIndicator(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
But I can´t seem to make it work.
Basically, you can do this in just a few lines of code:
func showActivityIndicatory() {
let activityView = UIActivityIndicatorView(style: .whiteLarge)
activityView.center = self.view.center
self.view.addSubview(activityView)
activityView.startAnimating()
}
If you need more controll on activityView please set Origin of container view to place activityindicator anywhere on the screen.
func showActivityIndicatory() {
let container: UIView = UIView()
container.frame = CGRect(x: 0, y: 0, width: 80, height: 80) // Set X and Y whatever you want
container.backgroundColor = .clear
let activityView = UIActivityIndicatorView(style: .whiteLarge)
activityView.center = self.view.center
container.addSubview(activityView)
self.view.addSubview(container)
activityView.startAnimating()
}
You can declare:
var activityView: UIActivityIndicatorView?
And, in your class, create the next methods for showing or hiding the indicator:
func showActivityIndicator() {
activityView = UIActivityIndicatorView(style: .large)
activityView?.center = self.view.center
self.view.addSubview(activityView!)
activityView?.startAnimating()
}
func hideActivityIndicator(){
if (activityView != nil){
activityView?.stopAnimating()
}
}
This code works for me. Good luck!
Swift 5:
let activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
// Place the activity indicator on the center of your current screen
myActivityIndicator.center = view.center
// In most cases this will be set to true, so the indicator hides when it stops spinning
myActivityIndicator.hidesWhenStopped = true
// Start the activity indicator and place it onto your view
myActivityIndicator.startAnimating()
view.addSubview(myActivityIndicator)
// Do something here, for example fetch the data from API
// Finally after the job above is done, stop the activity indicator
myActivityIndicator.stopAnimating()
Swift 4, Swift 5
Here if you prefer my daily/favorite programmatic pattern
// MARK: - Component
lazy var indicatorView: UIActivityIndicatorView = {
let view = UIActivityIndicatorView(style: .medium)
view.color = .white
view.startAnimating()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
// MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
setupLayouts()
}
func setupViews() {
addSubview(indicatorView)
}
func setupLayouts() {
NSLayoutConstraint.activate([
indicatorView.centerXAnchor.constraint(equalTo: centerXAnchor),
indicatorView.centerYAnchor.constraint(equalTo: centerYAnchor)
])
}

UISwipeGestureRecognizer not recognized when added to UIView's subviews

I currently have a subclass of UIView which contains numerous subviews. I wish to add a UISwipeGesture to the subviews but unfortunately the swipe gesture is not recognized. I've set userInteractionEnabled = true and direction of the swipe gesture but nothing works.
public class CardStackView: UIView{
public var dataSource = [UIImage]()
private var swipeGuesture: UISwipeGestureRecognizer!
override public func layoutSubviews() {
for img in dataSource{
let view = AppView(image: img, frame: self.frame)
self.addSubview(view)
}
animateSubview()
self.userInteractionEnabled = true
}
func animateSubview(){
for (index, sView) in self.subviews.enumerate() {
swipeGuesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeGuestureDidSwipeRight(_:)))
swipeGuesture.direction = .Right
sView.addGestureRecognizer(swipeGuesture)
sView.userInteractionEnabled = true
let move: CGFloat = CGFloat(-20 + index * 20)
let opacity = Float(1 - 0.2 * CGFloat(index))
sView.shadowOpacity(opacity).shadowOffset(CGSizeMake(20 - CGFloat(index) * 5, 20 - CGFloat(index) * 5)).shadowRadius(5).moveX(-move).moveY(-move).gravity().shadowColor(UIColor.grayColor()).duration(1)
.completion({
}).animate()
}
}
func swipeGuestureDidSwipeRight(gesture: UISwipeGestureRecognizer) {
print("Swiped right")
let subview = self.subviews[0]
subview.moveX(-60).duration(1).animate()
}
}
Example
class ExampleController: UIViewController {
var stackView: CardStackView!
override func viewDidLoad() {
super.viewDidLoad()
stackView = CardStackView(frame: CGRect(x: 20, y: 80, width: 200, height: 200))
stackView.dataSource = [UIImage(named: "2008")!, UIImage(named: "2008")!]
self.view.addSubview(stackView)
}
}
self.view.bringSubviewToFront(yourSubview)
try this code for all your subviews and if it doesn't work try this in your controller class for your CardStackView.
Try to call setNeedsLayout for stackView:
override func viewDidLoad() {
super.viewDidLoad()
stackView = CardStackView(frame: CGRect(x: 20, y: 80, width: 200, height: 200))
stackView.dataSource = [UIImage(named: "2008")!, UIImage(named: "2008")!]
stackView.setNeedsLayout()
self.view.addSubview(stackView)
}

How to move a label only once (swift)?

I created a label programmatically, and initially the label don't appear , only when button "appear" is touched (Ok). I want to know how to move the label only once. If the label was moved for the first time, when I touch the button again, nothing must happen.
import UIKit
class ViewController: UIViewController {
var label = UILabel()
var screenWidth: CGFloat = 0.0
var screenHeight: CGFloat = 0.0
override func viewDidLoad() {
super.viewDidLoad()
let screenSize: CGRect = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
label = UILabel(frame: CGRectMake(0, 64, screenWidth, 70))
label.textAlignment = NSTextAlignment.Center
label.backgroundColor = UIColor.blackColor()
label.text = "Label Appear"
label.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
label.textColor = UIColor.whiteColor()
view.addSubview(label)
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.label.center.y -= self.view.bounds.width
}
#IBAction func appear(sender: AnyObject) {
UIView.animateWithDuration(0.5, animations: {
self.label.center.y += self.view.bounds.width
}, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
One solution is to add a boolean that indicate if the label has already moved or hasn't.
For example :
var hasLabelAlreadyMoved = false
...
#IBAction func appear(sender: AnyObject) {
/*
We want to move the label if the bool is set to false ( that means it hasn't moved yet ),
else ( the label has already moved ) we exit the function
*/
guard !self.hasLabelAlreadyMoved else {
return
}
self.hasLabelAlreadyMoved = true
UIView.animateWithDuration(0.5, animations: {
self.label.center.y += self.view.bounds.width
}, completion: nil)
}