iOS UIPageView与UIScrollView
Posted jzdwajue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS UIPageView与UIScrollView相关的知识,希望对你有一定的参考价值。
在ios开发中,我们一定会用的类似欢迎界面的东西。当中须要翻页和以下的提示小圆点。
在iOS的实现中,翻页效果是使用UIScrollView。而UIPageView仅仅是加入以下的提示小圆点而已。
以下我贴出详细实现比較简单:
// // ADView.m // Ting3 // // Created by 杜甲 on 11/17/14. // Copyright (c) 2014 杜甲. All rights reserved. // #import "ADView.h" @interface ADView()<UIScrollViewDelegate> @property (nonatomic , strong) UIScrollView *adScrollView; @property (nonatomic , strong) UIPageControl *adPageControl; @end @implementation ADView - (id)initWithFrame:(CGRect)frame andImagePathArr:(NSArray *)pathArr { if (self = [super initWithFrame:frame]) { _adScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; _adScrollView.pagingEnabled = true; _adScrollView.bounces = false; _adScrollView.delegate = self; _adScrollView.contentSize = CGSizeMake(frame.size.width * pathArr.count, frame.size.height); [self addSubview:_adScrollView]; for (int i = 0; i < pathArr.count; i++) { UIImageView * adImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:pathArr[i]]]; adImageView.frame = CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height); [_adScrollView addSubview:adImageView]; } _adPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_adScrollView.frame) - 30, frame.size.width, 30)]; [_adPageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventAllEditingEvents]; _adPageControl.numberOfPages = pathArr.count; _adPageControl.currentPage = 0; [self addSubview:_adPageControl]; } return self; } - (void) pageTurn:(UIPageControl *)sender { CGSize viewSize = _adScrollView.frame.size; CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height); [_adScrollView scrollRectToVisible:rect animated:YES]; } - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGPoint offset = scrollView.contentOffset; CGRect bounds = scrollView.frame; _adPageControl.currentPage = offset.x / bounds.size.width; } @end
以上是关于iOS UIPageView与UIScrollView的主要内容,如果未能解决你的问题,请参考以下文章
在 UITableView 下方的 UIPageView 上检测滑动事件
如何最好地将使用一个 UIViewController 将所有页面作为 UIView 处理为 StoryBoarding 和 UIPageView 的图书应用程序转换?