天天看點

iOS開發 -- 圖檔輪播(詳解)

一個簡單的圖檔輪播期小Demo,用了UIScrollView和UIPageControl的巧妙搭配.

能夠實作圖檔的輪播,用定時器(NSTimer)控制.當按住圖檔的不動的時候,計時器停止,當松開圖檔的時候計時器又開始.

能夠實作簡單的多線程.當滑動其他事件的時候,輪播事件不受影響. 差別了消息機制(NSRunloop)裡 預設屬性NSDefaultRunLoopMode(單線程) 和 另外一個屬性NSRunLoopCommonModes(多線程).

希望能夠幫助到一些人.

#import "RootViewController.h"

@interface RootViewController ()< UIScrollViewDelegate>
@property (nonatomic,retain) UIScrollView *scrollView;
@property (nonatomic,retain) UIPageControl *pageControl;
@property (nonatomic,strong)NSTimer *timer;
@end

@implementation RootViewController
-(void)dealloc{
    self.scrollView = nil;
    self.pageControl = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //整體布局
    [self layout];

}
           
#pragma  mark -- 布局
-(void)layout{
    //布局ScrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(,, , )];
    [self.view addSubview:_scrollView];
    [_scrollView release];
    //布局pagecontrol
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(, , , )];

    [self.view addSubview:_pageControl];
    [_pageControl release];

    int count = ;
    CGSize size = self.scrollView.frame.size;
    //1 動态生成5個imageView
    for (int i = ; i < count; i++) {
        //
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.scrollView addSubview:iconView];

        NSString *imgName = [NSString stringWithFormat:@"img_%02d",i+];
        iconView.image = [UIImage imageNamed:imgName];

        CGFloat x = i * size.width;
        //frame
        iconView.frame = CGRectMake(x, , size.width, size.height);
    }
    //2 設定滾動範圍
    self.scrollView.contentSize = CGSizeMake(count * size.width, );
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //3 設定分頁
    self.scrollView.pagingEnabled = YES;

    //4 設定pageControl
    self.pageControl.numberOfPages = count;
    self.pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    self.pageControl.pageIndicatorTintColor = [UIColor blackColor];
    //5 設定scrollView的代理
    self.scrollView.delegate = self;
    //6 添加定時器
    [self addTimerTask];

//添加測試View  當點選滑動textView時候 看看圖檔是否滑動  若是滑動則多線程成功 若是停止則是單線程
    UITextView *testTextView = [[UITextView alloc]initWithFrame:CGRectMake(, , , )];
    testTextView.text = @"添加測試View  當滑動textView時候 看看圖檔是否滑動  若是滑動則多線程成功 若是停止則是單線程 主要原因是消息機制裡 預設屬性NSDefaultRunLoopMode是單線程用的 是另外一個屬性NSRunLoopCommonModes能夠在多線程中起作用";
    testTextView.font = [UIFont fontWithName:@"Arial" size:];
    [self.view addSubview:testTextView];
    [testTextView release];
}
           
//把定時器封裝起來 友善調用
-(void)addTimerTask{
    //6 定時器
    NSTimer *timer = [NSTimer timerWithTimeInterval: target:self selector:@selector(nextImage) userInfo:nil repeats:YES];

    self.timer = timer;

    //消息循環
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    // 預設是NSDefaultRunLoopMode  但是另外一個屬性NSRunLoopCommonModes 能夠在多線程中起作用
    [runloop addTimer:timer forMode:NSDefaultRunLoopMode];

    //立即執行定時器的方法  fire 是定時器自帶的方法
    // [timer fire];

}
           
-(void)nextImage{
    //目前頁碼
    NSInteger page = self.pageControl.currentPage;
    //如果是到達最後一張
    if (page == self.pageControl.numberOfPages - ) {
        page = ;
        //設定偏移量  當到達最後一張時候 切換到第一張  不産生從最後一張倒回第一張效果
        _scrollView.contentOffset = CGPointMake(, );
        [_scrollView setContentOffset:_scrollView.contentOffset animated:YES];
    }else{
        page++;
    }
    //  self.scrollView setContentOffset:(CGPoint) animated:(BOOL)

    CGFloat offsetX = page * self.scrollView.frame.size.width;
    [UIView animateWithDuration: animations:^{
        self.scrollView.contentOffset = CGPointMake(offsetX, );
    }];
}
           
#pragma mark - - 實作ScrollView代理方法

//正在滾動的時候
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //   (offset.x + 100/2)/100
    int page = (scrollView.contentOffset.x + scrollView.frame.size.width / )/ scrollView.frame.size.width;
    self.pageControl.currentPage = page;
}
//當你點選圖檔按住不動的時候 把定時器停止
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//停止定時器
    [self.timer invalidate];
}
//當不再按圖檔 也就是松開的時候 立馬調用計時器方法
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    //用scheduledTimerWithTimeInterval 建立定時器是用的系統預設的方法 當遇見多線程時候會出現問題
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    //是以還是調用上面建立的定時器方法
    [self addTimerTask];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end