IOS:UI系列之UISCROLLVIEW和UIPAGECONTROL -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    转眼间,又是一天,就这样忙忙碌碌的一天一天的过着, 不过还好,不是浑浑噩噩的,也算是小有所成,劳有所获吧,嘿嘿!

    好了,到了总结的时间啦, 下面就为大家简单讲解下我今天学习的内容吧,希望对各位都有所帮助吧,同时也是对自己的一种激励,最终实现共赢吧 嘿嘿!

    首先,在上课时间我们先简单讲述了UIScrollView, 它是一个滚动视图,继承于UIView,他没有自己的初始化方法,所以要用到父类的创建方法下面就为大家简单说明下其创建过程哈:

    复制代码

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame.:self.view.bounds];

    scrollView.backgroundColor = [UIColor redColor];

    //设置内容页的大小,内容的大小必须比frame大,才能够滚动

    //如果不设置内容页的大小,默认和frame大小一致

    [scrollView setContentSize:CGSizeMake(320, 568 * 3)];

    //设置滚动条是否可见

    //水平滚动条

    scrollView.showsHorizontalScrollIndicator = YES;

    //竖直滚动条

    scrollView.showsVerticalScrollIndicator = YES;

    //设置内容页的偏移量, 默认内容页从(0, 0)点开始

    scrollView.contentOffset = CGPointMake(0, 0);

    //设置整页滑动

    scrollView.pagingEnabled = NO;

    //设置边界是否回弹

    scrollView.bounces = YES;

    //滚动到顶部

    scrollView.scrollsToTop = YES;

    //设置能否滚动

    scrollView.scrollEnabled = YES;

    //设置边界是否回弹,只有在content的宽度或者高度 小于 frame的宽度或者高度,才有效

    scrollView.alwaysBounceVertical = YES;

    scrollView.alwaysBounceHorizontal = YES;

    //缩放, 缩放必须结合着代理(delegate)才能生效, 指定scrollView上哪个视图进行缩放

    //设置当前的缩放比例, 默认为1.0

    scrollView.zoomScale = 1.0;

    //设置最大缩放比例

    scrollView.maximumZoomScale = 2.0;

    //设置最小缩放比例

    scrollView.minimumZoomScale = 0.5;

    //设置delegate

    scrollView.delegate = self;

    [self.view addSubview:scrollView];

    [scrollView release];

    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];

    imageView.frame. = CGRectMake(0, 0, 320, 568 * 3);

    [scrollView addSubview:imageView];

    [imageView release];

    复制代码

    其中delegate代理的常用方法如下:

    复制代码

    - (void)didReceiveMemoryWarning

    {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    //当滚动scrollView时调用

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

    NSLog(@"%s", __FUNCTION__);

    }

    //当发生了缩放的时候调用

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView

    {

    NSLog(@"%s", __FUNCTION__);

    }

    //对ScrollView中的哪个视图缩放

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

    {

    return imageView;

    }

    //将要开始拖拽的时候调用

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

    {

    NSLog(@"%s", __FUNCTION__);

    }

    //将要结束拖拽的时候调用

    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

    {

    NSLog(@"%s", __FUNCTION__);

    }

    //已经结束拖拽的时候调用

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

    {

    NSLog(@"%s", __FUNCTION__);

    }

    复制代码

    感觉UIScrollView是为接下来所讲的做了一个铺垫,以便于实现之后图片页码的切换和滚动

    在之后我们又讲了UIPageControl

    UIPageControl:页码控制器,继承与UIControl

    他和UIScrollView一样,也没有自己的初始化方法,同样要使用自己父类的方法,其具体创建方法如下所示

    复制代码

    - (void)viewDidLoad

    {

    [super viewDidLoad];

    //UIPageControl:页码控制器, 继承于UIControl

    UIPageControl *pageControl = [[UIPageControl alloc] init];

    pageControl.frame. = CGRectMake(0, 100, 320, 30);

    pageControl.backgroundColor = [UIColor blackColor];

    pageControl.numberOfPages = 10;

    //设置未选中的颜色

    pageControl.pageIndicatorTintColor = [UIColor yellowColor];

    //设置已选中的颜色

    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];

    //设置只有一页时隐藏

    pageControl.hidesForSinglePage = NO;

    //关联方法

    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:pageControl];

    [pageControl release];

    }

    - (void)didReceiveMemoryWarning

    {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    - (void)changePage:(UIPageControl *)pageControl

    {

    //页码是从零开始的

    NSLog(@"%d", pageControl.currentPage + 1);

    }

    复制代码

    下面我们来通过一个练习来进一步对这些空间作进一步的了解:

    通过页面的滚动和页码的切换来调用方法进而使效果类似于我们看电子书一样翻页跳转, 下面我就来附上代码供大家参考:

    复制代码

    - (void)changePage:(UIPageControl *)pageControl

    {

    if (pageControl.currentPage == 0) {

    [scrollView1 setContentOffset:CGPointMake(0, 0) animated:YES];

    } else if (pageControl.currentPage == 1) {

    [scrollView1 setContentOffset:CGPointMake(320, 0) animated:YES];

    } else if (pageControl.currentPage == 2) {

    [scrollView1 setContentOffset:CGPointMake(320 * 2, 0) animated:YES];

    }

    }

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

    {

    pageControl1.currentPage = scrollView.contentOffset.x / 320;

    }

    - (void)close:(UIButton *)button1

    {

    //如果子视图的父视图被移除,那么这个子视图也会被移除

    [button removeFromSuperview];

    [scrollView1 removeFromSuperview];

    [pageControl1 removeFromSuperview];

    }

    复制代码

    其中- (void)close:(UIButton *)button1 这个方法是用来跳转主页的, 就像我们在打开一款应用的时候上面都会显示一些新功能简介

最新文章