-1

Can someone tell me what mistake I have done in following code?

 func animateView(view: UIView){
       view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
            self.view.transform = .identity
        }, completion: nil)
    }

enter image description here

I want animation like this. I read it on some blog but I didn't get how use it. Thanks in advance

  • Unclear what you expect. Setting a view's `transform` to `CGAffineTransform(scaleX: 1.0, y: 1.0)` has no effect, because that is its scale by default. And then animating it to `.identity` has no effect, because that is its transform by default. So I would not expect your code to have any visible effect. – matt Feb 27 '20 at 19:34
  • Why "unclear what you expect"? The OP showed a gif of the animation they expect to see. The thing that's unclear is whether this is a simple typo or a genuine misunderstanding of what `CGAffineTransform.init(scaleX:y:)` does. @matt – Sweeper Feb 27 '20 at 19:39
  • @Sweeper Yes, I see what you mean. I think I was responding more to the fact that the OP provided code but no statement of how the result differs from the desired effect. Also, for all I know, the OP also expects to see the little circle sliding along the time-vs-scale curve, and _that_ is certainly not going to happen just by changing the transform. – matt Feb 27 '20 at 19:49
  • @matt you r right it's not giving any effect – Muhammad Hasan Irshad Feb 27 '20 at 19:53
  • @matt my view has 400 height and width is equal to viewcontroler any solution? – Muhammad Hasan Irshad Feb 27 '20 at 19:59
  • @matt Thank you so much it start working. – Muhammad Hasan Irshad Feb 27 '20 at 20:04

1 Answers1

0

The mistake lies in the first line, where you set the transform to a scale of (1, 1):

view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)

CGAffineTransform(scaleX: 1.0, y: 1.0) means a scale with scale factor of 1, i.e. normal scale. This is equivalent to .identity, so you're askibf it to animate a transformation from the identity transform to the identity transform.

I'm guessing you probably thought (1, 1) meant 1 pixel by 1 pixel?

You should instead set the scale to (0, 0) initially:

view.transform = CGAffineTransform(scaleX: 0, y: 0)

Your second mistake is that you are animating self.view, not the parameter view. This is likely a typo.

so your whole method looks like:

func animateView(view: UIView){
   view.transform = CGAffineTransform(scaleX: 0, y: 0)
    UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
        view.transform = .identity
    }, completion: nil)
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313