UIScrollView + UIPageControl的使用,代码片段,可以直接贴哦
可是这些代码我还没有吸收,先用再说,哈哈哈
//.h
@interface RootViewController : UIViewController<UIScrollViewDelegate> //记得要引入代理哦
@property (nonatomic, strong) UIScrollView *helpScrView;
@property (nonatomic, strong) UIPageControl *pageCtrl;
- (void)viewDidLoad
{
CGRect bounds = self.view.frame; //获取界面区域
//加载蒙板图片,限于篇幅,这里仅显示一张图片的加载方法
UIImageView* imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height)];
[imageView1 setImage:[UIImage imageNamed:@"1.png"]];
imageView1.alpha = 0.5f;
//继续加载图片
UIImageView* imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(bounds.origin.x+320, bounds.origin.y, bounds.size.width, bounds.size.height)]; //创建UIImageView,位置大小与主界面一样。
[imageView2 setImage:[UIImage imageNamed:@"1.png"]]; //加载图片help01.png到imageView1中。
imageView2.alpha = 0.5f; //将透明度设为50%。
UIImageView* imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(bounds.origin.x+640, bounds.origin.y, bounds.size.width, bounds.size.height)]; //创建UIImageView,位置大小与主界面一样。
[imageView3 setImage:[UIImage imageNamed:@"1.png"]]; //加载图片help01.png到imageView1中。
imageView3.alpha = 0.5f; //将透明度设为50%。
_helpScrView = [[UIScrollView alloc] initWithFrame:CGRectMake(bounds.origin.x, bounds.origin.y+60, bounds.size.width, bounds.size.height)]; //创建UIScrollView,位置大小与主界面一样。
[_helpScrView setContentSize:CGSizeMake(bounds.size.width * 3, bounds.size.height)]; //设置全部内容的尺寸,这里帮助图片是3张,所以宽度设为界面宽度*3,高度和界面一致。
_helpScrView.pagingEnabled = YES; //设为YES时,会按页滑动
_helpScrView.bounces = NO; //取消UIScrollView的弹性属性,这个可以按个人喜好来定
[_helpScrView setDelegate:self]; //UIScrollView的delegate函数在本类中定义
_helpScrView.showsHorizontalScrollIndicator = NO; //因为我们使用UIPageControl表示页面进度,所以取消UIScrollView自己的进度条。
[_helpScrView addSubview:imageView1];
[_helpScrView addSubview:imageView2];
[_helpScrView addSubview:imageView3];
[self.view addSubview:_helpScrView];
//创建UIPageControl
_pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, bounds.size.height - 80, bounds.size.width, 30)];
_pageCtrl.numberOfPages = [_everyDisplayView count];//总的图片页数
_pageCtrl.currentPage = 0; //当前页
[_pageCtrl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; //用户点击UIPageControl的响应函数
[self.view addSubview:_pageCtrl]; //将UIPageControl添加到主界面上。
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//更新UIPageControl的当前页
CGPoint offset = scrollView.contentOffset;
CGRect bounds = scrollView.frame;
[_pageCtrl setCurrentPage:offset.x / bounds.size.width];
}
- (void)pageTurn:(UIPageControl*)sender
{
//令UIScrollView做出相应的滑动显示
CGSize viewSize = _helpScrView.frame.size;
CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);
[_helpScrView scrollRectToVisible:rect animated:YES];
}