#import "ViewController.h"
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView * sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
sv.delegate = self;
sv.backgroundColor = [UIColor orangeColor];
sv.contentSize = CGSizeMake(320, 1000);
[self.view addSubview:sv];
[sv release];
// UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
// label.text = @"label in scrollView";
// [sv addSubview:label];
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -460, 320, 460)];
[sv addSubview:view];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 430, 320, 30)];
label.text = @"hello world!";
[view addSubview:label];
NSLog(@"sv.contentSize = {%f,%f}",sv.contentSize.width,sv.contentSize.height);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidScroll");
NSLog(@"contentOffset.y = %f",scrollView.contentOffset.y);
if(scrollView.contentOffset.y <= -35)
{
scrollView.contentInset = UIEdgeInsetsMake(30, 0, 0, 0);
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidEndDecelerating");
NSLog(@"scllView.contentSize = {%f,%f}",scrollView.contentSize.width,scrollView.contentSize.height);
//scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
- (void)scrollViewDidEndDecelerating //结束 减速
- (void)scrollViewWillBeginDecelerating //开始 减速
注意 这两个函数的执行时机
手动拖动时,这两个函数不会执行的。
在egofresh上两个函数
先执行:
scrollView.contentInset = UIEdgeInsetsMake(64.0f, 0.0f, 0.0f, 0.0f);
多出来的64 .0f 算做scrollView的content (其实本来就是加到scrollView上的)
并且执行完毕后 scrollView的contentOffset变成了 64.0f,这也就是为什么当你再往上拉,它没有重复叠加,还是显示64的高,
其实这个函数的本质就是改变contentOffset把围绕在原来scrollView周围的content拿来显示. 此时contentSize并不改变!
再执行:
[scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
scrollView的content还原 会看到scrollView向上移动的过程
但是这个过程中 会触发
-(void)scrollViewDidScroll 函数
只要scrollView的contentOffset改变,那么scrollViewDidScroll这个函数必然执行
并不会触发
- (void)scrollViewDidEndDecelerating 这个函数 而且 scrollView的 offset也根本不会改变,改变的只是scrollView.content.size
scrollView.contentInset这个属性貌似只能在代理方法中修改.
--切记