有时候我们需要用Switch来控制某个视图的出现和消失,我们希望能有淡入淡出的效果,而不是突然的出现和消失。
以Date Picker为例,常用于备忘录中设置提醒时间。
@property (weak, nonatomic) IBOutlet UISwitch *switchButton;
@property (weak, nonatomic) IBOutlet UIView *datepickView;
- (IBAction)switch:(id)sender
{
BOOL isButtonOn = [self.switchButton isOn];
if (isButtonOn) {
self.datepickView.alpha = 0.0;
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:0.6]; //修改延时
self.datepickView.alpha = 1.0;
[UIView commitAnimations];
}else{
self.datepickView.alpha = 1.0;
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:0.6];
self.datepickView.alpha = 0.0;
[UIView commitAnimations];
}
}