通过简单的调用UIView的类函数(void)animateWithDuration: animations:可以快速的实现ui控件的简单实用的动画效果
本文通过一个按钮来交替控制日期选择器从屏幕底部以动画的方式弹出以及收回,效果图:
demo源码:
@interface ViewController ()
{
BOOL bShowAction;
CGSize screenSize;
UIButton *button;
UIDatePicker *datePicker;
}
@end
@implementation ViewController
- (void)viewDidLoad {
bShowAction = NO;
[super viewDidLoad];
button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 45)];
button.center = CGPointMake(self.view.center.x, self.view.center.y-20); //按钮中心位置
[button setTitle:@"弹出日期选择器" forState:UIControlStateNormal]; //按钮文字
button.backgroundColor = [UIColor greenColor]; //按钮文字
button.layer.cornerRadius = 10.0; //设置按钮圆角
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
screenSize = self.view.bounds.size;
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, screenSize.height, 0, 0)];
[self.view addSubview:button];
[self.view addSubview:datePicker];
}
-(void) pressButton:(id)sender{
if (!bShowAction) {
bShowAction = YES;
[button setTitle:@"隐藏日期选择器" forState:UIControlStateNormal];
[UIView animateWithDuration:0.5 animations:^{
datePicker.frame = CGRectMake(0, screenSize.height/2, screenSize.width, 216);
}];
}
else{
bShowAction = NO;
[button setTitle:@"弹出日期选择器" forState:UIControlStateNormal];
[UIView animateWithDuration:0.5 animations:^{
datePicker.frame = CGRectMake(0, screenSize.height, screenSize.width, 216);
}];
}
}
那哪些属性放到UIView animateWithDuration:0.5 animations:的block里面可以实现动画效果呢?
可以分为两类:
1、以位置或尺寸相关的属性:
frame、bounds、center、transform、contentStretch
2、颜色属性:
alpha、backgroundColor