#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *currentView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
/*语法形式:
[UIView beginAnimations:@“动画的名字 “ context:nil];
……..
[UIView commitAnimations];
*/
//首尾动画
- (IBAction)beginAndEndAnmation:(UIButton *)sender {
[UIView beginAnimations:@"第一个动画" context:nil];
//是否根据最新的状态开始动画
[UIView setAnimationBeginsFromCurrentState:YES];
//设置延迟几秒开始动画
// [UIView setAnimationDelay:1.0];
//动画执行时间
[UIView setAnimationDuration:2.0];
//设置动画翻转
[UIView setAnimationRepeatAutoreverses:YES];
//设置翻转次数
[UIView setAnimationRepeatCount:2];
//要执行动画开始和结束方法,一定要设置代理
[UIView setAnimationDelegate:self];
//动画结束执行方法
[UIView setAnimationDidStopSelector:@selector(endAction)];
//动画开始执行方法
[UIView setAnimationWillStartSelector:@selector(startAction)];
//拿到view的中心点
CGPoint point = self.currentView.center;
//让view向下移动50
point.y += 50;
self.currentView.center = point;
//旋转
self.currentView.transform = CGAffineTransformRotate(self.currentView.transform, M_PI);
//放缩
self.currentView.transform = CGAffineTransformScale(self.currentView.transform, 0.5, 0.5);
[UIView commitAnimations];
}
//结束时执行
-(void)endAction{
NSLog(@"end");
}
//开始时执行
-(void)startAction{
NSLog(@"start");
}
//block动画块
- (IBAction)blockAnimation:(UIButton *)sender {
//block动画1
// [UIView animateWithDuration:2.0f animations:^{
// //拿到view的中心点
// CGPoint point = self.currentView.center;
//
// //让view向下移动50
// point.y += 50;
// self.currentView.center = point;
//
// }];
//block动画2
// [UIView animateWithDuration:2.0f animations:^{
// //拿到view的中心点
// CGPoint point = self.currentView.center;
//
// //让view向下移动50
// point.y += 50;
// self.currentView.center = point;
// } completion:^(BOOL finished) {
// [UIView animateWithDuration:2.0f animations:^{
// //拿到view的中心点
// CGPoint point = self.currentView.center;
//
// //让view向下移动50
// point.y -= 50;
// self.currentView.center = point;
//
// }];
// }];
//block动画3
// [UIView animateKeyframesWithDuration:2.0f delay:1.0f options:UIViewKeyframeAnimationOptionRepeat animations:^{
// //拿到view的中心点
// CGPoint point = self.currentView.center;
//
// //让view向下移动50
// point.y -= 50;
// self.currentView.center = point;
//
// } completion:^(BOOL finished) {
// NSLog(@"123");
// }];
//第一个参数 弹簧动画效果(0-1)
//第四个参数 动画起始速度
[UIView animateWithDuration:2.0f delay:1.0f usingSpringWithDamping:0.01 initialSpringVelocity:5.0f options:UIViewAnimationOptionRepeat animations:^{
//拿到view的中心点
CGPoint point = self.currentView.center;
//让view向下移动50
point.y += 50;
self.currentView.center = point;
} completion:^(BOOL finished) {
}];
}
//转场动画
- (IBAction)translationAnimation:(UIButton *)sender {
// [UIView transitionWithView:self.currentView duration:3 options:UIViewAnimationOptionRepeat animations:^{
// //拿到view的中心点
// CGPoint point = self.currentView.center;
//
// //让view向下移动50
// point.y += 50;
// self.currentView.center = point;
//
// } completion:^(BOOL finished) {
//
// }];
UIView * view = [[UIView alloc]init];
view.backgroundColor = [UIColor yellowColor];
view.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:view];
//转场后 self.currentView 从父视图移除 toView显示
[UIView transitionFromView:self.currentView toView:view duration:2.0f options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end