大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > IOS技巧 > UIActionSheet完美解决方案

UIActionSheet完美解决方案

关键词:UIActionSheet解决方案  阅读(654) 赞(19)

[摘要]本文是对UIActionSheet完美解决方案的讲解,对学习IOS苹果软件开发有所帮助,与大家分享。

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

最古老的方法一:

-(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就产生了,童鞋们可以在这个基础上,绘制自己需要的界面,添加自己的委托等等。



相关评论