//
// UIScrollViewController.m
// AppUI组件学习
//
// Created by 麦子 on 15/6/18.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import "UIScrollViewController.h"
@interface UIScrollViewController ()
@end
@implementation UIScrollViewController{
UIPageControl *page;
UIScrollView *scroll;
CGFloat screenWidth;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
[self createView:self.view];
}
- (void)createView:(id)uiView{
UIView *view = (UIView *)uiView;
scroll = [[UIScrollView alloc] init];
screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
scroll.frame = [[UIScreen mainScreen] bounds];
scroll.backgroundColor = [UIColor grayColor];
// 加入视图进去
UIView *scrollView1 = [[UIView alloc] init];
scrollView1.frame = CGRectMake(0, 0, screenWidth, screenHeight);
scrollView1.backgroundColor = [UIColor blackColor];
[scroll addSubview:scrollView1];
UIView *scrollView2 = [[UIView alloc] init];
scrollView2.frame = CGRectMake(screenWidth, 0, screenWidth, screenHeight);
scrollView2.backgroundColor = [UIColor blueColor];
[scroll addSubview:scrollView2];
UIView *scrollView3 = [[UIView alloc] init];
scrollView3.frame = CGRectMake(screenWidth*2, 0, screenWidth, screenHeight);
scrollView3.backgroundColor = [UIColor purpleColor];
[scroll addSubview:scrollView3];
UIView *scrollView4 = [[UIView alloc] init];
scrollView4.frame = CGRectMake(screenWidth*3, 0, screenWidth, screenHeight);
scrollView4.backgroundColor = [UIColor redColor];
[scroll addSubview:scrollView4];
// 设置可以滚动 --- 设置这两个就可以滚动了
scroll.pagingEnabled = YES;
// 设置滚动范围
scroll.contentSize = CGSizeMake(screenWidth*4, screenHeight);
// 设置指定视图, 高度为0,才行。
scroll.contentOffset = CGPointMake(screenWidth*1, 0);
//页码
page = [[UIPageControl alloc] init];
page.frame = CGRectMake(0, screenHeight-100, screenWidth, 50);
page.numberOfPages = 4;
page.currentPageIndicatorTintColor = [UIColor redColor];
page.pageIndicatorTintColor = [UIColor blackColor];
[page addTarget:self action:@selector(valChange:) forControlEvents:UIControlEventTouchUpInside];
scroll.delegate = self;
// 组件放在上下,和顺序有关。 这里也是一个数组管理。栈一样
[view addSubview:scroll];
[view addSubview:page];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat width = scrollView.frame.size.width;
CGFloat x = scrollView.contentOffset.x;
int index = x/width;
page.currentPage = index;
NSLog(@"%d",index);
}
- (void)valChange:(UIPageControl *)pageView{
long count = pageView.currentPage;
[scroll setContentOffset:CGPointMake(count*screenWidth, 0) animated:true];
}
@end
UIScrollView
最新推荐文章于 2024-08-11 11:27:03 发布