iOS UIPageView與UIScrollView

來源:互聯網
上載者:User

標籤:fse   uiimage   led   ddt   sar   scroll   mic   ide   height   

在iOS開發中,我們一定會用的類似歡迎介面的東西。當中須要翻頁和以下的提示小圓點。

在iOS的實現中,翻頁效果是使用UIScrollView。而UIPageView僅僅是加入以下的提示小圓點而已。

以下我貼出詳細實現比較簡單:

////  ADView.m//  Ting3////  Created by 杜甲 on 11/17/14.//  Copyright (c) 2014 杜甲. All rights reserved.//#import "ADView.h"@interface ADView()<UIScrollViewDelegate>@property (nonatomic , strong) UIScrollView *adScrollView;@property (nonatomic , strong) UIPageControl *adPageControl;@end@implementation ADView- (id)initWithFrame:(CGRect)frame andImagePathArr:(NSArray *)pathArr{    if (self = [super initWithFrame:frame]) {                       _adScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];        _adScrollView.pagingEnabled = true;        _adScrollView.bounces = false;        _adScrollView.delegate = self;        _adScrollView.contentSize = CGSizeMake(frame.size.width * pathArr.count, frame.size.height);               [self addSubview:_adScrollView];                             for (int i = 0; i < pathArr.count; i++) {            UIImageView * adImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:pathArr[i]]];            adImageView.frame = CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height);            [_adScrollView addSubview:adImageView];        }                                _adPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_adScrollView.frame) - 30, frame.size.width, 30)];        [_adPageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventAllEditingEvents];                _adPageControl.numberOfPages = pathArr.count;        _adPageControl.currentPage = 0;        [self addSubview:_adPageControl];                            }    return self;}- (void) pageTurn:(UIPageControl *)sender{    CGSize viewSize = _adScrollView.frame.size;    CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);    [_adScrollView scrollRectToVisible:rect animated:YES];    }- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    CGPoint offset = scrollView.contentOffset;    CGRect bounds = scrollView.frame;    _adPageControl.currentPage = offset.x / bounds.size.width;            }@end


iOS UIPageView與UIScrollView

聯繫我們

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