10.2 UIScrollview概念和用法:UIScrollView常用代理方法



UIScrollView代理方法
// scrollView已经滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
// 视图已经放大或缩小
- (void)scrollViewDidZoom:(UIScrollView *)scrollView;
// scrollView开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
// scrollView结束拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
// scrollView开始减速(以下两个方法注意与以上两个方法加以区别)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; 
// scrollview减速停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;



常用代理方法
// 返回⼀一个放大或者缩小的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
// 开始放大或者缩小
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView: (UIView *)view
// 缩放结束时
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView: (UIView *)view atScale:(float)scale;
// 是否支持滑动至顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
// 滑动到顶部时调用该方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;



10.2 UIScrollview概念和用法:UIScrollView常用代理方法_第1张图片


测试代码:

//
//  RootViewController.m
//  my_ScrollView
//
//  Created by fenghuo on 14-7-21.
//  Copyright (c) 2014年 fenghuo. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController () 
{
    UIScrollView *scrollView;
}

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
    scrollView.backgroundColor = [UIColor redColor];
    
    // 是否支持滑动最顶端
//    scrollView.scrollsToTop = NO;
    
    scrollView.delegate = self;
    
    // 设置内容大小
    scrollView.contentSize = CGSizeMake(320, 460 * 10);
    [self.view addSubview:scrollView];

    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 40)];
    label.backgroundColor = [UIColor yellowColor];
    label.text = @"学习scrolleview";
    [scrollView addSubview:label];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UIScrollViewDelegate

/*
// 返回一个放大或者缩小的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return nil;
}

// 开始放大或者缩小
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
    
}

// 缩放结束时
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    
}

// 视图已经放大或缩小
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    
}
*/

// 是否支持滑动至顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
    return YES;
}

// 滑动到顶部时调用该方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScrollToTop");
}

// scrollView 已经滑动----正在滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScroll");
}

// scrollView 开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewWillBeginDragging");
}

// scrollView 结束拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"scrollViewDidEndDragging");
}


// scrollView 开始减速(以下两个方法注意与以上两个方法加以区别)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewWillBeginDecelerating");
}


// scrollview 减速停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidEndDecelerating");
}

@end


运行结果:

2014-07-21 14:35:03.566 my_ScrollView[2489:a0b] scrollViewWillBeginDragging
2014-07-21 14:35:03.566 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:03.723 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:03.741 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:03.785 my_ScrollView[2489:a0b] scrollViewDidEndDragging
2014-07-21 14:35:03.786 my_ScrollView[2489:a0b] scrollViewWillBeginDecelerating
2014-07-21 14:35:03.803 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:03.820 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.319 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.653 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.670 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.686 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.703 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.720 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.903 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.919 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.936 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.953 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.969 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:04.986 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:05.003 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:05.486 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:35:05.503 my_ScrollView[2489:a0b] scrollViewDidEndDecelerating




ScrollView当滚动很多时,点击状态栏,它自己会回到最顶上

可以使用属性控制,			scrollView.scrollsToTop = NO;
也可以使用代理方法控制,
// 是否支持滑动至顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
    return YES;
}

// 滑动到顶部时调用该方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScrollToTop");
}

2014-07-21 14:37:04.353 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:37:04.369 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:37:04.386 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:37:04.402 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:37:04.419 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:37:04.436 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:37:04.452 my_ScrollView[2489:a0b] scrollViewDidScroll
2014-07-21 14:37:04.453 my_ScrollView[2489:a0b] scrollViewDidScrollToTop







你可能感兴趣的:(IOS,知识整理)