iOS: Animate item twice

2019-09-15 13:40发布

Just starting out with core graphics. Playing with a simple button and label. I want this label to rotate 180 degrees on each click of the button. It only animates on the first click (the console does write "DONE" on each click, however)

- (IBAction)btnTest:(id)sender
{

    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{

        lblTest.layer.transform = CATransform3DMakeRotation(M_PI,0.0,1.0,0.0);

    }completion:^(BOOL finished) {
        if(finished)
            NSLog(@"DONE");
    }];
}

1条回答
Lonely孤独者°
2楼-- · 2019-09-15 14:12

The reason its not working is because on the first button press, the views rotation is set to 180 deg. On the second button press you set the rotation to 180 deg again but since that is the same as the current value of the rotation, nothing happens. What you really want to be doing is setting the rotation to 180 deg + the current rotation. You can achieve this by rotating the current transform by 180 deg with the following change.

lblTest.layer.transform = CATransform3DRotate(lblTest.layer.transform, M_PI,0.0,1.0,0.0);
查看更多
登录 后发表回答