scollView和pageControlView

来源:互联网 发布:php调用网页代码 编辑:程序博客网 时间:2024/06/16 20:51
#import "RootViewController.h"#import "RootView.h"@interface RootViewController () <UIScrollViewDelegate>#pragma mark - 声明私有属性@property (nonatomic, retain) RootView *rootView;@end@implementation RootViewController#pragma mark - 设置自定义视图- (void)loadView{    self.rootView = [[[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];    self.view = _rootView;}- (void)viewDidLoad{    [super viewDidLoad];            // 设置滚动视图代理    _rootView.scrollView.delegate = self;        // 给小点点绑定事件    [_rootView.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];}#pragma mark - 小点点的事件- (void)pageControlAction:(UIPageControl *)sender{        CGPoint offset = CGPointMake(0, sender.currentPage * _rootView.scrollView.frame.size.height);//    _rootView.scrollView.contentOffset = offset;    [_rootView.scrollView setContentOffset:offset animated:YES];}#pragma mark - 停止滚动- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    NSInteger index = scrollView.contentOffset.y / scrollView.frame.size.height;    _rootView.pageControl.currentPage = index;    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark - 重写#pragma mark dealloc- (void)dealloc{    [_rootView release];        [super dealloc];}

#import "RootView.h"@implementation RootView- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.backgroundColor = [UIColor cyanColor];        [self addAllViews];    }    return self;}#pragma mark - 添加全部控件- (void)addAllViews{    // 1.添加滚动视图    self.scrollView = [[[UIScrollView alloc] initWithFrame:self.frame] autorelease];    [self addSubview:_scrollView];            // 2.添加小点点    self.pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(10, self.frame.size.height - 30, self.frame.size.width - 20, 20)] autorelease];    _pageControl.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5f];    [self addSubview:_pageControl];            // 添加图片到scrollView    NSInteger count = 9;        for (int i = 0; i < count; i++) {        NSString *imgName = [NSString stringWithFormat:@"bg_%d.JPG", i];        UIImage *image = [UIImage imageNamed:imgName];                UIImageView *imageView = [[UIImageView alloc] initWithImage:image];        imageView.frame = CGRectMake(0, i * _scrollView.frame.size.height, _scrollView.frame.size.width, _scrollView.frame.size.height);        [_scrollView addSubview:imageView];        [imageView release];    }        _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, _scrollView.frame.size.height * count);    _scrollView.pagingEnabled = YES;    _scrollView.showsVerticalScrollIndicator = NO;    _scrollView.showsHorizontalScrollIndicator = NO;        // 小点点个数    _pageControl.numberOfPages = count;}#pragma mark - 重写#pragma mark dealloc- (void)dealloc{    [_scrollView release];    [_pageControl release];        [super dealloc];}

0 0