码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发UI篇----UI基础之美女轮播器(自动轮播)2

时间:2015-11-28 13:28:48      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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

 

iOS开发UI篇----UI基础之美女轮播器(自动轮播)2

标签:

原文地址:http://www.cnblogs.com/DoNib-Coding/p/5002500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!