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

iOS开发UI篇—用纯代码写实现图片轮播

时间:2014-11-02 17:51:32      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   使用   

一、实现效果

实现图片的自动轮播

bubuko.com,布布扣

二、实现代码

  1 //  手写图片轮播器
  2 //
  3 //  Created by 鑫 on 14-10-9.
  4 //  Copyright (c) 2014年 梁镋鑫. All rights reserved.
  5 //
  6 #define TXImageCount 5
  7 
  8 #import "TXViewController.h"
  9 
 10 
 11 @interface TXViewController ()<UIScrollViewDelegate>
 12 {
 13     UIScrollView *_scrollView;
 14     UIPageControl *_pageControl;
 15 }
 16 @property(nonatomic ,strong)NSTimer *timer;
 17 
 18 @end
 19 
 20 @implementation TXViewController
 21 
 22 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 23 {
 24     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 25     if (self) {
 26         // Custom initialization
 27     }
 28     return self;
 29 }
 30 
 31 - (void)viewDidLoad
 32 {
 33     [super viewDidLoad];
 34     
 35     
 36     _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 85, 300, 130)];
 37     
 38     //一些固定参数
 39     CGFloat imageW = _scrollView.frame.size.width;
 40     CGFloat imageH = _scrollView.frame.size.height;
 41     CGFloat imageY = 0;
 42     _scrollView.backgroundColor = [UIColor blackColor];
 43     [self.view addSubview:_scrollView];
 44     
 45     _scrollView.delegate = self;
 46     //添加五张图片到scrollview中
 47     for (int i = 0; i<TXImageCount; i++) {
 48         UIImageView *imageView = [[UIImageView alloc] init];
 49         
 50         // 设置frame
 51         CGFloat imageX = i * imageW;
 52         imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
 53         
 54         // 设置图片
 55        NSString *name = [NSString stringWithFormat:@"img_0%d", i + 1];
 56         imageView.image = [UIImage imageNamed:name];
 57         [_scrollView addSubview:imageView];
 58         
 59         
 60         //添加定时器
 61         
 62         [self addTimer];
 63         
 64     
 65     
 66     }
 67     //添加分页点
 68     _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(107, 128, 106, 37)];
 69     _pageControl.backgroundColor=[UIColor orangeColor];
 70     
 71     //设置pageControl的总页数
 72     
 73     _pageControl.numberOfPages = TXImageCount;
 74     
 75     [_pageControl addTarget:self action:@selector(changPage) forControlEvents:UIControlEventValueChanged];
 76     [self.view addSubview:_pageControl];
 77     //设置内容尺寸
 78     CGFloat contentW =TXImageCount *imageW;
 79     _scrollView.contentSize = CGSizeMake(contentW, 0);
 80     
 81     //隐藏水平滚动条
 82     _scrollView.showsHorizontalScrollIndicator =NO;
 83     
 84     //分页
 85     _scrollView.pagingEnabled = YES;
 86     
 87     
 88 }
 89 /**
 90  *  点击pageControl改变scrollView的页面
 91  */
 92 -(void)changPage
 93 {
 94     CGPoint offest=CGPointMake(_pageControl.currentPage*_scrollView.frame.size.width, 0);
 95     [_scrollView setContentOffset:offest animated:YES];
 96 }
 97 /**
 98  *  添加定时器
 99  */
100 -(void)addTimer
101 {
102      self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
103     //提高优先级
104     [[NSRunLoop currentRunLoop ] addTimer:self.timer forMode:NSRunLoopCommonModes];
105 }
106 /**
107  *  移除定时器
108  */
109 -(void)removeTimer
110 {
111     [self.timer invalidate];
112     self.timer =nil;
113 }
114 -(void)nextImage
115 {
116     //1.增加pageControl的页码
117     int page = 0;
118     if (_pageControl.currentPage ==TXImageCount-1) {
119         page = 0;
120     }
121     else
122     {
123         page = _pageControl.currentPage +1;
124     }
125     //计算scrolview滚动的位置
126     CGFloat offsetX = page *_scrollView.frame.size.width;
127     CGPoint offset = CGPointMake(offsetX, 0);
128     [_scrollView setContentOffset:offset animated:YES];
129     
130 }
131 #pragma mark  --代理方法
132 
133 -(void)scrollViewDidScroll:(UIScrollView *)scrollView
134 {
135     //根据scrollview的滚动位置决定pageControl显示第几页
136     CGFloat scrollW = _scrollView.frame.size.width;
137     
138     int page = (_scrollView.contentOffset.x+scrollW *0.5)/scrollW;
139     _pageControl.currentPage =page;
140 }
141 - (void)didReceiveMemoryWarning
142 {
143     [super didReceiveMemoryWarning];
144     // Dispose of any resources that can be recreated.
145 }
146 /**
147  *  停止拖拽时调用
148  
149  */
150 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
151 {
152     // 停止定时器(一旦定时器停止了,就不能再使用)
153     [self addTimer];
154 }
155 
156 /**
157  *  开始拖拽的时候调用
158  */
159 -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
160 {
161     // 停止定时器(一旦定时器停止了,就不能再使用)
162     [self removeTimer];
163 }
164 
165 
166 
167 @end

 

iOS开发UI篇—用纯代码写实现图片轮播

标签:des   style   blog   http   io   color   ar   os   使用   

原文地址:http://www.cnblogs.com/asd5551680/p/4069512.html

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