ViewController.m
#import "ViewController.h"
#import "RedViewController.h"
#import "GreenViewController.h"
#import "BlueViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
- (void)viewDidLoad {
[super viewDidLoad];
RedViewController *rvc = [[RedViewController alloc]init];
GreenViewController *gvc =[[GreenViewController alloc]init];
BlueViewController *bvc = [[BlueViewController alloc]init];
CGRect frame = rvc.view.frame;
frame.size.height = self.view.frame.size.height /3;
rvc.view.frame = frame;
frame = gvc.view.frame;
frame.size.height = self.view.frame.size.height /3;
frame.origin.y = self.view.frame.size.height / 3;
gvc.view.frame = frame;
frame = bvc.view.frame;
frame.size.height = self.view.frame.size.height /3;
frame.origin.y = self.view.frame.size.height / 3 * 2;
bvc.view.frame = frame;
//将三个控制器中的 view 取出 添加到当前的控制器的View上
[self.view addSubview:rvc.view];
[self.view addSubview:gvc.view];
[self.view addSubview:bvc.view];
//将三个控制器作为子控制器 添加 当前控制器上
[self addChildViewController:rvc];
[self addChildViewController:gvc];
[self addChildViewController:bvc];
}
RedViewController.m
@implementation RedViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *pThouch = [touches anyObject];
if([pThouch locationInView:self.view].x <= self.view.frame.size.width *0.5 && [pThouch locationInView:self.view].y <= self.view.frame.size.height *0.5 ){
NSLog(@"点中第一个view 点中左上边");
}else{
NSLog(@"点中第一个view 点中右下边");
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
}
GreenViewController.m
@implementation GreenViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *pThouch = [touches anyObject];
if([pThouch locationInView:self.view].x <= self.view.frame.size.width *0.5 && [pThouch locationInView:self.view].y <= self.view.frame.size.height *0.5 ){
NSLog(@"点中第二个view 点中左上边");
}else if([pThouch locationInView:self.view].x <= self.view.frame.size.width *0.5 && [pThouch locationInView:self.view].y > self.view.frame.size.height *0.5){
NSLog(@"点中第二个view 点中左下边");
}else if([pThouch locationInView:self.view].x > self.view.frame.size.width *0.5 && [pThouch locationInView:self.view].y <= self.view.frame.size.height *0.5){
NSLog(@"点中第二个view 点中右上边");
}else{
NSLog(@"点中第二个view 点中右下边");
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor greenColor];
}
BlueViewController.m
@implementation BlueViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *pThouch = [touches anyObject];
if([pThouch locationInView:self.view].x <= self.view.frame.size.width /3 ){
NSLog(@"点中第三个view 点中左边");
} else if([pThouch locationInView:self.view].x <= self.view.frame.size.width /3 && [pThouch locationInView:self.view].x <= self.view.frame.size.width /3 *2 ){
NSLog(@"点中第三个view 点中中边");
}else{
NSLog(@"点中第三个view 点中右边");
}
}
效果图
image.png
网友评论