弹出广告视图

点击页面的暂停按钮,弹出广告窗口,不多说,自己看代码,我注释应该写的挺清楚了��

//
//  ViewController.m
//  _advertisementAlert
//
//  Created by mac on 16/7/17.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
//引入广告窗口
#import "advertisementViewController.h"

//定义屏幕的宽高
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController () {

    //定义三个组成广告视图的元素
    //a) 阴影视图
    UIView *_shadowView;
    //b) 广告图片视图
    UIImageView *_imageView;
    //c) 关闭广告的按钮
    UIButton *_button;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //1.创建暂停按钮
    UIButton * pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //b) 设置frame
    pauseButton.frame = CGRectMake((kScreenWidth - 100)/2, (kScreenHeight-100)/2, 100, 100);
    //c) 设置按钮图片
    [pauseButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    //d) 设置点击事件
    [pauseButton addTarget:self action:@selector(clickPauseAction:) forControlEvents:UIControlEventTouchUpInside];
    //e) 添加到视图
    [self.view addSubview:pauseButton];
    //f) 设置背景图片
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgImage"]];

}

#pragma mark - 点击暂停按钮,弹出广告视图
- (void) clickPauseAction:(UIButton *) button {


    //1.创建阴影视图
    [self createShadowView];
    //2.创建广告图片视图
    [self createImageView];
    //3.创建关闭按钮
    [self createCloseButton];
}

#pragma mark - 创建关闭按钮
- (void) createCloseButton {

    //1.初始化
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    //2.设置frame
    _button.frame = CGRectMake(_imageView.frame.origin.x+_imageView.frame.size.width-20, _imageView.frame.origin.y, 20, 20);
    //3.设置按钮图片
    [_button setImage:[UIImage imageNamed:@"closeButton"] forState:UIControlStateNormal];
    //4.设置点击事件
    [_button addTarget:self action:@selector(closeAdverAction:) forControlEvents:UIControlEventTouchUpInside];
    //5.添加到视图上
    [self.view addSubview:_button];
}

#pragma mark - 关闭广告的响应事件
- (void) closeAdverAction:(UIButton *) button {

    //1.移除阴影视图
    [_shadowView removeFromSuperview];
    //2.移除图片视图
    [_imageView removeFromSuperview];
    //3.移除自己
    [_button removeFromSuperview];
}

#pragma mark - 创建广告图片视图
- (void) createImageView {

    //1.初始化
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-200)/2, (kScreenHeight-300)/2, 200, 300)];
    //2.添加广告图片
    _imageView.image = [UIImage imageNamed:@"adver"];
    _imageView.userInteractionEnabled = YES;
    //3.添加点击图片事件
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPictureAction:)];
    //4.将手势添加到视图
    [_imageView addGestureRecognizer:tapGesture];
    //4.添加到视图
    [self.view addSubview:_imageView];
}

#pragma mark - 点击广告图片,跳转到广告页面
- (void) clickPictureAction:(UITapGestureRecognizer *) tapGesture {

    //1.创建广告页面对象
    advertisementViewController *adverVC = [[advertisementViewController alloc] init];
    //2.设置跳转样式
    adverVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    //3.推送到广告页面
    [self presentViewController:adverVC animated:YES completion:nil];
}

#pragma mark - 创建阴影视图
- (void) createShadowView {

    //1.初始化
    _shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    //2.设置背景颜色
    _shadowView.backgroundColor = [UIColor blackColor];
    //3.设置透明度
    _shadowView.alpha = 0.618;
    //4.添加到视图
    [self.view addSubview:_shadowView];
}

@end
//
//  advertisementViewController.m
//  _advertisementAlert
//
//  Created by mac on 16/7/17.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "advertisementViewController.h"

#import "ViewController.h"

//定义屏幕的宽高
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface advertisementViewController () 

@end

@implementation advertisementViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    //1.返回按钮
    //a) 创建按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //b) 设置frame
    backButton.frame = CGRectMake(10, 20, 30, 30);
    //c) 设置图片
    [backButton setImage:[UIImage imageNamed:@"backButton"] forState:UIControlStateNormal];
    //d) 设置点击事件
    [backButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    //e) 添加到视图
    [self.view addSubview:backButton];

    //2.创建广告窗口
    [self createAdverst];

}

#pragma mark - 点击返回的响应事件
- (void) clickAction:(UIButton *) button {

    //1.创建页面对象
    ViewController *view = [[ViewController alloc] init];
    //2.设置跳转效果
    view.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    //3.推送到该页面
    [self presentViewController:view animated:YES completion:nil];

}

#pragma mark - 创建广告窗口
- (void) createAdverst {

    //1.创建一个web对象,获取网络地址
    UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 65, kScreenWidth, kScreenHeight)];
    //2.获取百度页面
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    //3.创建请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //4.加载请求
    [myWebView loadRequest:request];
    //5.将web对象添加到视图
    [self.view addSubview:myWebView];

}

@end

效果图
这里写图片描述
我这里点击图片之后,链接的是百度页面
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值