iOS開發UI篇—用純程式碼寫實現圖片輪播

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   ar   os   使用   

一、實現效果

實現圖片的自動輪播

二、實現代碼

  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)addTimer101 {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)removeTimer110 {111     [self.timer invalidate];112     self.timer =nil;113 }114 -(void)nextImage115 {116     //1.增加pageControl的頁碼117     int page = 0;118     if (_pageControl.currentPage ==TXImageCount-1) {119         page = 0;120     }121     else122     {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 *)scrollView134 {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)didReceiveMemoryWarning142 {143     [super didReceiveMemoryWarning];144     // Dispose of any resources that can be recreated.145 }146 /**147  *  停止拖拽時調用148  149  */150 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate151 {152     // 停止定時器(一旦定時器停止了,就不能再使用)153     [self addTimer];154 }155 156 /**157  *  開始拖拽的時候調用158  */159 -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView160 {161     // 停止定時器(一旦定時器停止了,就不能再使用)162     [self removeTimer];163 }164 165 166 167 @end

 

iOS開發UI篇—用純程式碼寫實現圖片輪播

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.