iosfacebookfacebook-pop

How to always rotate half with facebook POP?


I'm new in iOS.

I want to always rotate half when user clicked a button.

Following is the code, it only rotate once and never rotate again when I clicked it.

@property UIButton *button;

- (void)viewDidLoad {
  [super viewDidLoad];
  CGFloat buttonSize = 220;
  self.button = [UIButton buttonWithType:UIButtonTypeCustom];
  self.button.backgroundColor = [UIColor colorFromHexString:@"#B084C7"];
  self.button.frame = CGRectMake(0, 0, buttonSize, buttonSize);
  [self.button addTarget:self
                action:@selector(didTap:)
      forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:self.button];
}

- (void)didTap:(id)sender {
  POPSpringAnimation *anim = [self.button.layer pop_animationForKey:@"rotate"];
  if (anim) {
    anim.toValue = @(M_PI * 0.5);
  } else {
    anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerRotation];
    anim.toValue = @(M_PI * 0.5);
    anim.springSpeed = 3;
    anim.springBounciness = 1;
  }
  [self.button.layer pop_addAnimation:anim forKey:@"rotate"];
}

Solution

  • You are always rotating to the same value @(M_PI * 0.5) this is an absolute rotation that is applied to the layer. If you want to increment the angle then you need to do something like this:

    anim.toValue = @(anim.fromValue.floatValue + (M_PI * 0.5));