继续早上的源代码学习,下午练习了一下那些窗体移动的动画效果---视图动画。
视图动画属于动画的其中一种,其他还有关键动画。这些和学习flash动画的概念原理是差不多。因此大概可以理解补间里面所做的事情。UIView里面的方法很多,目前只是了解这种可以实现移动补间效果。至于还有其他?还要继续探讨一下。继续学习一下其他的动画内容。
下面写了个按钮调用动画的移动的效果,分别两个按钮,执行移动和移出的效果。
在block里面写执行的代码
[UIView animateWithDuration:0.5 animations:^{
//code
}];
#import "ViewController.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property (nonatomic,strong) UIView *uiView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.uiView = [[UIView alloc] init];
self.uiView .frame = (CGRect){0,0,240,ScreenHeight};
self.uiView.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.uiView];
UIButton *closeBtn = [UIButton buttonWithType: UIButtonTypeCustom];
closeBtn.frame = CGRectMake(30, 200, 200, 30);
[self.view addSubview:closeBtn];
closeBtn.layer.cornerRadius = 5;
[closeBtn setTitle:@"关闭菜单" forState:UIControlStateNormal];
closeBtn.backgroundColor =[UIColor greenColor];
[closeBtn addTarget:self action:@selector(closeMenu) forControlEvents:UIControlEventTouchUpInside];
UIButton *openBtn = [UIButton buttonWithType: UIButtonTypeCustom];
openBtn.frame = CGRectMake(30, 250, 200, 30);
[self.view addSubview:openBtn];
openBtn.layer.cornerRadius = 5;
[openBtn setTitle:@"打开菜单" forState:UIControlStateNormal];
openBtn.backgroundColor =[UIColor greenColor];
[openBtn addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
}
-(void) closeMenu
{
[UIView animateWithDuration:0.5 animations:^{
CGRect fr = self.uiView.frame;
fr.origin.x = -240;
self.uiView.frame = fr;
}];
}
-(void) openMenu
{
[UIView animateWithDuration:0.5 animations:^{
CGRect fr =self.uiView.frame;
fr.origin.x = 0;
self.uiView.frame = fr;
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行后的效果。