IOS: question about animation
I have this code: successview is my view and it star开发者_如何学Ct with alpha 0.00 but when it finish the animation with autoreverse, successview become with alpha 1.00...why?
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationRepeatAutoreverses:YES];
[successView setAlpha:1.00];
[UIView commitAnimations];
One approach is to do two different animations: One that progresses towards an alpha value of 1.0 and then another that goes from 1.0 back to 0.
Use the animateWithDuration:animations:completion:
method of the UIView
to accomplish this. You can do the reverse in the completion block.
Something along the lines of:
[UIView animateWithDuration:0.2
animations:^{view.alpha = 1.0;}
completion:^(BOOL finished){
[UIView animateWithDuration:0.2
animations:^{view.alpha = 0;}];
}];
See UIView documentation for more details on animations.
It's in the docs:
If you combine autoreversing with a repeat count (settable using the setAnimationRepeatCount: method), you can create animations that shift back and forth between the old and new values a specified number of times. However, remember that the repeat count indicates the number of complete cycles. If you specify a whole number such as 2.0, the animation ends on the old value, which is followed by the view immediately updating itself to show the new value, which might be jarring. If you want the animation to end on the new value (instead of the old value), add 0.5 to the repeat count value. This adds an extra half cycle to the animation.
Update: read wrong your code, but docs suggest that you use animateWithDuration:delay:options:animations:completion:
instead if you are targeting iOS 4.0 and later.
If someone is interested, here's Swift code:
UIView.animate(withDuration: 0.2, animations: {
view.alpha = 1
}) { (finished) in
UIView.animate(withDuration: 0.2, animations: {
view.alpha = 0
})
}
精彩评论