Vulkan
Vulkan

Reputation: 1076

How to constantly rotate a button by 45 degrees when another button is tapped?

I'm using this code:

[UIView animateWithDuration:ANIMATION_DURATION_SLOW
                      delay:0
     usingSpringWithDamping:0.4
      initialSpringVelocity:5.0
                    options:0
                 animations:^{

                     _button.transform = CGAffineTransformMakeRotation(-M_PI_4);
                 }
                 completion:nil];

But, the button only rotates the first time. No matter how many times i call this method it doesn't rotate again.

Upvotes: 1

Views: 266

Answers (2)

user4254887
user4254887

Reputation:

It's because you need to change the angle. consider:

-(CGFloat)angle{
    if (!_angle) {
        _angle = -M_PI_4;
    }

    _angle += -M_PI_4;

return _angle;
}

Upvotes: 1

Krys Jurgowski
Krys Jurgowski

Reputation: 2911

Setting a rotation transform doesn't add to the previous transform, it replaces it so you need to remember where it was during the last tap. A quick way to do this is a static variable.

static tapCount = 0;
tapCount++;

[UIView animateWithDuration:ANIMATION_DURATION_SLOW
                  delay:0
 usingSpringWithDamping:0.4
  initialSpringVelocity:5.0
                options:0
             animations:^{

                 _button.transform = CGAffineTransformMakeRotation(-M_PI_4 * tapCount);
             }
             completion:nil];

Upvotes: 3

Related Questions