ios 实现类似比微信还好的下拉tableview上面的图片变大的效果 博客分类: iOS 下拉图片放大UITableViewUIScrollViewiosios 图片效果
核心代码两个方法:
- (void)viewDidLoad
{
[superviewDidLoad];
self.myDataArray = [NSMutableArrayarrayWithObjects:@"小明",@"刘德华",@"李连杰",@"孙俪",@"刘小光",@"小沈阳", nil];
self.tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColorlightGrayColor];
self.tableView.contentInset = UIEdgeInsetsMake(kImageOriginHight, 0, 0, 0);
[self.view addSubview:self.tableView];
self.expandZoomImageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"aaa"]];
self.expandZoomImageView.frame = CGRectMake(0, -kImageOriginHight - kTempHeight, self.tableView.frame.size.width, kImageOriginHight + kTempHeight);
[self.tableViewaddSubview:self.expandZoomImageView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat yOffset = scrollView.contentOffset.y;
NSLog(@"yOffset===%f",yOffset);
CGFloat xOffset = (yOffset + kImageOriginHight)/2;
if (yOffset < -kImageOriginHight) {
CGRect f = self.expandZoomImageView.frame;
f.origin.y = yOffset - kTempHeight;
f.size.height = -yOffset + kTempHeight;
f.origin.x = xOffset;
f.size.width = 320 + fabsf(xOffset)*2;
self.expandZoomImageView.frame = f;
}
}
下面是全部的代码。。。。。。。。。
//
// HomeViewController.h
// expandZoom
//
// Created by Buddy on 14/7/14.
// Copyright (c) 2014年 Kyle Fang. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UIImageView *expandZoomImageView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *myDataArray;
@end
//
// HomeViewController.m
// expandZoom
//
// Created by Buddy on 14/7/14.
// Copyright (c) 2014年 Kyle Fang. All rights reserved.
//
#import "HomeViewController.h"
static CGFloat kImageOriginHight = 200;
static CGFloat kTempHeight = 80.0f;
@interfaceHomeViewController ()
@end
@implementation HomeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
self.myDataArray = [NSMutableArrayarrayWithObjects:@"小明",@"刘德华",@"李连杰",@"孙俪",@"刘小光",@"小沈阳", nil];
self.tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColorlightGrayColor];
self.tableView.contentInset = UIEdgeInsetsMake(kImageOriginHight, 0, 0, 0);
[self.view addSubview:self.tableView];
self.expandZoomImageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"aaa"]];
self.expandZoomImageView.frame = CGRectMake(0, -kImageOriginHight - kTempHeight, self.tableView.frame.size.width, kImageOriginHight + kTempHeight);
[self.tableViewaddSubview:self.expandZoomImageView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat yOffset = scrollView.contentOffset.y;
NSLog(@"yOffset===%f",yOffset);
CGFloat xOffset = (yOffset + kImageOriginHight)/2;
if (yOffset < -kImageOriginHight) {
CGRect f = self.expandZoomImageView.frame;
f.origin.y = yOffset - kTempHeight;
f.size.height = -yOffset + kTempHeight;
f.origin.x = xOffset;
f.size.width = 320 + fabsf(xOffset)*2;
self.expandZoomImageView.frame = f;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 44;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
returnself.myDataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *myCell = @"MyCellIndifer";
UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:myCell];
cell.textLabel.text = [self.myDataArray objectAtIndex:indexPath.row];
return cell;
}
@end