iOS UIPageControl使用


  940人阅读  评论(0)  收藏  举报
[html]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UIScrollViewDelegate>  
  4. {  
  5.     UIScrollView* helpScrView;  
  6.     UIPageControl* pageCtrl;  
  7. }  
  8. @end  
[html]  view plain copy
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad  
  10. {  
  11.     [super viewDidLoad];  
  12.     CGRect bounds = self.view.frame;  //获取界面区域  
  13.       
  14.     //加载蒙板图片,限于篇幅,这里仅显示一张图片的加载方法  
  15.     UIImageView* imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(0, bounds.origin.y, bounds.size.width, bounds.size.height)] autorelease];  //创建UIImageView,位置大小与主界面一样。  
  16.     [imageView1 setImage:[UIImage imageNamed:@"bg.png"]];  //加载图片help01.png到imageView1中。  
  17.     //imageView1.alpha = 0.5f;  //将透明度设为50%。  
  18.     UIImageView* imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(1*bounds.size.width, bounds.origin.y, bounds.size.width, bounds.size.height)] autorelease];  //创建UIImageView,位置大小与主界面一样。  
  19.     [imageView2 setImage:[UIImage imageNamed:@"bg.png"]];  
  20.     //继续加载图片  
  21.     //。。。。  
  22.       
  23.     //创建UIScrollView  
  24.     helpScrView = [[UIScrollView alloc] initWithFrame:CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, 300)];  //创建UIScrollView,位置大小与主界面一样。  
  25.     [helpScrView setContentSize:CGSizeMake(bounds.size.width * 6, 300)];  //设置全部内容的尺寸,这里帮助图片是3张,所以宽度设为界面宽度*3,高度和界面一致。  
  26.     helpScrView.pagingEnabled = YES;  //设为YES时,会按页滑动  
  27.     helpScrView.bounces = NO; //取消UIScrollView的弹性属性,这个可以按个人喜好来定  
  28.     [helpScrView setDelegate:self];//UIScrollView的delegate函数在本类中定义  
  29.     helpScrView.showsHorizontalScrollIndicator = NO;  //因为我们使用UIPageControl表示页面进度,所以取消UIScrollView自己的进度条。  
  30.     [helpScrView addSubview:imageView2];  
  31.     [helpScrView addSubview:imageView1];//将UIImageView添加到UIScrollView中。  
  32.     [self.view addSubview:helpScrView]; //将UIScrollView添加到主界面上。  
  33.       
  34.     //创建UIPageControl  
  35.     pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, bounds.size.width, 30)];  //创建UIPageControl,位置在屏幕最下方。  
  36.     pageCtrl.numberOfPages = 6;//总的图片页数  
  37.     pageCtrl.currentPage = 0; //当前页  
  38.     [pageCtrl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];  //用户点击UIPageControl的响应函数  
  39.     [self.view addSubview:pageCtrl];  //将UIPageControl添加到主界面上。  
  40. }  
  41.   
  42. //其次是UIScrollViewDelegate的scrollViewDidEndDecelerating函数,用户滑动页面停下后调用该函数。  
  43.   
  44.   
  45. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  
  46. {  
  47.     //更新UIPageControl的当前页  
  48.     CGPoint offset = scrollView.contentOffset;  
  49.     CGRect bounds = scrollView.frame;  
  50.     [pageCtrl setCurrentPage:offset.x / bounds.size.width];  
  51.     NSLog(@"%f",offset.x / bounds.size.width);  
  52. }  
  53.   
  54. //然后是点击UIPageControl时的响应函数pageTurn  
  55.   
  56.   
  57. - (void)pageTurn:(UIPageControl*)sender  
  58. {  
  59.     //令UIScrollView做出相应的滑动显示  
  60.     CGSize viewSize = helpScrView.frame.size;  
  61.     CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);  
  62.     [helpScrView scrollRectToVisible:rect animated:YES];  
  63. }  
  64. - (void)didReceiveMemoryWarning  
  65. {  
  66.     [super didReceiveMemoryWarning];  
  67.     // Dispose of any resources that can be recreated.  
  68. }  
  69.   
  70. @end  

修改PageControl d的小点点的图标

重写类!!

