[IOS UIScrollView+PageControl]信息展示横幅
ScrollViewController.h
#import <UIKit/UIKit.h> @interface ScrollViewController : UIViewController<UIScrollViewDelegate,UIPageViewControllerDelegate> { UIScrollView *_scrollView; UIPageControl*_pageControl; } @end
ScrollViewController.m
#import "ScrollViewController.h" @interface ScrollViewController () @end @implementation ScrollViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; //设置ScrollView的整体触摸与显示区域 //注意 宽 高不要超过 320X480 //否则会出现无法滚动的情况 _scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320,120)] autorelease]; //设置ScrollView滚动内容的区域 //它通常是需要大于ScrollerView的显示区域的 //这样才有必要在ScrollerView中滚动它 [_scrollView setContentSize:CGSizeMake(320 * 5, 120)]; //开启滚动分页功能,如果不需要这个功能关闭即可 [_scrollView setPagingEnabled:YES]; //隐藏横向与纵向的滚动条 [_scrollView setShowsVerticalScrollIndicator:NO]; [_scrollView setShowsHorizontalScrollIndicator:NO]; //在本类中代理scrollView的整体事件 [_scrollView setDelegate:self]; //如果你打开横向或纵向的滚动条,这里可以设置滚动条的风格 // UIScrollViewIndicatorStyleDefault, 默认风格 // UIScrollViewIndicatorStyleBlack, 黑色风格 // UIScrollViewIndicatorStyleWhite 白色风格 //[_scrollView setIndicatorStyle:UIScrollViewIndicatorStyleBlack] for (int i =0; i<5; i++) { //在这里给每一个ScrollView添加一个图片 和一个按钮 UIImageView *imageView= [[[UIImageView alloc] initWithFrame:CGRectMake(i * 320,0,320,120)] autorelease]; [imageView setImage:[UIImage imageNamed:@"image.png"]]; UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(i * 320, 10, 100, 30); [button setTitle:@"这是一个按钮" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; //把每页需要显示的VIEW添加进ScrollerView中 [_scrollView addSubview:imageView]; [_scrollView addSubview:button]; } //整体再将ScrollerView显示在窗口中 [self.view addSubview:_scrollView]; //页面控制小工具 //它会在底部绘制小圆点标志当前显示页面 _pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(0,100,self.view.frame.size.width, 20)]autorelease]; //设置页面的数量 [_pageControl setNumberOfPages:5]; //监听页面是否发生改变 [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:_pageControl]; } - (void)changePage:(id)sender { //得到当前页面的ID //int page = [sender currentPage]; //在这里写你需要执行的代码 //...... } //手指离开屏幕后ScrollView还会继续滚动一段时间只到停止 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSLog(@"结束滚动后缓冲滚动彻底结束时调用"); } -(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { NSLog(@"结束滚动后开始缓冲滚动时调用"); } -(void)scrollViewDidScroll:(UIScrollView*)scrollView { //页面滚动时调用,设置当前页面的ID [_pageControl setCurrentPage:fabs(scrollView.contentOffset.x/self.view.frame.size.width)]; NSLog(@"视图滚动中X轴坐标%f",scrollView.contentOffset.x); NSLog(@"视图滚动中X轴坐标%f",scrollView.contentOffset.y); } -(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView { NSLog(@"滚动视图开始滚动,它只调用一次"); } -(void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate { NSLog(@"滚动视图结束滚动,它只调用一次"); } -(void)buttonClick { NSLog(@"按钮点击了"); } - (void)viewDidUnload { [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end
___________________________________________________
专注iOS/前端开发,广泛涉猎多种平台和技术,欢迎交流
可以在微博关注并@沈z伟