0

I'm writing some simple animation code to make a button get taller and then shorter using UIView animations. The code is a little long, but fairly simple:

func animateButton(aButton: UIButton, step: Int)
{
  let localStep = step - 1

  let localButton = aButton
  let halfHeight = aButton.bounds.height / 2

  var transform: CGAffineTransform
  switch step
  {
  case 2:
    //Make the center of the grow animation be the bottom center of the button
    transform = CGAffineTransformMakeTranslation(0, -halfHeight)

    //Animate the button to 120% of it's normal height.
    transform = CGAffineTransformScale( transform, 1.0, 1.2)
    transform = CGAffineTransformTranslate( transform, 0, halfHeight)
    UIView.animateWithDuration(0.5, animations:
      {
        aButton.transform = transform
      },
      completion:
      {
        (finshed) in
        //------------------------------------
        //--- This line throws the error ---
        animateButton(aButton, step: 1)
        //------------------------------------
    })
  case 1:
    //In the second step, shrink the height down to .25 of normal
    transform = CGAffineTransformMakeTranslation(0, -halfHeight)

    //Animate the button to 120% of it's normal height.
    transform = CGAffineTransformScale( transform, 1.0, 0.25)
    transform = CGAffineTransformTranslate( transform, 0, halfHeight)
    UIView.animateWithDuration(0.5, animations:
      {
        aButton.transform = transform
      },
      completion:
      {
        (finshed) in
        animateButton(aButton, step: 0)
    })
  case 0:
    //in the final step, animate the button back to full height.
    UIView.animateWithDuration(0.5)
      {
        aButton.transform = CGAffineTransformIdentity
    }
  default:
    break
  }
}

The completion blocks for the animation methods are closures. I'm getting an error "Call to method animateButton in closure requires explicit self. to make capture semantics explicit.

The thing is, the parameter aButton is a parameter to the enclosing function. There is no reference to an instance variable.

It looks to me like this is compiler bug. Am I missing something here?

Duncan C
  • 128,072
  • 22
  • 173
  • 272

1 Answers1

1

Calling methods in the same class are called with an implied self. In this case because of the closure you have to make it explicit:

self.animateButton(aButton, step: 1)
vacawama
  • 150,663
  • 30
  • 266
  • 294