问题
I have the method:
- (void)blinkView:(UIView *)view
{
view.layer.opacity = 0.0f;
view.hidden = NO;
[UIView beginAnimations:@"Blinking" context:nil];
[UIView setAnimationRepeatCount:1.0];
[UIView setAnimationDuration:0.6f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
view.layer.opacity = 1.0f;
[UIView commitAnimations];
}
How can i write this code with blocks, and how i must implement method with reverse effect (hide uiview with blink) ?
回答1:
[UIView transitionWithView: view
duration:0.6f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{ view.layer.opacity = 1.0f; }
completion:NULL];
or
[UIView transitionWithView: view
duration:0.6f
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^{ view.layer.opacity = 1.0f; }
completion:NULL];
You can set the repeat count by recursively calling the animation block (see here).
Hope it will help you.
回答2:
You can use UIView's setAnimationDelegate: and setAnimationDidStopSelector:
[UIView beginAnimations:@"Blinking" context:nil];
[UIView setAnimationRepeatCount:1.0];
[UIView setAnimationDuration:0.6f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
view.layer.opacity = 1.0f;
[UIView commitAnimations];
- (void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
// add your final code here : you can give new animation effect here.
}
Or try animateWithDuration (available only in iOS 4 or later)
[UIView animateWithDuration:0.6f
animations:^{
view.layer.opacity = 1.0f;
}
completion:^(BOOL completed){
// add your final code here : you can give new animation effect here.
}
];
来源:https://stackoverflow.com/questions/7820232/blink-hidden-and-using-blocks