首页 > 其他 > 详细

UIActionSheet完美解决方案

时间:2014-05-19 08:35:56      阅读:408      评论:0      收藏:0      [点我收藏+]

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

最古老的方法一:

-(void)CreateActionSheet{

    self.actionSheet = [[UIActionSheetallocinitWithTitle:@"选择"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的原理重新定制。话不多说直接上代码

bubuko.com,布布扣
#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
bubuko.com,布布扣
bubuko.com,布布扣
#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
bubuko.com,布布扣

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

欢迎大家多多交流。。。

UIActionSheet完美解决方案,布布扣,bubuko.com

UIActionSheet完美解决方案

原文:http://www.cnblogs.com/ggxbq/p/3732142.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!