[html]  view plain copy
  1. //  
  2. //  GrayPageControl.h  
  3. //  iPlayer  
  4. //  
  5. //  Created by 屎壳郎情调 on 13-9-11.  
  6. //  Copyright (c) 2013年 ibokan. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface GrayPageControl : UIPageControl  
  12. {  
  13.     UIImage* activeImage;  
  14.     UIImage* inactiveImage;  
  15. }  
  16. @end  

[html]  view plain copy
  1. //  
  2. //  GrayPageControl.m  
  3. //  iPlayer  
  4. //  
  5. //  Created by 屎壳郎情调 on 13-9-11.  
  6. //  Copyright (c) 2013年 ibokan. All rights reserved.  
  7. //  
  8.   
  9. #import "GrayPageControl.h"  
  10.   
  11. @implementation GrayPageControl  
  12.   
  13. -(id) initWithFrame:(CGRect)frame  
  14. {  
  15.     self = [super initWithFrame:frame];  
  16.       
  17.     activeImage = [UIImage imageNamed:@"appleDot@2x"];  
  18.     inactiveImage = [UIImage imageNamed:@"pageDot@2x"] ;  
  19.       
  20.     return self;  
  21. }  
  22.   
  23. -(void) updateDots  
  24. {  
  25.     for (int i = 0; i < [self.subviews count]; i++)  
  26.     {  
  27.         UIImageView* dot = [self.subviews objectAtIndex:i];  
  28.         if (i == self.currentPage) dot.image = activeImage;  
  29.         else dot.image = inactiveImage;  
  30.     }  
  31. }  
  32.   
  33. -(void) setCurrentPage:(NSInteger)page  
  34. {  
  35.     [super setCurrentPage:page];  
  36.     //修改图标大小  
  37.     for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++) {  
  38.           
  39.         UIImageView* subview = [self.subviews objectAtIndex:subviewIndex];  
  40.           
  41.         CGSize size;  
  42.           
  43.         size.height = 10;  
  44.           
  45.         size.width = 10;  
  46.           
  47.         [subview setFrame:CGRectMake(subview.frame.origin.x, subview.frame.origin.y,  
  48.                                        
  49.                                      size.width,size.height)];  
  50.           
  51.     }  
  52.   
  53.       
  54.     [self updateDots];  
  55. }  
  56.   
  57. /*  
  58. // Only override drawRect: if you perform custom drawing.  
  59. // An empty implementation adversely affects performance during animation.  
  60. - (void)drawRect:(CGRect)rect  
  61. {  
  62.     // Drawing code  
  63. }  
  64. */  
  65.   
  66. @end  
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以使用 `UICollectionView` 或 `UIPageViewController` 来实现 iOS 图片轮播。 使用 `UICollectionView` 实现的方法如下: 1. 在您的视图控制器中,创建一个 `UICollectionView` 实例,并将其作为子视图添加到您的视图控制器的视图中。 2. 使用自定义布局实现循环滚动。可以参考以下代码: ``` class LoopCollectionViewFlowLayout: UICollectionViewFlowLayout { override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { guard let collectionView = collectionView else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity) } let collectionViewSize = collectionView.bounds.size let proposedContentOffsetCenterX = proposedContentOffset.x + collectionViewSize.width * 0.5 let proposedRect = CGRect(x: proposedContentOffset.x, y: 0, width: collectionViewSize.width, height: collectionViewSize.height) guard let layoutAttributes = layoutAttributesForElements(in: proposedRect) else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity) } let centerX = proposedContentOffsetCenterX let offset = CGPoint(x: proposedContentOffset.x + nearestTargetOffset(for: layoutAttributes, with: centerX), y: proposedContentOffset.y) return offset } private func nearestTargetOffset(for layoutAttributes: [UICollectionViewLayoutAttributes], with centerX: CGFloat) -> CGFloat { let targetAttributes = layoutAttributes.sorted { abs($0.center.x - centerX) < abs($1.center.x - centerX) } let nearestAttribute = targetAttributes.first return nearestAttribute?.center.x ?? 0 - centerX } } ``` 3. 创建自定义 `UICollectionViewCell` 类,并在其中添加一个 `UIImageView` 用于显示图片。 4. 实现 `UICollectionViewDataSource` 协议中的方法,用于设置图片数据源和自定义的 `UICollectionViewCell`。 5. 实现定时器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值