iOS tableview 상단 스 트 레 칭 효과 구현

본 고 는 iOS tableview 머리 스 트 레 칭 효과 가 보 여 주 는 구체 적 인 코드 를 공유 했다.예 를 들 어 개인 정보 인터페이스 스 트 레 칭 효과,드 롭 다운 이미지 확대 등 이다.

코드:

//
// PersonController.m
// Spread
//
// Created by qiuxuewei on 16/3/21.
// Copyright © 2016     . All rights reserved.
//

#import "PersonController.h"

@interface PersonController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>{

}

//    
/**        */
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UIView *headerBackView;
/**        */
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation PersonController
#pragma mark -    
-(UIView *)headerBackView{
 if (_headerBackView == nil) {
  _headerBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
  [_headerBackView setBackgroundColor:[UIColor lightGrayColor]];
 }
 return _headerBackView;
}
-(UIImageView *)headerImageView{
 if (_headerImageView == nil) {
  _headerImageView = [[UIImageView alloc] init];
  [_headerImageView setImage:[UIImage imageNamed:@" _  .JPG"]];
  [_headerImageView setBackgroundColor:[UIColor greenColor]];
  [_headerImageView setContentMode:UIViewContentModeScaleAspectFill];
  [_headerImageView setClipsToBounds:YES];
 }
 return _headerImageView;
}
-(UITableView *)tableView{
 if (_tableView == nil) {
  _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
  [_tableView setDataSource:self];
  [_tableView setDelegate:self];
 }
 return _tableView;
}

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view from its nib.
 //     
 [self addChildViews];
}
#pragma mark -     
//     
-(void)addChildViews{
 //    
 [self.view addSubview:self.tableView];
 //      
 [self addHeaderImageView];
}
//    
-(void)addHeaderImageView{
 [self.tableView setTableHeaderView:self.headerBackView];
 [self.headerImageView setFrame:self.headerBackView.bounds];
 [self.headerBackView addSubview:self.headerImageView];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 return 4;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
 return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 return 64;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 //      ,          ,                 。
 //     ,            。
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//   cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

 static NSString *ID = @"cell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
 if (!cell) {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
 }
 //   cell  !
 [cell.textLabel setText:@"  "];
 [cell.detailTextLabel setText:@"2016-03-22"];

 return cell;
}

//  tableview     
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

 //    
 CGFloat imageHeight = self.headerBackView.frame.size.height;
 //    
 CGFloat imageWidth = kScreenWidth;
 //       
 CGFloat imageOffsetY = scrollView.contentOffset.y;

 NSLog(@"        imageOffsetY:%f ->",imageOffsetY);



 //  
 if (imageOffsetY < 0) {
  CGFloat totalOffset = imageHeight + ABS(imageOffsetY);
  CGFloat f = totalOffset / imageHeight;

  self.headerImageView.frame = CGRectMake(-(imageWidth * f - imageWidth) * 0.5, imageOffsetY, imageWidth * f, totalOffset);
 }

// //  
// if (imageOffsetY > 0) {
//  CGFloat totalOffset = imageHeight - ABS(imageOffsetY);
//  CGFloat f = totalOffset / imageHeight;
//  
//  [self.headerImageView setFrame:CGRectMake(-(imageWidth * f - imageWidth) * 0.5, imageOffsetY, imageWidth * f, totalOffset)];
// }


}


- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
}
*/

@end
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기