codecamp

UIActionSheet上添加UIPickerView iOS8替换方案

此套替换方案采用“UIView+动画”方式实现(将UIActionSheet替换为UIView)


界面层级如下:

第一层:view(这一层充满整个屏幕,初始化时颜色为透明,userInteractionEnabled 为NO;显示时颜色为黑色,alpha值设置为0.6,userInteractionEnabled 为YES。作用是遮挡界面上其他的控件。)

第二层:contentView(这一层用来代替原来UIActionSheet。UIPickerView就添加在这一层上,颜色为白色,alpha值设置为0.9)

第三层:UIPickerView


核心代码如下:

  1. -(void)showPickerView  
  2. {  
  3.     self.userInteractionEnabled = YES;  
  4.     [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){  
  5.         self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];  
  6.         [self.contentView setFrame:CGRectMake(0, SCREEN_SIZE.height-picker.frame.size.height, 320, picker.frame.size.height)];  
  7.     } completion:^(BOOL isFinished){  
  8.           
  9.     }];  
  10. }  
  11. -(void)hidePickerView  
  12. {  
  13.     [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){  
  14.         self.backgroundColor = [UIColor clearColor];  
  15.         [self.contentView setFrame:CGRectMake(0, SCREEN_SIZE.height, SCREEN_SIZE.width, SCREEN_SIZE.height)];  
  16.     } completion:^(BOOL isFinished){  
  17.         self.userInteractionEnabled = NO;  
  18.     }];  
  19. }  

注:

1.上述代码中的self即为第一层的view。

2.最好将这个功能封装成一个类,这样多个地方都用到的话就可以直接使用。

xcode6模拟器模拟位置功能
iOS利用storyboard修改UITextField的placeholder文字颜色
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }