IOS UIScrollerView滚动视图的简单使用

#import "RootViewController.h"

@interface RootViewController ()
{

    UIScrollView * _scollView;
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _scollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self .view.frame))];
    _scollView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_scollView];
    [_scollView release];
    
#pragma mark - 一个个添加图片
//    //为滚动视图添加子视图
//    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_scollView.frame), CGRectGetHeight(_scollView.frame))];
//    [imageView setImage:[UIImage imageNamed:@"tiank.jpg"]];
//    imageView.contentMode = UIViewContentModeScaleAspectFit;//设置图片视图的内容显示(这里设置为适合本身长宽比例)
//    [_scollView addSubview:imageView];
//    
//    UIImageView * imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(_scollView.frame), 0, CGRectGetWidth(_scollView.frame), CGRectGetHeight(_scollView.frame))];
//    [imageView2 setImage:[UIImage imageNamed:@"xiayang.jpg"]];
//    imageView2.contentMode = UIViewContentModeScaleAspectFit;
//    [_scollView addSubview:imageView2];
//    
#pragma mark - 利用for循环来添加图片
    for (int i = 0; i < 8; i ++) {
       
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*CGRectGetWidth(_scollView.frame), 0, CGRectGetWidth(_scollView.frame), CGRectGetHeight(_scollView.frame))];
        [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i+1]]];
            imageView.contentMode = UIViewContentModeScaleAspectFit;//设置图片视图的内容显示(这里设置为适合本身长宽比例)

        imageView.clipsToBounds = YES;//把超出父视图的部分剪切掉
        
        [_scollView addSubview:imageView];
    }
    
#pragma mark - 配置滚动视图
    [_scollView setContentSize:CGSizeMake(CGRectGetWidth(_scollView.frame)*8, CGRectGetHeight(_scollView.frame))]; //设置内容大小(能滚动的最大范围)
//    //设置偏移量
//    [_scollView setContentOffset:CGPointMake(1400, 50)];
    //设置指示器
    _scollView.showsHorizontalScrollIndicator = NO;//水平方向的指示器
    _scollView.showsVerticalScrollIndicator = NO; //垂直方向的指示器
    //设置按页滚动
    _scollView.pagingEnabled = YES;
    //设置滚动到边界的回弹效果
    _scollView.bounces = YES;
    _scollView.delegate = self;
    
    UILongPressGestureRecognizer * longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpressedGR:)];
    longGR.minimumPressDuration = 1;  //设置长按手势触发时间
    [self.view addGestureRecognizer:longGR];
#pragma mark - UIPageControl
    UIPageControl * pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];//他的大小影响用户交互的范围
    pageControl.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetWidth(self.view.bounds)+60);
    pageControl.numberOfPages = 8;
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];//页面控制器当前页面时的指示器的颜色
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
//  pageControl.currentPage = //该方法去scrollViewDidEndDecelerating里面写
    pageControl.tag = 101;
//    pageControl.backgroundColor = [UIColor grayColor];
    [pageControl addTarget:self action:@selector(pressedGR:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pageControl];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //设置偏移量
    [_scollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void)pressedGR:(UIPageControl *)sender
{
    [_scollView setContentOffset:CGPointMake(sender.currentPage * CGRectGetWidth(_scollView.frame), 0) animated:YES];
}

- (void)longpressedGR:(UILongPressGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan) {//手势的状态(这里设置为开始)
        //保存图片到相册
        UIActionSheet * sheet = [[UIActionSheet alloc]initWithTitle:@"提示" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"保存到相册" otherButtonTitles:@"分享", nil];
        [sheet showInView:self.view];
        UIImageWriteToSavedPhotosAlbum([UIImage imageNamed:@"1.png"], nil, nil, nil);
    }
}

#pragma mark -
//滚动视图滚动的时候被调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//检测视图偏移量
    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

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

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

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{

    NSLog(@"开始减速");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int index = scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame);
    UIView * view = [self.view viewWithTag:101];
    if ([view isKindOfClass:[UIPageControl class]]) {
        UIPageControl* pagecontrol = (UIPageControl *)view;//强转为UIPageControl
        pagecontrol.currentPage = index;
    }
    
    NSLog(@"减速完毕");
}


@end

你可能感兴趣的:(IOS-UI)