MyPageView是自定义的UIView。实现效果如下。
1.MyPageView.h
#import <UIKit/UIKit.h> @interface MyPageView : UIView<UIScrollViewDelegate> @property(strong,nonatomic) UIScrollView* scrollView; @property (strong, nonatomic) IBOutlet UIPageControl *pageControl; -(void)initPageViewWithViewController:(UIViewController*)controller imagePaths:(NSArray*)imagePathArr; @end
2.MyPageView.m
#import "MyPageView.h" @implementation MyPageView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } -(void)initPageViewWithViewController:(UIViewController*)controller imagePaths:(NSArray*)imagePathArr{ NSInteger count = [imagePathArr count]; CGFloat height = self.frame.size.height; CGFloat width = self.frame.size.width; self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; self.scrollView.contentSize = CGSizeMake(width*count, height); self.scrollView.showsVerticalScrollIndicator = FALSE; self.scrollView.showsHorizontalScrollIndicator = FALSE; //important [self.scrollView setPagingEnabled:YES]; [self addSubview:self.scrollView]; CGFloat pageControlWidth = 10.0f * count; CGFloat pageControlHeight = 15.0f; self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((width - pageControlWidth) / 2, height - pageControlHeight, pageControlWidth, pageControlHeight)]; [self.pageControl setCurrentPage:0]; [self.pageControl setNumberOfPages:count]; [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged]; [self addSubview:self.pageControl]; NSInteger index = 0; for(NSString *imagePath in imagePathArr){ CGFloat xPos = index * width; UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, 0, width, height)]; imageView.image = [UIImage imageNamed:imagePath]; [self.scrollView addSubview:imageView]; index++; } //implement UIScrollViewDelegate self.scrollView.delegate = self; } - (void) scrollViewDidScroll: (UIScrollView *) aScrollView { CGFloat width = self.frame.size.width; CGPoint offset = aScrollView.contentOffset; self.pageControl.currentPage = offset.x / width; } //在点击pageControl时调用 - (IBAction)changePage:(id)sender { CGFloat width = self.frame.size.width; [UIView animateWithDuration:0.3f animations:^{ int whichPage = self.pageControl.currentPage; self.scrollView.contentOffset = CGPointMake(width * whichPage, 0.0f); }]; } @end
3.调用方法
MyPageView* pageView = [[MyPageView alloc] initWithFrame:CGRectMake(0,44.0f,320.0f,140.0f)]; NSMutableArray* arr = [NSMutableArray array]; [arr addObject:@"m1.png"]; [arr addObject:@"m2.png"]; [pageView initPageViewWithViewController:self imagePaths:arr]; [self.view addSubview:pageView];
相关推荐
上次上传的资源,现在才发现漏了说一点,要保证首尾两张的图片是同一张。。。。
ios代码,可以根据博客来详细了解http://blog.csdn.net/u010335966/article/details/8832940
UIScrollView+UIPageControl 核心步骤 在图片显示完全(endDecelerating)时,重新设置三个UIImageView的图片内容 调整UIScrollView的偏移量,始终显示中间的UIImageView 如有图片1、2、3、4、5,默认存放图片5、1、...
iOS UIScrollView and UIPageControl 导航控制器 demo
UIScrollView与UIPageControl的简单应用以及一些非常基本的使用方法
1.自动滑动图片,UIPageControl会随着图片滑动而改变; 2.用户可以手动切换图片,左右切换均可,当用户手动滑动图片时,图片停止自动切换; 3.实现了图片的循环播放; 4.UIPageControl可点击从而实现向左或右滑动,...
主要为大家详细介绍了swift实现自动轮播图效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
IOS UIScrollView + UIButton 实现页面和顶部标签页水平滚动效果,可以运用到实际项目中
iOS基础——通过案例学知识之LaunchScreen、APPIcon、StatusBar、UIScrollView、UIPageControl
基于UIScrollVIew 和UIPageControl的相册,相片可以放大缩小,当活动到其他页面,在返回时候放大或者缩小的相片恢复到原来的大小.
无缝可循环可点击的UIScrollVeiw+UIPageControl例子,不过没有用到重用机制,有需要的朋友可以拿去看看,要了5分,因为最近分比较紧缺,望体谅,没资源分或者没帐号的朋友可以发邮件给我,我传给你。tuxinxiang@...
利用UISrollView+UILabel 的方法实现表格的制作,实现的功能有: 1)以表格单元的形式显示数据; 2)可以左右、上下滑动表格。 适合IOS初学者。
UIScrollView+PullLoad
见标题,没有用到xib,开发环境是ios7
swipe fingers to left or right can change the image when you click the little dot at the right corner, you also can switch the image page by page.
ios swift用纯代码SnapKit框架AutoLayout约束设置滚动视图UIScrollView的教程代码
该Demo是UIScrollView和UIWebView结合EGO实现下拉刷新的demo
UIScrollView UIPageControl联合使用的demo,非常简单
UIScrollView分页滑动宽度自定义 更多欢迎关注:http://blog.csdn.net/u014220518
UIPageControl继承了UIControl基类,默认属于活动控件,它可以与用户进行交互...使用UIPageControl显示当前的UIScrollView正在显示第几页。 当用户点击UIPageControl控件时程序控制UIScrollView自动滚动到相应的页面。