TableView下拉之视图放大及图片展示
2016-07-26 本文已影响236人
会走路的键盘
下面是动画效果
动画.gif话不多说,直接上代码
视图的创建
//UITableView顶部的ImageView
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, V_Width, V_height/3)];
self.imageView.image = [UIImage imageNamed:@"3"];
//设置imageView的填充模式
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createAllImageView)];
[self.imageView addGestureRecognizer:tap];
//UITableView
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, V_Width, V_height) style:UITableViewStylePlain];
//设置tableView的内边距
self.tableView.contentInset = UIEdgeInsetsMake(V_height/3, 0, 0, 0);
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
[self.view addSubview:self.imageView];
拖动TableView时的事件
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint point = scrollView.contentOffset;
//下拉图片放大
if (point.y < -V_height/3 && point.y >= -V_height/3-60) {
CGRect rect = self.imageView.frame;
rect.origin.y = 0;
rect.size.height = -point.y;
self.imageView.frame = rect;
}
//上拉图片跟随移动
if (point.y > -V_height/3) {
CGRect rect = self.imageView.frame;
rect.origin.y = -point.y - V_height/3;
self.imageView.frame = rect;
}
//下拉到一定程度,创建新的View
if (point.y <= -V_height/3-60) {
[self createAllImageView];
}
}
创建新的视图
/**
* 在Window上创建新的View
*/
- (void)createAllImageView{
//V_Width 屏幕宽的宏定义
//V_height 屏幕高的宏定义
//判断如果已经创建_allView,则不在创建
if (_allView) {
return;
}
//Window上的View
_allView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, V_Width, V_height/3)];
_allView.backgroundColor = [UIColor blackColor];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeAllImageView:)];
[_allView addGestureRecognizer:tap];
//新视图上的ImageView
_likeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, V_Width, V_height/3)];
_likeImageView.image = [UIImage imageNamed:@"3"];
[_allView addSubview:_likeImageView];
//在Window上添加视图
[[UIApplication sharedApplication].keyWindow addSubview:_allView];
CGRect rect1 = _allView.frame;
rect1.size.height = V_height;
CGRect rect2 = _likeImageView.frame;
rect2.origin.y = (V_height - V_height/3)/2;
//动画显示
[UIView animateWithDuration:0.8 animations:^{
_allView.frame = rect1;
_likeImageView.frame = rect2;
}];
}
Window上视图销毁
/**
* 移除Window上的View
*
*/
- (void)removeAllImageView:(UITapGestureRecognizer *)tap{
CGRect rect1 = _allView.frame;
rect1.size.height = V_height/3;
CGRect rect2 = _likeImageView.frame;
rect2.origin.y = 0;
//动画显示
[UIView animateWithDuration:0.5 animations:^{
_allView.frame = rect1;
_likeImageView.frame = rect2;
_allView.alpha = 0;
} completion:^(BOOL finished) {
[_likeImageView removeFromSuperview];
[_allView removeFromSuperview];
_allView = nil;
}];
}
tableView的一些代理事件这里就不在详细的写了,欢迎大家指正