iOS 仿滴滴出行界面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shihuboke/article/details/82699027

     联系人:石虎 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 效果图

IOS 仿滴滴出行界面

下载 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

谢谢!!!

猜你喜欢

转载自blog.csdn.net/shihuboke/article/details/82699027