天天看點

iOS 自定義彈窗UIAlertView —— HERO部落格

上一篇簡述了iOS系統彈窗的使用,具體使用可以參考iOS 彈窗UIAlertView、UIActionSheet、UIAlertController簡述。

本篇自定義了一個繼承UIWindow的彈窗,可以自定義彈窗樣式,效果圖如下:

iOS 自定義彈窗UIAlertView —— HERO部落格

下面貼上相關代碼:(沒有使用圖檔,是以代碼中圖檔為空)

ViewController:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end


#import "ViewController.h"
#import "HWAlertView.h"

@interface ViewController ()<HWAlertViewDelegate>

@property (nonatomic, strong) HWAlertView *alertView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //建立控件
    [self creatContorl];
}

- (void)creatContorl
{
    //建立按鈕
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 150, 100, 30)];
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"HERO部落格" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)btnOnClick
{
    //初始化
    _alertView = [[HWAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //顯示alertView
    [_alertView show];
    //設定代理
    _alertView.delegate = self;
}

#pragma mark - HWAlertViewDelegate
- (void)alertView:(HWAlertView *)alertView didSelectOptionButtonWithTag:(NSInteger)tag
{
   if (tag == 1) {
        //跳轉至部落格
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://blog.csdn.net/hero_wqb"]];
    }
    
    _alertView.hidden = YES;
    _alertView = nil;
}

@end
           

HWAlertView:

#import <UIKit/UIKit.h>

@class HWAlertView;

@protocol HWAlertViewDelegate <NSObject>

/**
 *  HWAlertView代理方法,選項按鈕點選事件
 *
 *  @param alertView HWAlertView
 *  @param tag       選項按鈕tag值,左0右1
 */
- (void)alertView:(HWAlertView *)alertView didSelectOptionButtonWithTag:(NSInteger)tag;

@end

@interface HWAlertView : UIWindow

@property (nonatomic, weak) id<HWAlertViewDelegate> delegate;

- (void)show;
- (void)dismiss;

@end


#import "HWAlertView.h"

@implementation HWAlertView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.windowLevel = UIWindowLevelAlert;
        
        //背景遮蓋
        UIView *backView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        backView.backgroundColor = [UIColor blackColor];
        backView.alpha = 0.7;
        [self addSubview:backView];
        
        //彈窗背景圖檔
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
        imageView.frame = CGRectMake(0, 0, 280, 120);
        imageView.center = CGPointMake(self.center.x, self.center.y);
        imageView.contentMode = UIViewContentModeScaleToFill;
        imageView.userInteractionEnabled = YES;
        imageView.backgroundColor = [UIColor whiteColor];
        [self addSubview:imageView];
        
        //彈窗标題
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 280, 40)];
        label.text = @"是否進入HERO部落格";
        label.textAlignment = NSTextAlignmentCenter;
        [imageView addSubview:label];
        
        //選項按鈕
        NSArray *titleArray = @[@"取消", @"确定"];
        NSArray *imageArray = @[@"", @""];
        for (int i = 0; i < imageArray.count; i++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(40 + 120 * i, 70, 80, 30)];
            [btn setTitle:titleArray[i] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
            btn.tag = i;
            btn.backgroundColor = [UIColor grayColor];
            [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:btn];
        }
    }
    return self;
}

- (void)show
{
    [self makeKeyAndVisible];
}

- (void)dismiss
{
    [self resignKeyWindow];
    [self removeFromSuperview];
}

- (void)btnOnClick:(UIButton *)btn
{
    if (_delegate && [_delegate respondsToSelector:@selector(alertView:didSelectOptionButtonWithTag:)]) {
        [_delegate alertView:self didSelectOptionButtonWithTag:btn.tag];
    }
}

@end
           

寫部落格是希望大家共同交流成長,部落客水準有限難免有偏頗不足之處,歡迎批評指正。

繼續閱讀