国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

UIActionSheet完美解決方案

2019-11-14 20:29:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  最近程序中用到了,底部彈出框,看了下,前輩寫得代碼,搜索了下網(wǎng)路,發(fā)現(xiàn)其實(shí)都沒(méi)有很好的解決問(wèn)題,所以研究了下,將代碼分享出來(lái),跟大家分享一下,有問(wèn)題的話,歡迎各位大牛批評(píng)指正。

最古老的方法一:

-(void)CreateActionSheet{

    self.actionSheet = [[UIActionSheetallocinitWithTitle:@"選擇"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)有辦法自定

改進(jìn)方法二:

繼承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ǔ)上,繪制自己需要的界面,添加自己的委托等等。

歡迎大家多多交流。。。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 郯城县| 若羌县| 织金县| 宁武县| 黎川县| 南开区| 郓城县| 西贡区| 同江市| 浏阳市| 宾川县| 布尔津县| 清远市| 神木县| 蓝田县| 金沙县| 陈巴尔虎旗| 望都县| 志丹县| 盖州市| 花莲市| 琼海市| 宁强县| 林芝县| 和田县| 锦屏县| 宁阳县| 乌鲁木齐县| 抚远县| 永新县| 尼玛县| 汝城县| 久治县| 古交市| 松江区| 偏关县| 卢氏县| 新晃| 田东县| 龙南县| 马边|