1.新建Empty Application
command+n,添加一个oc文件(UIViewController),命名为:HomeViewController
2.在HomeViewController.h中声明全局变量
UIView *tView;
3.在HomeViewController.m中:
//创建View的函数
-(void) loadView
{
[super loadView];
//创建View
//UIView *tView;
tView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
tView.backgroundColor=[UIColor redColor];
[self.view addSubview:tView];
//创建按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(120, 300, 100, 30);
[btn setTitle:@"移动" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(animate:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void) animate:(id)sender
{
//添加动画
[UIView beginAnimations:nil context:nil];//开始动画
[UIView setAnimationDuration:0.75];//0.75s
// //翻转效果1
// [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
// forView:tView
// cache:YES];
//翻转效果2
if(tView.frame.origin.x==100)
{
tView.frame = CGRectMake(150, 150, 150, 150);
return;
}
if(tView.frame.origin.x==150)
{
tView.frame = CGRectMake(100, 100, 100, 100);
return;
}
[UIView commitAnimations]; //提交动画
}
4.运行,成功!