iOS开发 UI UIScrollView和UIPageController

来源:互联网 发布:方阵和矩阵的区别 编辑:程序博客网 时间:2024/05/21 00:19

UIScrollView:滚轴视图

为了显示多于一个屏幕的内容或者超过你能放在内存中的内容。

                 Scroll View为你处理缩小放大手势,UIScrollView实现了这些手势,并且替你处理对于它们的探测和回应。其中需要注意的子类是UITableView以及UITextView(用来显示大量的文字)。还有一个UIWebView,尽管那不是UIScrollView的直接子类,它适用UIScrollView去显示网页内容

        contentsize是内容的宽和高,contentsize.width是内容的宽度,contentsize.heght是高度,contentsizeUIScrollView的一个属性,它是一个CGSize,是由核心图形所定义的架构,那定义了你可以滚轴内容的宽度和高度,你也可以添加可以上下滚动的额外区域。第一种方法是你可以通过添加内容的大小来完成。另外一个比较动态的选择是UIScrollView的另一个属性contentInsetcontentInset增加你在contentsize中指定的内容能够滚动的上下左右区域数量contentInset.top以及contentInset.buttom分别表示上面和下面的距离。

在滚轴视图中,有一个叫做ContentOffset的属性跟踪UIScrollView的具体位置,你能够自己获取和设置它,ContentOffset是你当前可视内容在滚轴视图边界的左上角那个点ContentOffset内容中的那个点不是从contentInset的左上角开始的,而是内容的左上角,此时的ContentOffset是正值,但有时也是负值


UIPageController:

它主要的功能是实现视图分页,有点类似于网页上的分页功能

现在用UIPageController和UIScrollView写一个简单轮播图的代码

1.创建文件,RootviewController,RootView。

2.在RootView.h中申明page,Scroll的属性,在RootView.m中设置

#import "RootView.h"
//宏定义
#define SWidth CGRectGetWidth([UIScreen mainScreen].bounds)
#define SHeight CGRectGetHeight([UIScreen mainScreen].bounds)
@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView{
    self.backgroundColor = [UIColor orangeColor];
    
    self.sv = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    //设置包含空间的大小
    self.sv.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width*8, CGRectGetHeight([UIScreen mainScreen].bounds));
    //设置整页滑动
    self.sv.pagingEnabled = YES;
    self.sv.backgroundColor = [UIColor blueColor];
    //回弹
    self.sv.bounces = NO;
    //设置缩放倍数
    self.sv.maximumZoomScale = 2;
    self.sv.minimumZoomScale = 1;
    //打开竖向和横向的滚动条
    self.sv.showsHorizontalScrollIndicator = YES;
    self.sv.showsVerticalScrollIndicator = YES;
    [self addSubview:self.sv];
    
   
    [self addImages:self.sv];
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds) - 50, CGRectGetWidth([UIScreen mainScreen].bounds), 50)];
    //设置页码数量
    self.page.numberOfPages = 8;
    //指定第二页
    self.page.currentPage = 2;
    //圆点的颜色
    self.page.pageIndicatorTintColor = [UIColor cyanColor];
    //添加到视图上
    [self addSubview:self.page];
    
}


- (void)addImages:(UIView *)view{
    for (int i = 0; i < 8; i++) {
        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(i * SWidth, 0, SWidth, SHeight)];
        
        iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpeg",i]];
        [view addSubview:iv];
    }
}
3.再在主函数中实现响应事件

#import "RootViewController.h"
#define SWidth CGRectGetWidth([UIScreen mainScreen].bounds)
#define SHeight CGRectGetHeight([UIScreen mainScreen].bounds)
@interface RootViewController ()<UIScrollViewDelegate>

@end

@implementation RootViewController
- (void)loadView{
    self.rv = [[RootView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    self.view = self.rv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //接受协议,指定代理
    self.rv.sv.delegate = self;
    
    //给Page添加响应事件
    [self.rv.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
}

- (void)pageAction:(UIPageControl *)page{
    self.rv.sv.contentOffset = CGPointMake(SWidth * page.currentPage, 0);
}

//当减速完成的时候会执行这个代码
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int page = scrollView.contentOffset.x / SWidth;
    self.rv.page.currentPage = page;
}




0 0