2018-11-02 15:19:02 1963瀏覽
今天扣丁學(xué)堂iOS培訓(xùn)老師給大家介紹一下關(guān)于iOS開(kāi)發(fā)中你需要的彈窗效果總結(jié)大全,希望對(duì)iOS開(kāi)發(fā)的同學(xué)有所幫助,下面我們一起來(lái)看一下吧。// 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 - 協(xié)議 @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 - 協(xié)議 @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() /** 彈窗主內(nèi)容view */ @property (nonatomic,strong) UIView *contentView; /** 彈窗標(biāo)題 */ @property (nonatomic,copy) NSString *title; /** message */ @property (nonatomic,copy) NSString *message; /** 確認(rèn)按鈕 */ @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; }]; //------- 彈窗主內(nèi)容 -------// 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]; // 標(biāo)題 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]; // 確認(rèn)按鈕 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:@"通過(guò)Block彈窗回調(diào)的彈窗" block:^(NSInteger index) { if (index == AlertSureButtonClick) { [self alertSureButtonClick]; }else{ [self alertCauseButtonClick]; } }]; [alertView show];
到此為止,我們的block彈窗申明方法也搞定了。//.m @interface HLAlertViewBlock() /** 彈窗主內(nèi)容view */ @property (nonatomic,strong) UIView *contentView; /** 彈窗標(biāo)題 */ @property (nonatomic,copy) NSString *title; /** message */ @property (nonatomic,copy) NSString *message; /** 確認(rèn)按鈕 */ @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];
以上就是扣丁學(xué)堂iOS培訓(xùn)之iOS彈窗效果總結(jié)大全的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值。
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“iOS開(kāi)發(fā)培訓(xùn)”的相關(guān)資訊>>