iOS 仿滴滴出行介面
阿新 • • 發佈:2018-11-16
聯絡人:石虎 QQ:1224614774 暱稱: 嗡嘛呢叭咪哄
QQ群:807236138 群稱: iOS 技術交流學習群
一、概念
hitTest作用
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
hitTest的作用:當在一個view上新增一個遮蔽罩,但又不影響對下面view的操作,也就是可以透過遮蔽罩對下面的view進行操作,這個函式就很好用了。
什麼是hitTest
point : 在接收器的區域性座標系(界)中指定的點。
event : 系統保證呼叫此方法的事件。如果從事件處理程式碼外部呼叫此方法,則可以指定nil。
returnValue : 檢視物件是當前檢視和包含點的最遠的後代。如果點完全位於接收方的檢視層次結構之外,則返回nil。
hitTest 的呼叫順序
touch -> UIApplication -> UIWindow -> UIViewController.view -> subViews -> ....-> 合適的view
事件的傳遞順序
view -> superView ...- > UIViewController.view -> UIViewController -> UIWindow -> UIApplication -> 事件丟棄
1、 首先由 view 來嘗試處理事件,如果他處理不了,事件將被傳遞到他的父檢視superview
2、superview 也嘗試來處理事件,如果他處理不了,繼續傳遞他的父檢視
UIViewcontroller.view
3、UIViewController.view嘗試來處理該事件,如果處理不了,將把該事件傳遞給UIViewController
4、UIViewController嘗試處理該事件,如果處理不了,將把該事件傳遞給主視窗Window
5、主視窗Window嘗試來處理該事件,如果處理不了,將傳遞給應用單例Application
6、如果Application也處理不了,則該事件將會被丟棄
二、demo 效果圖
下載 demo : https://github.com/shihu132/travelViewDemo
三、 仿滴滴出行介面實現
//
// ViewController.m
// 仿滴滴出行介面
//
// Created by joyshow on 2018/9/14.
// Copyright © 2018年 石虎. All rights reserved.
//
#import "ViewController.h"
#import "SHTableView.h"
/* 螢幕的尺寸 */
#define SHScreenW [UIScreen mainScreen].bounds.size.width
#define SHScreenH [UIScreen mainScreen].bounds.size.height
#define SHCell_height 200
#define SHCell_Count 8
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, weak) UITableView *sh_tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
}
- (void)setupUI {
UIImageView *sh_imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
sh_imageView.image = [UIImage imageNamed:@"qin_ai_nv_shen"];
[self.view addSubview:sh_imageView];
//20為狀態列高度;sh_tableView 設定的大小要和view的大小一致
SHTableView *sh_tableView = [[SHTableView alloc] initWithFrame:CGRectMake(0, 20, SHScreenW, SHScreenH) style:UITableViewStyleGrouped];
//tableview不延時
self.sh_tableView.delaysContentTouches = NO;
for (UIView *subView in self.sh_tableView.subviews) {
if ([subView isKindOfClass:[UIScrollView class]]) {
((UIScrollView *)subView).delaysContentTouches = NO;
}
}
//tableview下移
sh_tableView.contentInset = UIEdgeInsetsMake(500, 0, 0, 0);
sh_tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SHScreenW, 0.001)];//去掉頭部空白
sh_tableView.backgroundColor = [UIColor clearColor];
sh_tableView.delegate = self;
sh_tableView.dataSource = self;
sh_tableView.showsVerticalScrollIndicator = NO;
sh_tableView.sectionHeaderHeight = 0.0;//消除底部空白
sh_tableView.sectionFooterHeight = 0.0;//消除底部空白
self.sh_tableView = sh_tableView;
[self.view addSubview:sh_tableView];
}
#pragma mark - cell數量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return SHCell_Count;
}
#pragma mark - cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return SHCell_height;
}
#pragma mark - 每個cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [UITableViewCell new];
if(indexPath.row % 2 == 0){
cell.backgroundColor = [UIColor orangeColor];
UILabel *sh_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SHScreenW, SHCell_height)];
sh_label.text = @"仿滴滴出行介面";
[cell.contentView addSubview:sh_label];
}else{
cell.backgroundColor = [UIColor redColor];
UIImageView *sh_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SHScreenW, SHCell_height)];
sh_imageView.image = [UIImage imageNamed:@"aaaaaa"];
[cell.contentView addSubview:sh_imageView];
}
return cell;
}
#pragma mark - 選中cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"shihu---點選了cell");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
四、自定義SHTableView
//
// SHableView.m
// 仿滴滴出行介面
//
// Created by joyshow on 2018/9/14.
// Copyright © 2018年 石虎. All rights reserved.
//
#import "SHTableView.h"
@implementation SHTableView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
NSLog(@"point=%@",NSStringFromCGPoint(point));
NSLog(@"y=%f",self.contentOffset.y);
if (point.y<0) {
return nil;
}
return [super hitTest:point withEvent:event];
}
@end