首先我们先集成一个UIView做一个自己的View
#import <UIKit/UIKit.h>
@interface MyView : UIView
@property (nonatomic,strong)UIView * commentView;
@end
在实现文件里重写系统的- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event方法
#import "MyView.h"
@implementation MyView
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *result = [super hitTest:point withEvent:event];
if (result == self) {
return nil;
} else {
return result;
}
}
@end
然后我们转回到ViewController里首先我们导入我们实现的MyView,并创建一个它的属性,然后写一个UITableView的属性,并实现它的dai代理,
#import "ViewController.h"
#import "MyView.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)MyView * headerView;
@end
接下来 我们就可以实现TableVeiw的代理方法和创建,然后我们设置TableView和headerView并用观察者观察tableVeiw的偏移量,代码实现如下
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.automaticallyAdjustsScrollViewInsets = NO;
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
[self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
self.headerView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 )];
self.moreButtonView.frame = CGRectMake(0, self.headerView.frame.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50) ;
self.headerView.backgroundColor =[UIColor redColor];
[self.headerView addSubview:self.moreButtonView];
[self.view addSubview:self.headerView];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"contentOffset"])
{
CGPoint offset = [change[NSKeyValueChangeNewKey] CGPointValue];
NSLog(@"%f",offset.y);
if (offset.y <= 0 && -offset.y >= 114) {
if (-offset.y >= 200) {
return;
}
// UILabel * label = (UILabel*)[self.headerView viewWithTag:110];
// [label removeFromSuperview];
CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, -offset.y);
[UIView animateWithDuration:1 animations:^{
[self.myLabel removeFromSuperview];
self.headerView.backgroundColor = [UIColor redColor];
}];
self.headerView.frame = newFrame;
CGRect newViewRect = CGRectMake(0, self.headerView.bounds.size.height - 64 , [UIScreen mainScreen].bounds.size.width, 64);
UIView * view = (UIView*)[self.headerView viewWithTag:110];
view.frame = newViewRect;
if (-offset.y <=200)
{
self.tableView.contentInset = UIEdgeInsetsMake(-offset.y, 0, 0, 0);
}
self.moreButtonView.frame = CGRectMake(0, self.headerView.frame.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50) ;
} else {
CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, 64);
self.headerView.frame = newFrame;
// CGRect viewRect = CGRectMake(0, self.headerView.bounds.size.height - 64 , [UIScreen mainScreen].bounds.size.width, 64);
// UIView * view = (UIView*)[self.headerView viewWithTag:110];
// view.frame = viewRect;
[UIView animateWithDuration:1.0 animations:^{
[self.headerView addSubview:self.myLabel];
self.headerView.backgroundColor= [UIColor lightGrayColor];
}];
self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
self.moreButtonView.frame = CGRectMake(0, self.headerView.frame.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50) ;
}
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 30;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
if (indexPath.row == 0) {
cell.textLabel.text =@"123";
}else
cell.textLabel.text = @"哈哈哈";
return cell;
}
//- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
// UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
// view.backgroundColor = [UIColor greenColor];
// return view;
//}
//- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
// return @"1";
//}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
// SKStoreProductViewController * vc = [[SKStoreProductViewController alloc] init];
// vc.delegate = self;
// [vc loadProductWithParameters:
// //appId唯一的
// @{SKStoreProductParameterITunesItemIdentifier : @"587767923"} completionBlock:^(BOOL result, NSError *error) {
// //block回调
// if(error){
// NSLog(@"error %@ with userInfo %@",error,[error userInfo]);
// }else{
// //模态弹出appstore
// [self presentViewController:vc animated:YES completion:^{
//
// }
// ];
// }
// }];
//
//}
//- (UIView *)moreButtonView
//{
// if (_moreButtonView == nil) {
//
// _moreButtonView = [[UIView alloc] init];
// _moreButtonView.backgroundColor = [UIColor yellowColor];
//
// NSArray * arr = @[@"动态",@"关注",@"粉丝"];
//
// for (int i= 0; i<3; i++) {
// UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
// button.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width/3)*i, 0, [UIScreen mainScreen].bounds.size.width/3, 50);
// [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// [button setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
// button.tag = 100 + i;
// [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
// [_moreButtonView addSubview:button];
// }
//
//
//
// }
// return _moreButtonView;
//}
- (NSString*)sql
{
return @"jianbo_su";
}
- (void)buttonClick:(UIButton*)btn
{
if (btn.tag == 100) {
NSLog(@"su");
UIView * mainView = [[UIView alloc] initWithFrame:self.view.bounds];
mainView.tag = 250;
mainView.backgroundColor = [UIColor lightGrayColor];
mainView.alpha = 0.9;
[self.view addSubview:mainView];
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
view.backgroundColor = [UIColor whiteColor];
view.center = self.view.center;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[view addGestureRecognizer:tap];
[mainView addSubview:view];
}
if (btn.tag == 101) {
NSLog(@"jian");
}
if (btn.tag == 102) {
NSLog(@"bo");
}
}
- (void)tapAction:(UITapGestureRecognizer*)tap
{
UIView * view = (UIView*)[self.view viewWithTag:250];
[view removeFromSuperview];
}
- (UILabel *)myLabel
{
if (_myLabel == nil) {
_myLabel = [[UILabel alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width/2) - 75 , 10, 150, 40)];
_myLabel.text = @"苏建波";
_myLabel.textAlignment = NSTextAlignmentCenter;
_myLabel.tag = 110;
_myLabel.textColor = [UIColor blackColor];
[self.headerView addSubview:_myLabel];
}
return _myLabel;
}
接下来我们可以看一下效果
需要demo的联系qq:1956305923