UIScrollView 實現廣告欄的無限輪播(可設置自動播放時間)

 

  有時需要設置一個廣告欄,那麼用UIScrollView是再好不過了,直接上代碼

1.首先在.h裏面設置屬性

//

//  FirstViewController.h

//  CyberGo

//

//  Created by chenzhizheng on 15/2/6.

//  Copyright (c) 2015 KLH. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface FirstViewController : UIViewController<UIScrollViewDelegate>

{

    int PageNum;

    BOOL Tend;


}


@property (nonatomic,strong) UIScrollView * home_images;


@property (nonatomic,strong) UICollectionView * home_collection;


@property (nonatomic,strong) UIPageControl * home_page;

@end


2.在.m裏面實現方法

//

//  FirstViewController.m

//  CyberGo

//

//  Created by chenzhizheng on 15/2/6.

//  Copyright (c) 2015 KLH. All rights reserved.

//


#import "FirstViewController.h"

#import "Common.h"


@interface FirstViewController ()


@end


@implementation FirstViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"底板.jpg"]];

    [self.view addSubview:[self home_images]];

    [self.view addSubview:[self home_page]];

    [self.view addSubview:[self home_collection]];

}

這裏是UIScrollView的getter方法

-(UIScrollView *)home_images

{

    if (!_home_images) {

        

        _home_images = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 100)];

        _home_images.indicatorStyle = UIScrollViewIndicatorStyleDefault;//設置滾動條風格

        _home_images.showsHorizontalScrollIndicator = NO;//設置是否顯示橫向滾動條

        _home_images.showsVerticalScrollIndicator = NO;//設置是否顯示縱向滾動條

        _home_images.bounces = YES;//是否具有反彈效果

        _home_images.pagingEnabled = YES;//是否可滑動翻頁

        _home_images.contentSize = CGSizeMake(WIDTH * 3, 0);//設置UIScrollView的大小(3張圖片大小)

        _home_images.delegate = self;//設置代理

        //視圖滾動循環

        for (int i =0; i < 3; i++) {

            UIImageView * view = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, 100)];

            view.image = [UIImage imageNamed:[NSString stringWithFormat:@"滾動圖%d.jpg",i+1]];

            [_home_images addSubview:view];

        }

        //設置定時器

        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(autoScrollimages:) userInfo:nil repeats:YES];

        

    }

    return _home_images;

}

-(void)autoScrollimages:(id)sender

{

    if (PageNum % 3 == 0) {

        if (!Tend) {

            self.home_page.currentPage++;

            if (self.home_page.currentPage == self.home_page.numberOfPages - 1) {

                Tend = YES;

            }

        }else{

            self.home_page.currentPage --;

            if (self.home_page.currentPage == 0) {

                Tend = NO;

            }

        }

        [UIView animateWithDuration:1.0f

                         animations:^{

                             self.home_images.contentOffset = CGPointMake(self.home_page.currentPage * 320, 0);

                         }];

    }

    PageNum ++;

}

//翻頁控制顯示器

-(UIPageControl *)home_page

{

    if (!_home_page) {

        //三個點的位置及大小

        _home_page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH * 0.75, 100 * 0.9, 40, 5)];

        _home_page.numberOfPages = 3;

        _home_page.currentPage = 0;//默認選中第一張展示

 

    }

    return _home_page;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end


照着敲run起來就好了


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章