- 主要利用scrollView的偏移量和形变实现效果
- 关键代码
//
// ViewController.m
// Test
//
// Created by liyang on 16/5/9.
// Copyright © 2016年 liyang. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
/** 图片 */
@property (nonatomic, strong) UIImageView *img;
/** backView2 */
@property (nonatomic, strong) UIView *navBackView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationItem.leftBarButtonItem = ({
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStylePlain target:self action:@selector(left:)];
left;
});
[self.view addSubview:self.scrollView];
// 设置导航条的背景色透明
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithStretchableImageName:@"navBar_background"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage imageWithStretchableImageName:@"navBar_background"]];
[self navBackView];
}
- (UIView *)navBackView
{
if (!_navBackView) {
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, kScreenWidth, 64);
view.alpha = 0;
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
self.navBackView = view;
}
return _navBackView;
}
- (void)left:(UIBarButtonItem *)sender
{
}
- (UIScrollView *)scrollView
{
if (!_scrollView) {
UIScrollView *scrollerView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
scrollerView.contentSize = CGSizeMake(0, kScreenHeight*1.5);
scrollerView.delegate = self;
scrollerView.backgroundColor = [UIColor whiteColor];
self.scrollView = scrollerView;
UIImageView *imgV = [[UIImageView alloc] init];
imgV.frame = CGRectMake(0, 0, self.scrollView.width, 200);
imgV.image = [UIImage imageNamed:@"1"];
[self.scrollView addSubview:imgV];
self.img = imgV;
}
return _scrollView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offset_y = scrollView.contentOffset.y;
if (offset_y < 0) {
self.navBackView.alpha = 0;
self.img.alpha = 1;
// 下拉的高度变化
CGFloat h = -offset_y - 0;
// 缩放系数
CGFloat scale = h / 100 + 1;
// 下拉放大
self.img.transform = CGAffineTransformMakeScale(scale, scale);
}else if (offset_y == 0){
self.navBackView.alpha = 0;
self.img.alpha = 1;
self.img.transform = CGAffineTransformIdentity;
}else if (offset_y > 0){
// 变化系数
CGFloat scale = offset_y/(200-64);
self.navBackView.alpha = scale;
self.img.alpha = (1-scale);
}
}
@end
应该都能看懂,我就没有多注释。
PS:求推荐一款好用的gif图制作软件(mac用),我现在用的是GIFBrewery,感觉用起来一般般