標籤:
1>.h聲明屬性
@interface RootView : UIView@property (nonatomic, strong) UIScrollView *scrollView;@property (nonatomic, strong) UIPageControl *pageControl;
2>.m實現步驟
@implementation RootView- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // 添加子視圖 [self addAllViews]; } return self;}// 添加子視圖- (void)addAllViews{ // 布局scrollView // 1. 建立對象 self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame]; // 2. 定義屬性 // 蘋果4s分配的記憶體: 30W 5s: 80w 6s: 100; // 設定滾動範圍 self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.frame) * 7, 0); // 在scrollView上放上7張圖片 for (int i = 0; i < 7; i ++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame) * i, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))]; NSString *name = [NSString stringWithFormat:@"%d.png", i + 10]; imageView.image = [UIImage imageNamed:name]; // 將imageView添加到scrollView上 [self.scrollView addSubview:imageView]; } // 設定整頁滑動 self.scrollView.pagingEnabled = YES; // 3. 添加到父視圖 [self addSubview:self.scrollView]; // 布局pageControl self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame) - 40, CGRectGetWidth(self.frame), 40)]; self.pageControl.backgroundColor = [UIColor grayColor]; // 小圓點的個數 self.pageControl.numberOfPages = 7; // 當前小圓點的個數 self.pageControl.currentPageIndicatorTintColor = [UIColor redColor]; // 沒有被選擇的小圓點 self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor]; [self addSubview:self.pageControl]; }3>在ViewController裡設定UIScrollView的代理Delegate和給UIPageControl添加事件
@interface RootViewController ()<UIScrollViewDelegate>@property (nonatomic, strong) RootView *rootView;@end@implementation RootViewController- (void)loadView{ self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.view = self.rootView;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 4. 設定代理 self.rootView.scrollView.delegate = self; //給pageControl添加事件 [self.rootView.pageControl addTarget:self action:@selector(pageControlClick:) forControlEvents:UIControlEventValueChanged]; // 開始添加計時器,實現自動滾動 [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES]; }// 實現自動滾動- (void)timeAction:(NSTimer *)time{ NSLog(@"定時"); // 擷取當前pageControl(頁數) NSInteger index = self.rootView.pageControl.currentPage; // 如果不是最後一頁, 向後位移一頁 if (index != self.rootView.pageControl.numberOfPages - 1) { index++; } else if (index == self.rootView.pageControl.numberOfPages - 1){ // 如果是最後一頁,跳到第一頁 index = 0; } // 通過index修改scrollView的位移量// self.rootView.scrollView.contentOffset = CGPointMake(index * CGRectGetWidth(self.view.frame), 0); [self.rootView.scrollView setContentOffset:CGPointMake(index * CGRectGetWidth(self.view.frame), 0) animated:YES]; self.rootView.pageControl.currentPage = index;}// 實現點擊pageControl方法- (void)pageControlClick:(UIPageControl *)pageControl{ // 小圓點位置 NSInteger currentIndex = self.rootView.pageControl.currentPage; // self.rootView.scrollView.contentOffset = CGPointMake(currentIndex * self.view.frame.size.width, 0); [UIView animateWithDuration:0.5 animations:^{ self.rootView.scrollView.contentOffset = CGPointMake(currentIndex * self.view.frame.size.width, 0); }];}// 實現代理方法- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ // 改變pageCotrol的當前顯示的位置 // 擷取當前顯示的位置 CGFloat currentX = self.rootView.scrollView.contentOffset.x; self.rootView.pageControl.currentPage = currentX / self.view.frame.size.width; }
輪播圖的實現步驟