0
votes

I am a beginner in swift. I am building a single view tip calculator app and i only want some animation to happen when the app loads for the first time. And not happen every time i edit something.

Here is my code: I am hiding the container outside the view when the app loads

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated) 

    billView.center.y -= (billView.frame.height)/2

}

And animating it to move down

override func viewDidLayoutSubviews() {
           super.viewDidLayoutSubviews()

    UIView.animateWithDuration(0.8, delay: 0.0,
        options: [.CurveEaseOut], animations: {
            self.billView.center.y = 210  }, completion: nil)
    print(billView.center.y)

}

This works fine when the app loads. Unfortunately this animation keeps happening again and again anytime I interact with the app.

Like this animation happens when this function is called (which is when any information is entered on the text field or the slider is moved)

let billAmount = NSString(string: billField.text!).doubleValue

    var tipPercentages = [0.15, 0.18, 0.22]

    let tipPercentage = tipPercentages[segTipControl.selectedSegmentIndex]

    let percentLabel = round(tipPercentage * 100)

    selectedbillPercentageLabel.text = "+\(percentLabel)%"

    let tipAmount = billAmount * tipPercentage

    let totalAmount = billAmount + tipAmount

    let roundedTotalAmount = round(totalAmount * 100)/100

    totalLabel.text = "$\(roundedTotalAmount)"

    let splitSelected = Double(splitValueSlider.value)

    let intsplitSelected = Int(splitSelected)

    splitLabel.text = "\(intsplitSelected)"

    let splitDollarAmount = roundedTotalAmount/splitSelected

    let roundedSplitDollarAmount = round(splitDollarAmount * 100)/100

    splitAmount.text = "$\(roundedSplitDollarAmount)"

Is there a way to avoid calling the method viewDidLayoutSubviews() when any data is entered in the app.

animated gif

1
You don't want to avoid calling viewDidLayoutSubviews when data is entered in your app. Instead, you should invoke your animation in the right place to serve your purpose, or you can use certain control flag to make it happen only once.Joe Huang

1 Answers

1
votes

Rather than avoiding calling viewDidLayourSubviews, you could add a simple boolean flip to show your animation only once. Add a boolean property that you initialize to false; show animation on condition of this property being false, and flip the boolean after the animation has been shown.

var animationHasBeenShown = false // class property

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if !animationHasBeenShown {
        UIView.animateWithDuration(0.8, delay: 0.0,
            options: [.CurveEaseOut], animations: {
                self.billView.center.y = 210  }, completion: nil)
        print(billView.center.y)

        animationHasBeenShown = true
    }
}

Alternatively, consider placing your animation in another context; does it really need to be in viewDidLayoutSubviews() (depends on the context of your app, but possibly rather in viewDidAppear()?)