完美解决三:

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

最近程序中用到了,底部弹出框,看了下,前辈写得代码,搜索了下网路,发现其实都没有很好的解决问题,所以研究了下,将代码分享出来,跟大家分享一下,有问题的话,欢迎各位大牛批评指正。

最古老的方法一:-(void)CreateActionSheet{

self.actionSheet = [[UIActionSheetalloc] initWithTitle:@"选择"delegate:selfcancelButtonTitle:@"cancel"destructiveButtonTitle:nilotherButtonTitles:@"a",@"a",@"a", nil];

[self.actionSheetaddSubview:self.baseView];

}

上面的方法,是在系统的ActionSheet图层上覆盖图层,已达到底部弹出的效果。

优点:使用简单方便

缺点: 这个方法无法很好的解决ActionSheet高度问题,没有办法自定

改进方法二:继承UIActionSheet 重写 -(void)layoutSubviews方法。网上有很多这种代码,大家可以去看下。

优点:实现了ActionSheet的高度自定问题

缺点:失去了原有ActionSheet中得动画效果,和阴影点击效果

完美解决三:方案一: 在方法二中,补全确实的动画效果和阴影点击效果。问题可以很好的解决,单需要对IOS6和IOS7做不同的处理,因为IOS7时,ActonSheet的父视图有所改变,有兴趣的童鞋可以看看。

想要代码的可以给我留言。

方案二:按照IOS7中ActionSheet的原理重新定制。话不多说直接上代码

#import<UIKit/UIKit.h>@interfaceRecreateActionSheet : UIView

@PRoperty (nonatomic,retain) UIButton*baseView;

@property (nonatomic,retain) UIView*baseActionSheet;

@property (nonatomic,retain) UIView*contentView;

@property (nonatomic,retain) UINavigationBar*navBar;

@property (nonatomic,retain) UINavigationItem*navItem;-(id)initWithHeight:(CGFloat)_height WithTitle:(NSString *)_title;-(void)show;-(void)dismiss;@end

#import"RecreateActionSheet.h"@interfaceRecreateActionSheet (){

CGRect mainBounds;//主屏幕大小//UIWindow * mainWindow;//主屏幕CGRect actionRect1;//初始位置CGRect actionRect2;//结束位置}@end@implementationRecreateActionSheet-(id)initWithFrame:(CGRect)frame

{

self=[super initWithFrame:frame];if(self) {//Initialization code}returnself;

}-(id)initWithHeight:(CGFloat)_height WithTitle:(NSString *)_title{

mainBounds=[UIScreen mainScreen].bounds;//mainWindow = [[UIapplicationsharedApplication].windows objectAtIndex:0];actionRect1 = CGRectMake(0, mainBounds.size.height, mainBounds.size.width,0);

actionRect2= CGRectMake(0, mainBounds.size.height - _height -44, mainBounds.size.width, _height +44);

self=[super initWithFrame:mainBounds];if(self) {//Initialization code[self createUiWithHeight:_height];

[self createNavItemWithTitle:_title];

}returnself;

}-(void)dealloc{

[self.baseActionSheet release];

[self.contentView release];

[self.navBar release];

[self.navItem release];

[super dealloc];

}-(void)createUiWithHeight:(CGFloat)_height{

_baseView=[UIButton buttonWithType:UIButtonTypeCustom];

_baseView.frame=mainBounds;

_baseView.backgroundColor=[UIColor blackColor];

_baseView.alpha=0;

[_baseView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:_baseView];

_baseActionSheet=[[UIView alloc] init];

_baseActionSheet.frame=actionRect1;

_baseActionSheet.backgroundColor=[UIColor clearColor];

[self addSubview:_baseActionSheet];

_navBar=[[UINavigationBar alloc] init];

_navBar.frame= CGRectMake(0,0, mainBounds.size.width,44);

_navBar.barStyle=UIBarStyleBlackOpaque;

[_baseActionSheet addSubview:_navBar];

_contentView=[[UIView alloc] init];

_contentView.frame= CGRectMake(0,44, mainBounds.size.width, _height);

_contentView.backgroundColor=[UIColor whiteColor];

[_baseActionSheet addSubview:_contentView];

}-(void)createNavItemWithTitle:(NSString *)_title{

_navItem=[[UINavigationItem alloc] initWithTitle:nil];

UIBarButtonItem* leftButton = [[UIBarButtonItem alloc] initWithTitle:@"取消"style:UIBarButtonItemStyleBordered target:self action:@selector(buttonCancel)];

UIBarButtonItem* rightButton = [[UIBarButtonItem alloc] initWithTitle:@"确定"style:UIBarButtonItemStyleBordered target:self action:@selector(buttonOk)];

_navItem.leftBarButtonItem=leftButton;

_navItem.rightBarButtonItem=rightButton;

_navItem.title=_title;

[leftButton release];

[rightButton release];

[self.navBar setItems:[NSArray arrayWithObject:_navItem]];

}#pragmamark 取消和确定按钮事件

-(void)buttonOk{

[self dismiss];

}-(void)buttonCancel{

[self dismiss];

}#pragmamark 显示和隐藏

-(void)show{

[mainWindow addSubview:self];

[self addAnimationIn];

}-(void)dismiss{

[self addAnimationOut];

}#pragmamark 显示和隐藏动画

-(void)addAnimationIn{

[UIView animateWithDuration:0.2delay:0options:UIViewAnimationOptionCurveEaSEOut animations:^(void){

self.baseActionSheet.frame=actionRect2;

self.baseView.alpha=0.4;

} completion:^(BOOL finished){}];

}-(void)addAnimationOut{

[UIView animateWithDuration:0.3delay:0options:UIViewAnimationOptionCurveEaseOut animations:^(void){

self.baseActionSheet.frame=actionRect1;

self.baseView.alpha=0;

} completion:^(BOOL finished){[self removeFromSuperview];}];

}/*// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

// Drawing code

}*/@end

这样一个完美ActionSheet就产生了,童鞋们可以在这个基础上,绘制自己需要的界面,添加自己的委托等等。

欢迎大家多多交流。。。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航