iOS開發UI篇----UI基礎之美女輪播器(自動輪播)2

//
//  ViewController.m
//  圖片輪播器-(自動)
//
//  Created by rms on 15/11/28.
//  Copyright © 2015年 rms. All rights reserved.
//

#import "ViewController.h"
#define kIMAGECOUNT 8

@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,strong) UIImageView *leftImageView;
@property(nonatomic,strong) UIImageView *centerImageView;
@property(nonatomic,strong) UIImageView *rightImageView;
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer;

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:scrollView];
    
    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, height - 100, width, 20)];
    pageControl.numberOfPages = kIMAGECOUNT;
    
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    pageControl.pageIndicatorTintColor = [UIColor blackColor];
    [self.view addSubview:pageControl];
    
    self.pageControl = pageControl;
    
    scrollView.contentSize = CGSizeMake(width * (kIMAGECOUNT + 2), 0);
   
    scrollView.contentOffset = CGPointMake(width, 0);
    scrollView.pagingEnabled = YES;
    
    self.scrollView = scrollView;
    scrollView.delegate = self;
    //31231
    
    [self imageViewWithFrame:CGRectMake(0, 0, width, height) imageName:[NSString stringWithFormat:@"%02d",kIMAGECOUNT]];
    [self imageViewWithFrame:CGRectMake(width * (kIMAGECOUNT + 1), 0, width, height) imageName:@"01.jpg"];
    
    for (int i = 1; i < kIMAGECOUNT + 1; i++) {
        [self imageViewWithFrame:CGRectMake(width * i, 0, width, height) imageName:[NSString stringWithFormat:@"%02d",i]];
    }
    
    
    [self startTimer];
}

-(UIImageView *)imageViewWithFrame:(CGRect)frame imageName:(NSString *)imageName{

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame];
    imageView.image = [UIImage imageNamed:imageName];
    
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    imageView.clipsToBounds = YES;
    [self.scrollView addSubview:imageView];
    return imageView;
}


/開啓一個全新的定時器
- (void) startTimer
{
    //  定時器
    //  scheduled 調度(執行)
    //   Interval 間隔的時間 (s)
    //  target 目標(調用誰的方法)
    //  selector 方法(調用哪個方法)
    //  repeats 是否須要重複執行
//  作了兩件事 1.建立NSTimer的對象
//           2。把這個NSTimer以默認形式添加到了主運行循環中 (默認優先級低於事件處理的優先級)
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    
// 1.建立NSTimer對象
    NSTimer *timer  = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
// 2.添加到主運行循環中
//  Run 運行 Loop 循環
//  NSDefaultRunLoopMode 默認模式(低於事件處理優先級)
//  NSRunLoopCommonModes 通用模式 (於事件處理的優先級相同)
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
// 3.記錄當前的timer
    self.timer = timer;
}


-(void)nextImage{

    NSInteger page = self.pageControl.currentPage;
    
    if (page == self.pageControl.numberOfPages - 1) {
        
        page = 0;
//        [UIView animateWithDuration:1 animations:^{
            self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * (page+1), 0);
//        }];


    }else{
        page++;
        [UIView animateWithDuration:1 animations:^{
            self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * (page+1), 0);
        }];

        
    }
    
}

- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //  中止定時器
    //  讓定時器失效,一旦失效就不能在使用了。
    [self.timer invalidate];
}
/**
 *  當用戶的手指從scrollViwew上擡起的時候執行這個方法
 */
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //  從新開始調度
    [self startTimer];
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGPoint offset = scrollView.contentOffset;
    int page = round(offset.x / scrollView.frame.size.width);
    NSLog(@"%d",page);
    self.pageControl.currentPage = page - 1;
    if (page == kIMAGECOUNT + 1) {

        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);
    }
    if(page == 0){
    
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * kIMAGECOUNT, 0);
    }
}

@end
相關文章
相關標籤/搜索