iOS開發---輪播圖模組(普通版)

來源:互聯網
上載者:User

iOS開發---輪播圖模組(普通版)

//  用ScrollView實現圖片輪播//  ViewController.m//  Slider-輪播////  Created by JamesXiang on 15/7/21.//  Copyright (c) 2015年 JamesXiang. All rights reserved.//#import ViewController.h@interface ViewController () @property (nonatomic, strong) UIScrollView *scrollView;@property (nonatomic, strong) UIPageControl *pageControl;@property (nonatomic, assign) int sliderIndex;@property (nonatomic, strong) NSTimer *timer;@property (nonatomic, assign) int count;@property (nonatomic, assign) double sliderWidth;@property (nonatomic, assign) double sliderHeight;@end@implementation ViewController- (void)viewDidLoad {        [super viewDidLoad];        // 只留一個入口,程式解耦    [self loadSlider];    }- (void)loadSlider {        [self setParam]; // 設定相關參數        [self loadSliderContainer]; // 載入輪播容器        [self loadSliderItem]; // 載入輪播內容        [self loadPageController]; // 載入輪播        [self sliderBegin]; // 輪播開始    }- (void)setParam {        self.sliderIndex = 0; // 起始索引        self.count = 5; // 輪播圖片個數        self.sliderWidth = self.view.frame.size.width; // slider寬度        self.sliderHeight = 160; // slider高度    }// 設定輪播容器- (void)loadSliderContainer {        // scrollView可見寬度不能設定為內容的總寬度,會造成無法滾動        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, self.sliderWidth, self.sliderHeight)];        // 設定分頁滾動        scrollView.pagingEnabled = YES;        // 隱藏水平方向的捲軸        scrollView.showsHorizontalScrollIndicator = NO;        scrollView.delegate = self;        scrollView.contentSize = CGSizeMake(self.sliderWidth*self.count, self.sliderHeight);        self.scrollView = scrollView;        [self.view addSubview:scrollView];    }// 設定輪播器內容- (void)loadSliderItem {        for (int i = 0; i < self.count; i++) {                UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.sliderWidth * i, 0, self.sliderWidth, self.sliderHeight)];                imgView.userInteractionEnabled = YES;                // 為imageView添加手勢識別監聽器                [imgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderClick)]];                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@slider%d.jpg, i]];                imgView.image = img;                [self.scrollView addSubview:imgView];        //      以下代碼為用btn替換imageView,存在的問題是有時按鈕的點擊和滑動事件區分不開,導致可點擊時不可滑動的問題,具體解決方案還沒有想到                /*                 UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(self.sliderWidth * i, 0, self.sliderWidth, self.sliderHeight)];                btn.tag = i;                [btn addTarget:self action:@selector(sliderClick:) forControlEvents:UIControlEventTouchUpInside];                [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@slider%d.jpg, i]] forState:UIControlStateNormal];                // 取消點擊時的高亮狀態        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@slider%d.jpg, i]] forState:UIControlStateHighlighted];                [self.scrollView addSubview:btn];                  */        }    }- (void)sliderClick {    NSLog(@點擊事件...); // 這裡可以利用index來控制每個圖片的點擊事件}// 載入pageControl- (void)loadPageController {        UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 160, self.sliderWidth, 20)];        pageControl.numberOfPages = self.count;        pageControl.currentPage = 0;        pageControl.hidesForSinglePage = YES;        _pageControl = pageControl;        [self.view addSubview:pageControl];    }// 開始輪播- (void)sliderBegin {    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(autoChangeImg) userInfo:nil repeats:YES];}// 切換輪播圖片和按鈕- (void)autoChangeImg {        self.sliderIndex++;        if (self.sliderIndex == 5) {                self.sliderIndex = 0;            }        [UIView animateWithDuration:0.8 animations:^{                self.scrollView.contentOffset = CGPointMake(self.sliderIndex * self.sliderWidth, 0);                [self updateSliderIndex];            }];    }// 更新當前輪播索引- (void)updateSliderIndex {            self.sliderIndex = self.scrollView.contentOffset.x / self.sliderWidth;        self.pageControl.currentPage = self.sliderIndex;    }- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {        [self.timer invalidate];    }- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {        [self sliderBegin];    }- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {        [self updateSliderIndex];    }@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.