2018-11-02 15:19:02 473浏览
今天扣丁学堂iOS培训老师给大家介绍一下关于iOS开发中你需要的弹窗效果总结大全,希望对iOS开发的同学有所帮助,下面我们一起来看一下吧。// ios8.0 之后 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"message" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"确定"); }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; // ios8.0 之前 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Tittle" message:@"This is message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil]; [alertView show];
#pragma mark - 协议 @class HLAlertView; @protocol HLAlertViewDelegate<NSObject> - (void)alertViewDidClickButtonWithIndex:(NSInteger)index; @end
<HLAlertViewDelegate> self.delegate = self; #pragma mark --- HLAlertViewDelegate -(void)alertViewDidClickButtonWithIndex:(NSInteger)index{ if (index == AlertSureButtonClick) { [self alertSureButtonClick]; }else{ [self alertCauseButtonClick]; } }
//.h 文件 #import <UIKit/UIKit.h> typedef enum : NSUInteger { AlertCauseButtonClick = 0, AlertSureButtonClick } AlertButtonClickIndex; #pragma mark - 协议 @class HLAlertView; @protocol HLAlertViewDelegate<NSObject> - (void)alertViewDidClickButtonWithIndex:(NSInteger)index; @end @interface HLAlertView : UIView @property(nonatomic, weak) id <HLAlertViewDelegate> delegate; - (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message sureButton:(NSString *)sureBtn; - (void)show; @end
@interface HLAlertView() /** 弹窗主内容view */ @property (nonatomic,strong) UIView *contentView; /** 弹窗标题 */ @property (nonatomic,copy) NSString *title; /** message */ @property (nonatomic,copy) NSString *message; /** 确认按钮 */ @property (nonatomic,copy) UIButton *sureButton; @end @implementation HLAlertView - (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message sureButton:(NSString *)sureBtn{ if (self = [super init]) { self.title = tittle; self.message = message; [self sutUpView]; } return self; } - (void)sutUpView{ self.frame = [UIScreen mainScreen].bounds; self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.85]; [UIView animateWithDuration:0.5 animations:^{ self.alpha = 1; }]; //------- 弹窗主内容 -------// self.contentView = [[UIView alloc]init]; self.contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 80, 150); self.contentView.center = self.center; self.contentView.backgroundColor = [UIColor whiteColor]; self.contentView.layer.cornerRadius = 6; [self addSubview:self.contentView]; // 标题 UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, self.contentView.width, 22)]; titleLabel.font = [UIFont boldSystemFontOfSize:20]; titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.text = self.title; [self.contentView addSubview:titleLabel]; // message UILabel *messageLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, self.contentView.width, 22)]; messageLable.font = [UIFont boldSystemFontOfSize:17]; messageLable.textAlignment = NSTextAlignmentCenter; messageLable.text = self.message; [self.contentView addSubview:messageLable]; // 取消按钮 UIButton * causeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; causeBtn.frame = CGRectMake(0, self.contentView.height - 40, self.contentView.width/2, 40); causeBtn.backgroundColor = [UIColor grayColor]; [causeBtn setTitle:@"取消" forState:UIControlStateNormal]; [causeBtn addTarget:self action:@selector(causeBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:causeBtn]; // 确认按钮 UIButton * sureButton = [UIButton buttonWithType:UIButtonTypeCustom]; sureButton.frame = CGRectMake(causeBtn.width, causeBtn.y, causeBtn.width, 40); sureButton.backgroundColor = [UIColor redColor]; [sureButton setTitle:@"确定" forState:UIControlStateNormal]; [sureButton addTarget:self action:@selector(processSure:) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:sureButton]; } - (void)show{ UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; [keyWindow addSubview:self]; } - (void)processSure:(UIButton *)sender{ if ([self.delegate respondsToSelector:@selector(alertViewDidClickButtonWithIndex:)]) { [self.delegate alertViewDidClickButtonWithIndex:AlertSureButtonClick]; } [self dismiss]; } - (void)causeBtn:(UIButton *)sender{ if ([self.delegate respondsToSelector:@selector(alertViewDidClickButtonWithIndex:)]) { [self.delegate alertViewDidClickButtonWithIndex:AlertCauseButtonClick]; } [self dismiss]; } #pragma mark - 移除此弹窗 /** 移除此弹窗 */ - (void)dismiss{ [self removeFromSuperview]; }
HLAlertViewBlock * alertView = [[HLAlertViewBlock alloc] initWithTittle:@"提示" message:@"通过Block弹窗回调的弹窗" block:^(NSInteger index) { if (index == AlertSureButtonClick) { [self alertSureButtonClick]; }else{ [self alertCauseButtonClick]; } }]; [alertView show];
到此为止,我们的block弹窗申明方法也搞定了。//.m @interface HLAlertViewBlock() /** 弹窗主内容view */ @property (nonatomic,strong) UIView *contentView; /** 弹窗标题 */ @property (nonatomic,copy) NSString *title; /** message */ @property (nonatomic,copy) NSString *message; /** 确认按钮 */ @property (nonatomic,copy) UIButton *sureButton; @end @implementation HLAlertViewBlock - (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message block:(void (^)(NSInteger))block{ if (self = [super init]) { self.title = tittle; self.message = message; self.buttonBlock = block; [self sutUpView]; } return self; }
xib的封装弹窗
PopViewController * popVC = [[PopViewController alloc] init]; UIColor * color = [UIColor blackColor]; popVC.view.backgroundColor = [color colorWithAlphaComponent:0.85]; popVC.modalPresentationStyle = UIModalPresentationOverCurrentContext; [self presentViewController:popVC animated:NO completion:nil];
以上就是扣丁学堂iOS培训之iOS弹窗效果总结大全的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值。
【关注微信公众号获取更多学习资料】