筆者前不久終於發佈了本身的APP《小印記》,在此分享一些iOS源碼,若是讀者學到了有用的東西,但願能前往App Store下載《小印記》支持一下筆者,謝謝!服務器
首先看一下效果:atom
附上源碼:(佈局使用的是Masonry框架)spa
JKBaseAlertView.h.net
#import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, AlertViewMode) { OkCancelMode = 0, // 帶肯定和取消按鈕 OnlyOkMode // 僅帶肯定按鈕 }; typedef void(^ClickBlock)(NSMutableDictionary * dic); @interface JKBaseAlertView : UIView @property (nonatomic, strong) UIView * alertBgView; // 背景視圖 @property (nonatomic, strong) UIView * operateView; // 操做視圖 @property (nonatomic, strong) UILabel * titleLabel; // 標題 @property (nonatomic, strong) UILabel * detailLabel; // 副標題 @property (nonatomic, strong) UIButton * okButton; // 肯定按鈕 @property (nonatomic, strong) UIButton * cancelButton; // 取消按鈕 @property (nonatomic, strong) UIButton * neverShowBtn; // 再也不顯示按鈕 @property (nonatomic, copy) ClickBlock okBlock; /* * 單例 */ + (JKBaseAlertView *)sharedAlertView; /* * 彈框方式一 * mode 模式 * param 入參 * okBlock 肯定按鈕回調 */ - (void)showAlertWithMode:(AlertViewMode)mode param:(NSMutableDictionary*)param action:(ClickBlock)okBlock; /* * 彈框方式二 * mode 模式 * storeFlag 存儲標記(當storeFlag != nil時,會顯示「再也不顯示」按鈕) * param 入參 * okBlock 肯定按鈕回調 */ - (void)showAlertWithMode:(AlertViewMode)mode storeFlag:(NSString *)storeFlag param:(NSMutableDictionary*)param action:(ClickBlock)okBlock; /* * 肯定按鈕事件 */ - (void)okButtonClicked:(UIButton *)sender; /* * 取消按鈕事件 */ - (void)cancelButtonClicked:(UIButton *)sender; /* * 移除彈框視圖 */ - (void)removeAlertView; @end
JKBaseAlertView.mcode
#import "JKBaseAlertView.h" #define animateTime 0.25f #define titleFont 18 #define detailFont 16 #define plainHeight 95 @implementation JKBaseAlertView { NSString *_storeFlagStr; } #pragma mark - 單例 + (JKBaseAlertView *)sharedAlertView { static JKBaseAlertView * _alertView = nil; static dispatch_once_t once; dispatch_once(&once, ^{ _alertView = [[self alloc] init]; }); return _alertView; } #pragma mark - 建立UI - (void)showAlertWithMode:(AlertViewMode)mode storeFlag:(NSString *)storeFlag param:(NSMutableDictionary*)param action:(ClickBlock)okBlock { [self showAlertWithMode:mode param:param action:okBlock]; if (storeFlag != nil) { // 再也不顯示按鈕 [self.operateView addSubview:self.neverShowBtn]; [self.neverShowBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.operateView.mas_right).offset(-10); make.bottom.equalTo(self.operateView.mas_bottom).offset(-6); make.height.mas_equalTo(@30); }]; _storeFlagStr = storeFlag; } } - (void)showAlertWithMode:(AlertViewMode)mode param:(NSMutableDictionary*)param action:(ClickBlock)okBlock { WS(ws); int padding = 10; float titleHeight = [self heightForString:param[@"title"] fontSize:titleFont andWidth:kScreenWidth-40]; float detailHeight = [self heightForString:param[@"detail"] fontSize:detailFont andWidth:kScreenWidth-40]; // 背景視圖 [[[UIApplication sharedApplication] keyWindow] addSubview:self.alertBgView]; [UIView animateWithDuration:animateTime animations:^{ self.alertBgView.alpha = 1; }]; // 操做區背景 [self.alertBgView addSubview:self.operateView]; [self.operateView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(ws.alertBgView.mas_left).offset(padding); make.right.equalTo(ws.alertBgView.mas_right).offset(-padding); make.centerY.equalTo(ws.alertBgView.mas_centerY); make.height.mas_equalTo(titleHeight + detailHeight + plainHeight); }]; // 標題 [self.operateView addSubview:self.titleLabel]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(ws.operateView.mas_top).offset(padding); make.centerX.equalTo(ws.operateView.mas_centerX); make.left.equalTo(ws.operateView.mas_left).offset(padding); make.right.equalTo(ws.operateView.mas_right).offset(-padding); }]; if(param[@"title"]) { self.titleLabel.text = param[@"title"]; } // 副標題 if(param[@"detail"]) { [self.operateView addSubview:self.detailLabel]; [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(ws.titleLabel.mas_bottom).offset(padding*2); make.centerX.equalTo(ws.operateView.mas_centerX); make.left.equalTo(ws.operateView.mas_left).offset(padding); make.right.equalTo(ws.operateView.mas_right).offset(-padding); }]; self.detailLabel.text = param[@"detail"]; } [self.operateView addSubview:self.okButton]; if (mode == OkCancelMode) { // 肯定按鈕 [self.okButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(ws.operateView.mas_right).offset(-padding); make.left.equalTo(ws.operateView.mas_centerX).offset(padding); make.bottom.equalTo(ws.operateView.mas_bottom).offset(-padding); make.height.mas_equalTo(@35); }]; // 取消按鈕 [self.operateView addSubview:self.cancelButton]; [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(ws.operateView.mas_left).offset(padding); make.right.equalTo(ws.operateView.mas_centerX).offset(-padding); make.bottom.equalTo(ws.operateView.mas_bottom).offset(-padding); make.height.mas_equalTo(@35); }]; if(param[@"cancelButtonName"]) { [self.cancelButton setTitle:param[@"cancelButtonName"] forState:UIControlStateNormal]; } else { [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal]; } } else if (mode == OnlyOkMode) { // 肯定按鈕 [self.okButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(ws.operateView.mas_centerX); make.bottom.equalTo(ws.operateView.mas_bottom).offset(-padding); make.width.mas_equalTo(@100); make.height.mas_equalTo(@35); }]; } if(param[@"okButtonName"]) { [self.okButton setTitle:param[@"okButtonName"] forState:UIControlStateNormal]; } else { [self.okButton setTitle:@"肯定" forState:UIControlStateNormal]; } self.okBlock = okBlock; } #pragma mark - 根據字符串的的長度來計算高度 - (float)heightForString:(NSString *)string fontSize:(float)fontSize andWidth:(float)width { float height; if (string) { height = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:kSelfFontName size:fontSize],NSFontAttributeName, nil] context:nil].size.height; } else { height = 0.0f; } return height; } #pragma mark - 移除彈框視圖 - (void)removeAlertView { //退出 [UIView animateWithDuration:animateTime animations:^{ _alertBgView.alpha = 0; } completion:^(BOOL finished) { [self.alertBgView removeFromSuperview]; self.alertBgView = nil; self.operateView = nil; }]; } - (void)okButtonClicked:(UIButton *)sender { self.okBlock(nil); [self removeAlertView]; } - (void)cancelButtonClicked:(UIButton *)sender { [self removeAlertView]; } - (void)neverShowBtn:(UIButton *)sender { [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:_storeFlagStr]; [[NSUserDefaults standardUserDefaults] synchronize]; [self removeAlertView]; } #pragma mark - lazy load - (UIView *)alertBgView { if (_alertBgView == nil) { _alertBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]; _alertBgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]; _alertBgView.alpha = 0; } return _alertBgView; } - (UIView *)operateView { if (_operateView == nil) { _operateView = [[UIView alloc] init]; _operateView.backgroundColor = [UIColor whiteColor]; _operateView.layer.cornerRadius = 6.0f; _operateView.clipsToBounds = YES; } return _operateView; } - (UILabel *)titleLabel { if (_titleLabel == nil) { _titleLabel = [[UILabel alloc] init]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.numberOfLines = 0; _titleLabel.font = [UIFont fontWithName:kSelfFontName size:titleFont]; _titleLabel.textColor = [UIColor colorWithHexString:@"#555555"]; } return _titleLabel; } - (UILabel *)detailLabel { if (_detailLabel == nil) { _detailLabel = [[UILabel alloc] init]; _detailLabel.textAlignment = NSTextAlignmentCenter; _detailLabel.numberOfLines = 0; _detailLabel.font = [UIFont fontWithName:kSelfFontName size:detailFont]; _detailLabel.textColor = [UIColor colorWithHexString:@"#555555"]; } return _detailLabel; } - (UIButton *)okButton { if (_okButton == nil) { _okButton = [UIButton buttonWithType:UIButtonTypeCustom]; _okButton.backgroundColor = [UIColor colorWithHexString:@"#dd3030"]; _okButton.titleLabel.font = [UIFont fontWithName:kSelfFontName size:18]; [_okButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _okButton.layer.masksToBounds = YES; _okButton.layer.cornerRadius = 5.0f; [_okButton addTarget:self action:@selector(okButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; } return _okButton; } - (UIButton *)cancelButton { if (_cancelButton == nil) { _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; _cancelButton.layer.masksToBounds = YES; _cancelButton.layer.cornerRadius = 5.0f; _cancelButton.layer.borderWidth = 1.0f; _cancelButton.layer.borderColor = [UIColor colorWithHexString:@"#dd3030"].CGColor; _cancelButton.titleLabel.font = [UIFont fontWithName:kSelfFontName size:18]; [_cancelButton setTitleColor:[UIColor colorWithHexString:@"#dd3030"] forState:UIControlStateNormal]; [_cancelButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; } return _cancelButton; } - (UIButton *)neverShowBtn { if (_neverShowBtn == nil) { _neverShowBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _neverShowBtn.titleLabel.font = [UIFont fontWithName:kSelfFontName size:15]; [_neverShowBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [_neverShowBtn setTitle:@"再也不顯示" forState:UIControlStateNormal]; _neverShowBtn.layer.masksToBounds = YES; _neverShowBtn.layer.cornerRadius = 5.0f; [_neverShowBtn addTarget:self action:@selector(neverShowBtn:) forControlEvents:UIControlEventTouchUpInside]; } return _neverShowBtn; } @end
使用方法:orm
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"提示",@"title",@"確認退出?",@"detail", nil]; [[JKBaseAlertView sharedAlertView] showAlertWithMode:OkCancelMode param:dict action:^(NSMutableDictionary *dic) { NSLog(@"點擊肯定後的回調處理"); }];
最後附上《小印記》截圖,但願讀者多多支持