UIScrollView 的常用属性

ViewController.h

@interface ViewController : UIViewController

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //1.创建滚动视图
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 300)];
    scrollView.backgroundColor = [UIColor grayColor];
    //添加到当前的视图上显示
    [self.view addSubview:scrollView];
    
    //2.创建显示在滚动视图上的子视图
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350, 300)];
    view1.backgroundColor = [UIColor redColor];
    [scrollView addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 350, 300)];
    view2.backgroundColor = [UIColor greenColor];
    [scrollView addSubview:view2];
    
    //3.设置内容的尺寸
    scrollView.contentSize = CGSizeMake(350, 300*2);
    
    //____________UIScrollView 的常用属性____________
    
    //设置滚动时是否显示水平滚动条,默认是显示的(YES)
//    scrollView.showsHorizontalScrollIndicator = NO;
    //设置滚动时是否显示垂直滚动条,默认是显示的(YES)
//    scrollView.showsVerticalScrollIndicator = NO;
    
    //设置滚动条的样式,默认的、黑色和白色
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    
    //设置滚动视图滑动到末尾的时候是否有反弹效果,默认是YES
//    scrollView.bounces = NO;
    
    //设置是否有分页效果,默认是NO
//    scrollView.pagingEnabled = YES;
    
    //设置滚动的默认位置,设置偏移量
    scrollView.contentOffset = CGPointMake(0, 150);
    
    //是否开启滑动
//    scrollView.scrollEnabled = NO;
    
    //当手指离开屏幕的时候,如果视图开始滑动,则属性值为YES,其他情况为NO
//    scrollView.decelerating = YES;
    
    //设置代理
    scrollView.delegate = self;

}

#pragma mark - UIScrollViewDelegate
//视图在滑动的时候会不停的调用这个方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    //视图向上推动Y是正数
//    NSLog(@"Y方向的偏移量:%.2f",scrollView.contentOffset.y);
}

//开始拖动视图的时候调用这个代理方法
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    NSLog(@"开始拖动");
}

//结束拖动的时候调用的方法,手指离开屏幕的时候
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    NSLog(@"结束拖动");
    if (decelerate) {
        NSLog(@"正在减速");
    }else {
        NSLog(@"停止状态");
    }
}

//开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

    NSLog(@"将要开始减速了");
}

//已经停止减速,也就是停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    NSLog(@"已经停止");

}

@end


你可能感兴趣的:(UI初级)