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

ios-图片轮播器

时间:2015-08-12 13:16:29      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

//
//  ViewController.m
//  图片轮播器
//
//  Created by YaguangZhu on 15/8/12.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong)UIScrollView *scrollview;
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer;
@end

@implementation ViewController
- (UIScrollView *)scrollview
{
    if (_scrollview == nil) {
        _scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, 300, 130)];
        _scrollview.backgroundColor = [UIColor redColor];
        [self.view addSubview:_scrollview];
        
        _scrollview.showsHorizontalScrollIndicator = NO;
        _scrollview.showsVerticalScrollIndicator = NO;
        _scrollview.pagingEnabled = YES;
        self.scrollview.contentSize = CGSizeMake(5 * _scrollview.bounds.size.width, 0);
        
        _scrollview.delegate = self;
    }
    return _scrollview;
}
- (UIPageControl *)pageControl
{
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.numberOfPages = 5;
        CGSize size =[_pageControl sizeForNumberOfPages:5];
        _pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
        _pageControl.center = CGPointMake(self.view.center.x, 130);
        
        _pageControl.pageIndicatorTintColor = [UIColor blackColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        
        [self.view addSubview:_pageControl];
        
        [_pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _pageControl;
}

- (void)pageChange:(UIPageControl *)page
{
   // NSLog(@"%d",page.currentPage);
    CGFloat x = page.currentPage * self.scrollview.bounds.size.width;
    [self.scrollview setContentOffset:CGPointMake(x, 0) animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollview;
    for(int i= 0;i<5;i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"img_%02d",i+1];
        UIImage *image =[UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.scrollview.bounds];
        imageView.image = image;
        [self.scrollview addSubview:imageView];
        
    }
    
    [self.scrollview.subviews enumerateObjectsUsingBlock:^(UIImageView *imageview, NSUInteger idx, BOOL *stop) {
        CGRect frame = imageview.frame;
        frame.origin.x = idx * frame.size.width;
        imageview.frame = frame;
    }];
    
    self.pageControl.currentPage = 0;
    
    //时钟
    [self startTimer];

    // Do any additional setup after loading the view, typically from a nib.
}
- (void)startTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateTimer)  userInfo:nil  repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}
- (void)updateTimer
{
    int page = (self.pageControl.currentPage+1)%5;
    self.pageControl.currentPage = page;
    [self pageChange:self.pageControl];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int page = scrollView.contentOffset.x/scrollView.bounds.size.width;
    self.pageControl.currentPage =page;
}
//抓住图片停止时钟 放下开始
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    [self startTimer];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

ios-图片轮播器

标签:

原文地址:http://www.cnblogs.com/zhuyaguang/p/4723901.html

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