UIActionSheet完美解決方案

來源:互聯網
上載者:User

  最近程式中用到了,底部彈出框,看了下,前輩寫得代碼,搜尋了下網路,發現其實都沒有很好的解決問題,所以研究了下,將代碼分享出來,跟大家分享一下,有問題的話,歡迎各位大牛批評指正。

最古老的方法一:

-(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>@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; //結束位置}@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 顯示和隱藏動畫-(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

這樣一個完美ActionSheet就產生了,童鞋們可以在這個基礎上,繪製自己需要的介面,添加自己的委託等等。

歡迎大家多多交流。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.