UIView 动画一:
利用UIView animation进行平移操作
- (IBAction)click:(id)sender {
[UIView beginAnimations:@"animations" context:nil];
[UIView setAnimationDuration:1.5];
CGRect frame = self.ball.frame;//imageView 的frame
frame.origin.y += 200 * flag;
flag =flag*( -1); //取反
self.ball.frame = frame;
[UIView commitAnimations];
/*
[UIView animateWithDuration:1.5 animations:^{
CGRect frame = self.ball.frame;
frame.origin.y += 100 * flag;//Y每次加100或减100
flag *= -1; //取反
self.ball.frame = frame;
}];
*/
}
不仅可以得用UIView实现一些常用的动画,还可以通过设置代理来监听动画的开始或结束,如:
- (IBAction)click:(id)sender {
[self.button setAlpha:0.0];
[UIView beginAnimations:@"animations" context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationDelegate:self];//设置代理
[UIView setAnimationWillStartSelector:@selector(viewAnimationStart)];//监听开始
[UIView setAnimationDidStopSelector:@selector(viewAnimationDone)];//监听动画的结束
CGRect frame = self.ball.frame;
frame.origin.y += 100 * flag;
flag *= -1; //取反
self.ball.frame = frame;
[UIView commitAnimations];
}
-(void)viewAnimationStart{
NSLog(@"animation start-----");
}
- (void)viewAnimationDone {
NSLog(@"animation end-----");
[UIView beginAnimations:@"animationDone" context:nil];
[self.button setAlpha:1.0];
[UIView commitAnimations];
}
UIView 动画之回调:
还可以通过回调来监听动画的完成
[UIView animateWithDuration:1.5 animations:^{
CGRect frame = self.ball.frame;
frame.origin.y += 100 * flag;
flag *= -1; //取反
self.ball.frame = frame;
} completion:^(BOOL finished) {
NSLog(@"动画结束了。");
}];