最近程序中用到了,底部彈出框,看了下,前輩寫得代碼,搜索了下網(wǎng)路,發(fā)現(xiàn)其實(shí)都沒(méi)有很好的解決問(wèn)題,所以研究了下,將代碼分享出來(lái),跟大家分享一下,有問(wèn)題的話,歡迎各位大牛批評(píng)指正。
-(void)CreateActionSheet{
self.actionSheet = [[UIActionSheetalloc] initWithTitle:@"選擇"delegate:selfcancelButtonTitle:@"cancel"destructiveButtonTitle:nilotherButtonTitles:@"a",@"a",@"a", nil];
[self.actionSheetaddSubview:self.baseView];
}
上面的方法,是在系統(tǒng)的ActionSheet圖層上覆蓋圖層,已達(dá)到底部彈出的效果。
優(yōu)點(diǎn):使用簡(jiǎn)單方便
缺點(diǎn): 這個(gè)方法無(wú)法很好的解決ActionSheet高度問(wèn)題,沒(méi)有辦法自定
繼承UIActionSheet 重寫 -(void)layoutSubviews方法。網(wǎng)上有很多這種代碼,大家可以去看下。
優(yōu)點(diǎn):實(shí)現(xiàn)了ActionSheet的高度自定問(wèn)題
缺點(diǎn):失去了原有ActionSheet中得動(dòng)畫效果,和陰影點(diǎn)擊效果
方案一: 在方法二中,補(bǔ)全確實(shí)的動(dòng)畫效果和陰影點(diǎn)擊效果。問(wèn)題可以很好的解決,單需要對(duì)IOS6和IOS7做不同的處理,因?yàn)镮OS7時(shí),ActonSheet的父視圖有所改變,有興趣的童鞋可以看看。
想要代碼的可以給我留言。
方案二:按照IOS7中ActionSheet的原理重新定制。話不多說(shuō)直接上代碼
#import <UIKit/UIKit.h>@interface RecreateActionSheet : 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"@interface RecreateActionSheet (){ CGRect mainBounds; //主屏幕大小// UIWindow * mainWindow; //主屏幕 CGRect actionRect1; //初始位置 CGRect actionRect2; //結(jié)束位置}@end@implementation RecreateActionSheet-(id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code } return self;}-(id)initWithHeight:(CGFloat)_height WithTitle:(NSString *)_title{ mainBounds = [UIScreen mainScreen].bounds;// mainWindow = [[UIapplication sharedApplication].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]; } return self;}-(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]];}#pragma mark 取消和確定按鈕事件-(void)buttonOk{ [self dismiss];}-(void)buttonCancel{ [self dismiss];}#pragma mark 顯示和隱藏-(void)show{ [mainWindow addSubview:self]; [self addAnimationIn];}-(void)dismiss{ [self addAnimationOut];}#pragma mark 顯示和隱藏動(dòng)畫-(void)addAnimationIn{ [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaSEOut animations:^(void){ self.baseActionSheet.frame = actionRect2; self.baseView.alpha = 0.4; } completion:^(BOOL finished){}];}-(void)addAnimationOut{ [UIView animateWithDuration:0.3 delay:0 options: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
這樣一個(gè)完美ActionSheet就產(chǎn)生了,童鞋們可以在這個(gè)基礎(chǔ)上,繪制自己需要的界面,添加自己的委托等等。
歡迎大家多多交流。。。
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注