iOS 自動迴圈播放廣告控制項實現

來源:互聯網
上載者:User

標籤:

1.原理:圖片:圖片集合名稱儲存成array;     

           輪播:為了實現滑動效果,至少需要三個imageview,為了確保左右滑動,需要一直顯示中間的view

           每次滑動後都需要重新載入三個view的圖片。並設定當前圖片下標

2.介面:提供給viewcontroller一個判斷左右滑動的介面,並實現滑動效果

            提供給pagecontroller一個設定頁面的介面,根據下標設定當前現實頁

3.代碼:ADScrollView,附詳細注釋

.h

@interface ADScrollView : UIScrollView {    NSTimer *_timer;}@property (nonatomic, retain) NSMutableArray *ads;          // 圖集@property (nonatomic, retain) NSMutableArray *images;       // 視圖集合(三個視圖,左中右)@property (nonatomic, assign) NSUInteger nowImageIndex;     // 當前圖片下表@property (nonatomic, assign) CGPoint         curPoint;     // 當前視圖座標- (void)changeToImage:(NSUInteger)index;                    // 改變到某個廣告,當我們點擊pagecontrol的某個頁時,將頁號作為參數竄過來,即可實現效果// 滑動後,將最後的座標點傳過來,即可判斷是左滑還是右滑,並實現滑動效果// 返回1:右滑   -1:左滑    0:未滑動- (NSInteger)scrollToPoint:(CGPoint)point;@end

.m

@implementation ADScrollView- (void)dealloc {    // 關閉定時器並釋放    [_timer invalidate];    _timer = nil;}- (id)init {    self = [super init];    if (self) {        self.ads = [NSMutableArray arrayWithObjects:@"ad01.png", @"ad02.png", @"ad03.jpg", nil];                // scroll的分頁設定        self.pagingEnabled = YES;        self.showsHorizontalScrollIndicator = NO;        self.showsVerticalScrollIndicator = NO;        self.contentSize = CGSizeMake(3 * 320, 100.0);                NSInteger kCount = [_ads count];        self.images = [[NSMutableArray alloc] initWithCapacity:3];        // 初始化三個切換頁        for (int i = 0; i < 3; i++) {            UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[_ads objectAtIndex:(i+kCount-1)%kCount]]];            view.frame = CGRectMake(i*320, 0, 320, 100);            [self addSubview:view];            [self.images addObject:view];        }        _nowImageIndex = 0;        self.bounds = CGRectMake(320, 0, 320, 100);        _curPoint = self.bounds.origin;   // 當前左上方座標                // 初始化定時器        _timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];        //self.adDelegate = nil;    }    return self;}#pragma mark - 定時器- (void)scrollTimer {    // 自動播放    [self scrollRectToVisible:CGRectMake(640, 0, 320, 100) animated:YES];}#pragma mark - 設定顯示第幾頁- (void)changeToImage:(NSUInteger)index {    _nowImageIndex = index;         // 當前視圖下標切換過去        // 設定三個view的顯示    self.bounds = CGRectMake(320, 0, 320, 100);    ((UIImageView *)[self.images objectAtIndex:2]).image = [UIImage imageNamed:[self.ads objectAtIndex:[self getImageNext]]];     // 載入下一張視圖    ((UIImageView *)[self.images objectAtIndex:1]).image = [UIImage imageNamed:[self.ads objectAtIndex:_nowImageIndex]];     // 載入當前視窗視圖    ((UIImageView *)[self.images objectAtIndex:0]).image = [UIImage imageNamed:[self.ads objectAtIndex:[self getImagePre]]];     // 載入上一個視窗視圖}#pragma mark - 判斷左滑 右滑 實現滑動效果- (NSInteger)scrollToPoint:(CGPoint)point {    NSInteger dictionary = 0;    if (point.x-_curPoint.x == 320.0 || point.x-_curPoint.x == -640.0) {  // 右滑        _nowImageIndex = [self getImageNext];    // 擷取當前圖片下標        dictionary = 1;    }    else if (0 != point.x - _curPoint.x){       // 左滑        _nowImageIndex = [self getImagePre];    // 擷取當前圖片下標        dictionary = -1;    }        // 始終顯示的是中間視圖,只不過各個視圖載入的圖片變了    self.bounds = CGRectMake(320, 0, 320, 100);    ((UIImageView *)[self.images objectAtIndex:2]).image = [UIImage imageNamed:[self.ads objectAtIndex:[self getImageNext]]];     // 載入下一張視圖    ((UIImageView *)[self.images objectAtIndex:1]).image = [UIImage imageNamed:[self.ads objectAtIndex:_nowImageIndex]];     // 載入當前視窗視圖    ((UIImageView *)[self.images objectAtIndex:0]).image = [UIImage imageNamed:[self.ads objectAtIndex:[self getImagePre]]];     // 載入上一個視窗視圖        return dictionary;       // 如果是右滑  返回yes  左滑返回no}#pragma mark - 擷取圖片下標- (NSInteger)getImageNext {    if (_nowImageIndex == [_ads count] - 1) {        return 0;    }    return _nowImageIndex + 1;}- (NSInteger)getImagePre {    if (0 == _nowImageIndex) {        return [_ads count]-1;    }    return _nowImageIndex-1;}@end

ViewController需要設定ADScrollView的委託,並實現兩個方法

// scrollview 滑動結束,響應手勢滑動處理- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {    NSInteger dic = [_adScroll scrollToPoint:scrollView.bounds.origin];    if (1 == dic) {     // 右滑        _page.currentPage = (_page.currentPage+1)%_page.numberOfPages;    }    else if (-1 == dic){  // 左滑        _page.currentPage = (_page.currentPage+_page.numberOfPages-1)%_page.numberOfPages;    }}// scrollview動作結束,響應自動輪播方式處理- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {    NSInteger dic = [_adScroll scrollToPoint:scrollView.bounds.origin];    if (1 == dic) {     // 右滑        _page.currentPage = (_page.currentPage+1)%_page.numberOfPages;    }    else if (-1 == dic){  // 左滑        _page.currentPage = (_page.currentPage+_page.numberOfPages-1)%_page.numberOfPages;    }}// uipageControl的回應程式法- (void)scrolTimer:(NSInteger)dictionary {    if (1 == dictionary) {        _page.currentPage = (_page.currentPage+_page.numberOfPages+1)%_page.numberOfPages;    }    else if(-1 == dictionary) {        _page.currentPage = (_page.currentPage+_page.numberOfPages-1)%_page.numberOfPages;    }    NSLog(@"%@",_adScroll);}

ps:只需要這些設定就可以實現廣告的輪播了,

對於廣告的大小可以根據實際情況自己改,

我這個是320*100的尺寸

iOS 自動迴圈播放廣告控制項實現

聯繫我們

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