黑马程序员--分享自定义UIActionSheet
来源:互联网 发布:2016世界网络大会 编辑:程序博客网 时间:2024/06/05 21:18
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
学完黑马和基础视频许久, 最近牛刀小试, 发现在开发过程中很多UI界面都跟IOS原生的不一样, 加上要考虑4寸与3.5寸, 许多UI都要自定义, 在处分享一下自定义UIActionSheet的实现, 希望跟大家交流学习.
首先讲一下我的实现思路, 我是使用一个UIView来完成ActionSheet的界面, 之后在需要弹出的时候, 通过获取当前keyWindow, 然后添加我的UIView起到一个模仿UIActionSheet的效果, 这里我开放了比较多的属性, 使ActionSheet的界面效果可以调整. 另外命名还是比较容易看懂的, 就不加太多注释了, 自己添加了一些简单的动画效果, 界面布局的, 我用一些简单的计算去得出每一个控件的frame, 支持多个OtherButton的添加. 本人小菜鸟一个, 希望大神能指定一下其中的错误. 方便我改进.
头文件:
#import <UIKit/UIKit.h>@class SettingActionSheet;@protocol SettingActionSheetDelegate <NSObject>- (void)settingActionSheet:(SettingActionSheet *)actionSheet ClickButtonAtIndex:(NSInteger)buttonIndex;@end@interface SettingActionSheet : UIView@property (weak, nonatomic) id<SettingActionSheetDelegate> delegate;@property (weak, nonatomic) UIImageView *backgroundView;@property (weak, nonatomic) UILabel *titleLabel;@property (strong, nonatomic) UIImage *cancelButtonImage;@property (strong, nonatomic) UIImage *cancelButtonHighLightImage;@property (strong, nonatomic) UIImage *otherButtonImage;@property (strong, nonatomic) UIImage *otherButtonHighLightImage;- (instancetype)initWithTitle:(NSString *)title Delegate:(id<SettingActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle ButtonTitles:(NSArray *)titles;- (void)show;@end
下面是.m文件的实现
#import "SettingActionSheet.h"#define TitleLabelTopSpace 0 //标题栏顶部空隙#define TitleLabelWidth 320#define TitleLabelHeight 60#define ButtonHeight 40#define ButtonWidth 280#define ButtonSpace 10 // otherButton之间的距离#define CancelButtonHeight 40#define CancelButtonSpace 20 //Cancel按钮与Other按钮之间的间距#define LucencyViewAlpha 0.2@interface SettingActionSheet()@property (weak, nonatomic) UIView *lucencyView;@property (copy, nonatomic) NSString *title;@property (copy, nonatomic) NSString *cancelButtonTitle;@property (strong, nonatomic) NSArray *otherButtontitles;@end@implementation SettingActionSheet- (id)initWithTitle:(NSString *)title Delegate:(id<SettingActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle ButtonTitles:(NSArray *)titles { self = [super initWithFrame:[UIScreen mainScreen].bounds]; if (self) { // 使ActionSheet的View全透明 [self setBackgroundColor:[UIColor clearColor]]; self.title = title; self.cancelButtonTitle = cancelButtonTitle; self.delegate = delegate; self.otherButtontitles = titles;} return self;}#pragma mark - 加载所有子视图- (void)initSubViews { // 计算背景视图高度 NSInteger backgroundHeight = TitleLabelTopSpace + TitleLabelHeight + self.otherButtontitles.count * (ButtonHeight + ButtonSpace) + CancelButtonSpace + CancelButtonHeight + CancelButtonSpace; // 顶部半透明视图 UIControl *lucencyView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - backgroundHeight)]; [lucencyView setBackgroundColor:[UIColor lightGrayColor]]; [lucencyView setAlpha:0]; [lucencyView addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventTouchDown]; self.lucencyView = lucencyView; [self addSubview:lucencyView]; // 底部背景视图 UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - backgroundHeight, self.frame.size.width, backgroundHeight)]; [bgView setBackgroundColor:[UIColor blackColor]]; [bgView setUserInteractionEnabled:YES]; self.backgroundView = bgView; [self addSubview:bgView]; // 标题 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake( (320 - TitleLabelWidth) / 2, TitleLabelTopSpace, TitleLabelWidth, TitleLabelHeight)]; [titleLabel setBackgroundColor:[UIColor clearColor]]; [titleLabel setFont:[UIFont systemFontOfSize:17]]; [titleLabel setTextColor:[UIColor whiteColor]]; [titleLabel setTextAlignment:NSTextAlignmentCenter]; titleLabel.text = self.title; self.titleLabel = titleLabel; [self.backgroundView addSubview:titleLabel]; // 其它按钮 CGFloat x = (self.frame.size.width - ButtonWidth) / 2; CGFloat otherY = TitleLabelTopSpace + TitleLabelHeight; NSInteger i = 0; for (NSString *title in self.otherButtontitles) { [self createButtonWithFrame:CGRectMake(x, otherY, ButtonWidth, ButtonHeight) Title:title Index:i]; otherY += (ButtonSpace + ButtonHeight); i++; } // 取消按钮 CGFloat y = TitleLabelTopSpace + TitleLabelHeight + (ButtonHeight + ButtonSpace) * self.otherButtontitles.count + CancelButtonSpace; [self createCancelButtonWithFrame:CGRectMake(x, y, ButtonWidth, CancelButtonHeight) Title:self.cancelButtonTitle];}#pragma mark - 创建OtherButton- (void)createButtonWithFrame:(CGRect)buttonFrame Title:(NSString *)title Index:(NSInteger)index { UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame]; [button setTitle:title forState:UIControlStateNormal]; // 使用tag记录index button.tag = index; // 使用设置的背景 if (self.otherButtonImage) { [button setBackgroundImage:self.otherButtonImage forState:UIControlStateNormal]; } if (self.otherButtonHighLightImage) { [button setBackgroundImage:self.otherButtonHighLightImage forState:UIControlStateHighlighted]; } // 添加响应事件 [button addTarget:self action:@selector(clickButtonAtIndex:) forControlEvents:UIControlEventTouchUpInside]; [self.backgroundView addSubview:button];}#pragma mark - OtherButton监听事件- (void)clickButtonAtIndex:(UIButton *)button{ // 执行代理实现的方法 [self.delegate settingActionSheet:self ClickButtonAtIndex:button.tag]; [self dismissActionSheet];}#pragma mark - 创建cancelButton- (void)createCancelButtonWithFrame:(CGRect)frame Title:(NSString *)title { UIButton *cancelButton = [[UIButton alloc] initWithFrame:frame]; [cancelButton setTitle:title forState:UIControlStateNormal]; if (self.cancelButtonImage) { [cancelButton setBackgroundImage:self.cancelButtonImage forState:UIControlStateNormal]; } if (self.cancelButtonHighLightImage) { [cancelButton setBackgroundImage:self.cancelButtonHighLightImage forState:UIControlStateHighlighted]; } // 响应事件 [cancelButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventTouchUpInside]; [self.backgroundView addSubview:cancelButton];}#pragma mark 显示ActionSheet- (void)show { // 加载子视图 [self initSubViews]; // 将ActionSheet添加到主屏幕上 UIView *windowView = [UIApplication sharedApplication].keyWindow; [windowView addSubview:self]; // 移出屏幕 CGRect temp = self.backgroundView.frame; CGRect rect = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, temp.size.width, temp.size.height); [self.backgroundView setFrame:rect]; // 称回屏幕 [UIView animateWithDuration:LucencyViewAlpha animations:^{ self.backgroundView.frame = temp; [self.lucencyView setAlpha:LucencyViewAlpha]; }];}#pragma mark 退出ActionSheet- (void)dismissActionSheet { [self.lucencyView setAlpha:0]; [UIView animateWithDuration:LucencyViewAlpha animations:^{ self.backgroundView.frame = CGRectMake(0, self.frame.size.height, self.backgroundView.frame.size.width, self.backgroundView.frame.size.height); } completion:^(BOOL finished) { [self removeFromSuperview]; }];}@end
最后是实现代码:
SettingActionSheet *actionSheet = [[SettingActionSheet alloc] initWithTitle:@"恢复默认设置 ?" Delegate:self cancelButtonTitle:@"取消" ButtonTitles:@[@"确定"]]; actionSheet.cancelButtonImage = [UIImage imageNamed:@"shipin_btn_anniu2_normal"]; actionSheet.cancelButtonHighLightImage = [UIImage imageNamed:@"shipin_btn_anniu2_press"]; actionSheet.otherButtonImage = [UIImage imageNamed:@"shipin_btn_anniu1_normal"]; actionSheet.otherButtonHighLightImage = [UIImage imageNamed:@"shipin_btn_anniu1_press"]; [actionSheet show];
0 0
- 黑马程序员--分享自定义UIActionSheet
- 自定义UIActionSheet
- 自定义UIActionSheet
- UIActionsheet 自定义
- 自定义UIActionSheet
- 自定义UIActionSheet
- 自定义UIActionSheet
- UIActionSheet,UIAlertView技术分享
- iOS 自定义UIActionSheet
- 《黑马程序员》 自定义tomcat服务端
- 黑马程序员_自定义异常
- 黑马程序员 java知识点分享_radixConvertUtils
- 黑马程序员 java知识点分享_异常
- IOS开发之自定义UIActionSheet
- 如何自定义UIActionSheet中的内容
- iPhone开发之自定义UIActionSheet
- IOS开发之自定义UIActionSheet
- MMPopupView(自定义UIAlertView、UIActionSheet、UIDatePicker)
- [ZOJ 3353] Chess Board [搜索+状态压缩]
- 二叉查找树
- 射频电路设计实录第二章,产品技术指标
- 拓扑排序
- DOM JDOM DOM4J
- 黑马程序员--分享自定义UIActionSheet
- Apache Solr4 on Windows Tomcat7及中文分词安装
- cocos2D-x 学习笔记 【三】事件优先级和自定义事件
- dos命令大全
- 用JADE创建多agent系统
- hdu4906 Our happy ending,状态压缩DP
- sublime与python相遇(2)
- CCProxy on Windows Server 2012 设置
- hdu1072 bfs