此套替换方案採用“UIView+动画”方式实现(将UIActionSheet替换为UIView)
界面层级例如以下:
第一层:view(这一层充满整个屏幕,初始化时颜色为透明。userInteractionEnabled 为NO。显示时颜色为黑色,alpha值设置为0.6,userInteractionEnabled 为YES。作用是遮挡界面上其它的控件。)
第二层:contentView(这一层用来取代原来UIActionSheet。
UIPickerView就加入在这一层上。颜色为白色,alpha值设置为0.9)
第三层:UIPickerView
核心代码例如以下:
-(void)showPickerView
{
self.userInteractionEnabled = YES;
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
[self.contentView setFrame:CGRectMake(0, SCREEN_SIZE.height-picker.frame.size.height, 320, picker.frame.size.height)];
} completion:^(BOOL isFinished){ }];
}
-(void)hidePickerView
{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
self.backgroundColor = [UIColor clearColor];
[self.contentView setFrame:CGRectMake(0, SCREEN_SIZE.height, SCREEN_SIZE.width, SCREEN_SIZE.height)];
} completion:^(BOOL isFinished){
self.userInteractionEnabled = NO;
}];
}
注:
1.上述代码中的self即为第一层的view。
2.最好将这个功能封装成一个类,这样多个地方都用到的话就能够直接使用。