咱们可以创建一个测试的demo工程.
一.创建一个UITabBarController然后root两个视图控制器,并把UITabBarController控制器作为window的根视图
在AppDelegate.m里
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[MainTabBarVC alloc]init];
[self.window makeKeyAndVisible];
在MainTabBarVC.m里
FirstViewController *firstVC = [FirstViewController new];
SecondViewController *secondVC= [SecondViewController new];
firstVC.title = @"首页";
secondVC.title = @"好友";
[self addChildViewController:firstVC];
[self addChildViewController:secondVC];
二.我们可以自定义View或者控制器来显示这个弹出的界面,这里我们选择使用自定义View不带XIB,在这个View中自定义两个控件,一个是ImageView,另一个是Button
三.在自定义View中进行控件的封装,这个我们要重写两个方法
1.重写
- (instancetype)initWithFrame
方法,此方法创建并添加子控件2.重写
- (void)layoutSubviews
方法,此方法中设置子控件的frame,在方法尾部要调用[super layoutSubviews]
代码如下:
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
self.imageView = imageView;
[self addSubview:self.imageView];
self.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer * ImageViewTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
ImageViewTap.numberOfTouchesRequired = 1; //手指数
ImageViewTap.numberOfTapsRequired = 1; //tap次数
self.imageView.contentMode = UIViewContentModeScaleToFill;
[self.imageView addGestureRecognizer:ImageViewTap];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
self.button = button;
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchDown];
[self addSubview:self.self.button];
}
return self;
}
- (void)layoutSubviews
{
self.imageView.frame = CGRectMake(100, 100, 200, 200);
self.button.frame = CGRectMake(180, 350, 50, 50);
[super layoutSubviews];
}
注:这里我们给ImageView添加手势,Button这里咱们也要addTarget
添加事件,一会儿会实现点击事件的触发.
四.现在需要考虑如何添加这个自定义View,也就是在什么时机添加这个自定义View.这里我们选择AppDelegate.m中的在应用程序载入后执行的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法
MyView *view = [[MyView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height)];
[self.window addSubview:view];
五.下面实现以下自定义View中的ImageView和Button所触发的事件方法
1.点击ImageView,这里我们触发手势事件,所执行的事件就是跳转到相应界面,这里我们的场景就是自定义View里的ImageView手势事件实现控制器的modal
首先拿到主窗口控制器,用根控制器进行modal需要的modal的控制器
ThirdViewController *third = [[ThirdViewController alloc]init];
UIViewController *VC = [UIApplication sharedApplication].keyWindow.rootViewController;
[VC presentViewController:third animated:YES completion:nil];
[self removeFromSuperview];
注:这里在跳转界面后我们要移除视图
2.点击Button时这里我们只需要移除视图就可以了,当然我这里为了好看也给了个动画效果
- (void) buttonClick {
[UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.transform = CGAffineTransformMakeScale(1.2, 1.2);
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
六.在ThirdViewController.m我们简单使用一下WKWebView加载一个网页
#import "ThirdViewController.h"
#import <WebKit/WebKit.h>
@interface ThirdViewController ()<WKNavigationDelegate>
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建NSURL
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
//创建NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//创建WKWebView对象,添加到界面(storyboard没有控件)
WKWebView *webView = [[WKWebView alloc]initWithFrame:self.view.frame];
[self.view addSubview:webView];
//设置代理
webView.navigationDelegate = self;
//加载请求
[webView loadRequest:request];
}
@end
七.设置一下自定义View的背景颜色
在MyView.m类中的- (instancetype)initWithFrame:(CGRect)frame
方法中是现实
self.backgroundColor = [[UIColor blackColor];
self.alpha = 0.5;
这里写完后运行程序我们可以看到View中的ImageView和Button上的图片都被覆盖了颜色,这样与预期效果不符,那么我们另辟蹊径,使用self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
方法就可以完美解决这个问题了
百度网盘地址: https://pan.baidu.com/s/1kUJmlOb 密码: ie2j