輪播圖的實現步驟

來源:互聯網
上載者:User

標籤:

1>.h聲明屬性
@interface RootView : UIView@property (nonatomic, strong) UIScrollView *scrollView;@property (nonatomic, strong) UIPageControl *pageControl;
2>.m實現步驟
@implementation RootView- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // 添加子視圖        [self addAllViews];    }    return self;}// 添加子視圖- (void)addAllViews{    // 布局scrollView    // 1. 建立對象    self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];    // 2. 定義屬性    // 蘋果4s分配的記憶體: 30W 5s: 80w 6s: 100;    // 設定滾動範圍    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.frame) * 7, 0);    // 在scrollView上放上7張圖片    for (int i = 0; i < 7; i ++) {        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame) * i, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];                NSString *name = [NSString stringWithFormat:@"%d.png", i + 10];                        imageView.image = [UIImage imageNamed:name];                // 將imageView添加到scrollView上        [self.scrollView addSubview:imageView];                    }    // 設定整頁滑動    self.scrollView.pagingEnabled = YES;        // 3. 添加到父視圖        [self addSubview:self.scrollView];        // 布局pageControl    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame) - 40, CGRectGetWidth(self.frame), 40)];    self.pageControl.backgroundColor = [UIColor grayColor];    // 小圓點的個數    self.pageControl.numberOfPages = 7;    // 當前小圓點的個數    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];    // 沒有被選擇的小圓點    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];    [self addSubview:self.pageControl];    }
3>在ViewController裡設定UIScrollView的代理Delegate和給UIPageControl添加事件
@interface RootViewController ()<UIScrollViewDelegate>@property (nonatomic, strong) RootView *rootView;@end@implementation RootViewController- (void)loadView{    self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.view = self.rootView;}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.        // 4. 設定代理    self.rootView.scrollView.delegate = self;        //給pageControl添加事件    [self.rootView.pageControl addTarget:self action:@selector(pageControlClick:) forControlEvents:UIControlEventValueChanged];        // 開始添加計時器,實現自動滾動    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];    }// 實現自動滾動- (void)timeAction:(NSTimer *)time{    NSLog(@"定時");    // 擷取當前pageControl(頁數)    NSInteger index = self.rootView.pageControl.currentPage;    // 如果不是最後一頁, 向後位移一頁    if (index != self.rootView.pageControl.numberOfPages - 1) {                index++;    } else if (index == self.rootView.pageControl.numberOfPages - 1){        // 如果是最後一頁,跳到第一頁        index = 0;    }     // 通過index修改scrollView的位移量//    self.rootView.scrollView.contentOffset = CGPointMake(index * CGRectGetWidth(self.view.frame), 0);    [self.rootView.scrollView setContentOffset:CGPointMake(index * CGRectGetWidth(self.view.frame), 0) animated:YES];    self.rootView.pageControl.currentPage = index;}// 實現點擊pageControl方法- (void)pageControlClick:(UIPageControl *)pageControl{        // 小圓點位置    NSInteger currentIndex = self.rootView.pageControl.currentPage;    //    self.rootView.scrollView.contentOffset = CGPointMake(currentIndex * self.view.frame.size.width, 0);        [UIView animateWithDuration:0.5 animations:^{            self.rootView.scrollView.contentOffset = CGPointMake(currentIndex * self.view.frame.size.width, 0);    }];}// 實現代理方法- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    // 改變pageCotrol的當前顯示的位置       // 擷取當前顯示的位置    CGFloat currentX = self.rootView.scrollView.contentOffset.x;    self.rootView.pageControl.currentPage = currentX / self.view.frame.size.width;    }

 

輪播圖的實現步驟

聯繫我們

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