封裝分頁功能

來源:互聯網
上載者:User

標籤:

#import <UIKit/UIKit.h>

 

@interface LZJPageView : UIView

 

/** 圖片名資料 */

@property (nonatomic, strong) NSArray *imageNames;

+ (instancetype)pageView;

@end

 

 

 

 

 

#import "LZJPageView.h"

 

@interface LZJPageView () <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

 

 

/** 定時器 */

@property (nonatomic, weak) NSTimer *timer;

 

@end

@implementation LZJPageView

 

- (void)awakeFromNib

{

    // 1.單頁的時候是否隱藏pageControl

    self.pageControl.hidesForSinglePage = YES;

    

    // 2.設定pageControl的圖片

    [self.pageControl setValue:[UIImage imageNamed:@"current"] forKeyPath:@"_currentPageImage"];

    [self.pageControl setValue:[UIImage imageNamed:@"other"]  forKeyPath: @"_pageImage"];

    

    // 3.開啟定時器

    [self startTimer];

 

}

 

+ (instancetype)pageView

{

    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil]  lastObject];

}

 

- (void)setImageNames:(NSArray *)imageNames

{

    _imageNames = imageNames;

    // 0.移除之前添加的

    // 讓subviews這個數組中每一個對象都執行removeFromSuperview

    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    

    // 1.根據圖片名資料建立ImageView添加到scrollView

    CGFloat scrollViewW = self.scrollView.frame.size.width;

    CGFloat scrollViewH = self.scrollView.frame.size.height;

    NSUInteger count = imageNames.count;

    for (int i = 0; i < count; i ++) {

        UIImageView *imageView = [[UIImageView alloc] init];

        imageView.image = [UIImage imageNamed:imageNames[i]];

        imageView.frame = CGRectMake(i * scrollViewW, 0, scrollViewW, scrollViewH);

        [self.scrollView addSubview:imageView];

    }

    

    // 2.設定contentSize

    self.scrollView.contentSize = CGSizeMake(count * scrollViewW, 0);

    

    // 3.設定總頁數

    self.pageControl.numberOfPages = count;

}

 

#pragma mark - 定時器相關的代碼

- (void)startTimer

{

    // 返回一個自動執行的定時器對象

    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage:) userInfo:@"123" repeats:YES];

    

    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

}

 

- (void)stopTimer

{

    [self.timer invalidate];

}

 

/**

 *  滾動到下一頁

 */

- (void)nextPage:(NSTimer *)timer

{

    // 1.計算下一頁的頁碼

    NSInteger page = self.pageControl.currentPage + 1;

    

    // 2.超過了最後一頁

    if ( page == self.imageNames.count) {

        page = 0;

    }

    

    // 3.滾動到下一頁

    [self.scrollView setContentOffset:CGPointMake(page * self.scrollView.frame.size.width, 0) animated:YES];

}

 

#pragma mark - UIScrollViewDelegate

 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    // 1.計算頁碼

    int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);

    

    // 2.設定頁碼

    self.pageControl.currentPage = page;

}

 

/**

 *  使用者即將開始拖拽scrollView時,停止定時器

 */

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    [self stopTimer];

}

 

/**

 *  使用者已經停止拖拽scrollView時,開啟定時器

 */

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{

    [self startTimer];

}

 

@end

封裝分頁功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.