Swift语言实现下拉变大效果:(上拉缩小效果随后研究......)
关键代码:方法一:
self.automaticallyAdjustsScrollViewInsets = false;
tableViewW = UITableView.init(frame: CGRectMake(0, 0, screenWidth(), screenHeight()), style: .Plain);
tableViewW.delegate = self;
tableViewW.dataSource = self;
tableViewW.showsVerticalScrollIndicator = false;
tableViewW.tableFooterView = UIView.init();
//下面两句话是关键点,并且顺序不能错,否则初始化运行的时候图片显示不完全
tableViewW.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
tableViewW.contentOffset = CGPointMake(0, -200);
if tableViewW.respondsToSelector(Selector("setLayoutMargins:"))
{
tableViewW.layoutMargins = UIEdgeInsetsZero;
}
if tableViewW.respondsToSelector(Selector("setSeparatorInset:"))
{
tableViewW.separatorInset = UIEdgeInsetsZero;
}
self.view.addSubview(tableViewW);
headView = UIImageView.init(frame: CGRectMake(0, -200, screenWidth(), 200));
headView.image = UIImage.init(named: "33.jpg");
//这句话是关键点设置高度改变宽度也改变
headView.contentMode = UIViewContentMode.ScaleAspectFill;
tableViewW.addSubview(headView);
实现UIScrollView的代理方法:
func scrollViewDidScroll(scrollView: UIScrollView) {
let offSetY:CGFloat = scrollView.contentOffset.y;//当这里有导航条的时候:+NavigationBarHeight
if offSetY < -200
{
//下拉,图片放大
var frameE:CGRect = headView.frame;
frameE.origin.y = offSetY;
frameE.size.height = -offSetY;//contentMode = UIViewContentModeScaleAspectFill;时,高度改变宽度也改变
headView.frame = frameE;
}
OC语言运用第二种方法实现,图片的下拉放大,下拉缩小的效果
关键代码:
self.title = @"下拉变大,上拉缩小";
self.automaticallyAdjustsScrollViewInsets = NO;
_myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, navHeight, screenWidth, screenHeight - navHeight)];
_myTableView.delegate = self;
_myTableView.dataSource = self;
_myTableView.contentInset = UIEdgeInsetsMake(headViewRect.size.height-navHeight-navHeight, 0, 0, 0);
[self.view addSubview:_myTableView];
_myView = [[HeadView alloc]initWithFrame:headViewRect backgroundView:@"22.jpg" headView:@"sun.png" headViewWidth:(CGFloat)(screenWidth / 4) signLabel:@"用户名:太阳*黑子"];
_myView.backgroundColor = [UIColor clearColor];
_myView.userInteractionEnabled = NO;
[self.view addSubview:_myView];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(20, 20, 60, 44);
[backBtn setTitle:@"<<返回" forState:UIControlStateNormal];
[backBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
实现UIScrollView的方法:
#pragma mark ------- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offset_Y = scrollView.contentOffset.y + headViewRect.size.height-navHeight-navHeight;
if (offset_Y < 0) {
//下拉图片放大
_myView.backgroundView.contentMode = UIViewContentModeScaleToFill;
_myView.backgroundView.frame = CGRectMake(offset_Y*0.5 , -navHeight, screenWidth - offset_Y, headViewRect.size.height - offset_Y);
}else if (offset_Y > 0 && offset_Y <= (headViewRect.size.height-navHeight-navHeight)) {
//下拉图片缩小,并改变图片的显示效果
_myView.backgroundView.contentMode = UIViewContentModeTop;
_myView.backgroundView.frame = CGRectMake(0 ,navHeight* offset_Y/(headViewRect.size.height-navHeight-navHeight)-navHeight , screenWidth , headViewRect.size.height -(navHeight + navHeight* offset_Y/(headViewRect.size.height-navHeight-navHeight)-navHeight) - offset_Y);
CGFloat width = offset_Y*(40-(screenWidth / 4))/(headViewRect.size.height-navHeight-navHeight)+(screenWidth / 4);
_myView.headView.frame =CGRectMake(0, 0, width,width);
_myView.headView.layer.cornerRadius =width*0.5;
_myView.headView.center = _myView.backgroundView.center;
_myView.signLabel.frame =CGRectMake(0, CGRectGetMaxY(_myView.headView.frame), screenWidth, 40);
_myView.signLabel.alpha = 1 - (offset_Y*3 / (headViewRect.size.height-navHeight-navHeight) /2);
}
}
效果图: