iOS中UIView的动画
ViewController1.h
1
2
3
4
5
6
7
|
#
import
<uikit uikit.h=
""
>
@interface
ViewController1 : UIViewController
@property
(nonatomic,retain)UIView* view1;
@end
</uikit>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#
import
"ViewController1.h"
@interface
ViewController1 ()
@end
@implementation
ViewController1
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [
super
initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if
(self) {
// Custom initialization
}
return
self;
}
- (
void
)viewDidLoad
{
[
super
viewDidLoad];
// Do any additional setup after loading the view.
_view1 = [[UIView alloc]init];
_view1.frame = CGRectMake(
0
,
44
,
160
,
200
);
_view1.backgroundColor = [UIColor redColor];
[self.view addSubview:_view1];
//过度动画的效果,是2个View的切换
UIButton* button2 = [[UIButton alloc]initWithFrame:CGRectMake(
20
,
340
,
150
,
30
)];
[button2 addTarget:self action:
@selector
(button2) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:@
"开始动画"
forState:UIControlStateNormal];
button2.backgroundColor = [UIColor redColor];
[self.view addSubview:button2];
}
-(
void
)button2
{
// 1 传统方法
//开始动画
/*[UIView beginAnimations:@"testanimation" context:nil];
[UIView setAnimationDuration:0.5];
//动画的代理
[UIView setAnimationDelegate:self];
//动画的响应事件,使视图自动回到原来的位置
[UIView setAnimationDidStopSelector:@selector(animationstop)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//获得视图的位置
CGRect frames = self.view1.frame;
//将坐标移到右边160处
frames.origin.x = 160;
self.view1.frame = frames;
[UIView commitAnimations];*/
// 2 用block语法实现
[UIView animateWithDuration:
0.5
animations:^{
CGRect frames = self.view1.frame;
frames.origin.x =
160
;
self.view1.frame = frames;
//缩放
self.view1.transform = CGAffineTransformScale(self.view1.transform,
0.01
,
0.01
);
}completion:^(BOOL finished) {
CGRect frames = self.view1.frame;
frames.origin.x =
0
;
self.view1.frame = frames;
//恢复到原始的缩放
self.view1.transform = CGAffineTransformIdentity;
}];
}
-(
void
)animationstop
{
//传统方法
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:
0.5
];
CGRect frames = self.view1.frame;
frames.origin.x =
0
;
self.view1.frame = frames;
[UIView commitAnimations];
}
- (
void
)didReceiveMemoryWarning
{
[
super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
|