IOS scrollView以及pageControl使用
2012-12-23 21:20
344 查看
本文转自 http://www.999dh.net/article/iphone_ios_art/45.html 转载请注明,谢谢!
#import <UIKit/UIKit.h>
@interface CRViewController : UIViewController
{
UIScrollView * scrollView;
UIPageControl * pageControl;
}
@property(nonatomic,retain) UIScrollView * scrollView;
@property(nonatomic,retain) UIPageControl * pageControl;
@end
//
// CRViewController.m
// pageControl
//
// Created by chaoxiao zhuang on 12-12-22.
// Copyright (c) 2012年 taizhouxueyuan. All rights reserved.
//
#import "CRViewController.h"
@implementation CRViewController
@synthesize scrollView;
@synthesize pageControl;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(void)pageChanged:(UIPageControl*)page
{
int p = page.currentPage;
[scrollView scrollRectToVisible:CGRectMake(320*p, 0, 320, 480) animated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView*)scrollView
{
CGFloat pageWith = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWith/2)/pageWith)+1;
self.pageControl.currentPage = page;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollView.delegate = self;
[scrollView setContentSize:CGSizeMake(320*4, 480)];
//scrollView.showsHorizontalScrollIndicator = YES;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.pagingEnabled = YES;
[scrollView setBackgroundColor:[UIColor redColor]];
UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[view1 setBackgroundColor:[UIColor grayColor]];
[scrollView addSubview:view1];
UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(320, 0, 320, 480)];
[view2 setBackgroundColor:[UIColor blueColor]];
[scrollView addSubview:view2];
UIView * view3 = [[UIView alloc]initWithFrame:CGRectMake(320*2, 0, 320, 480)];
[view3 setBackgroundColor:[UIColor greenColor]];
[scrollView addSubview:view3];
UIView * view4 = [[UIView alloc]initWithFrame:CGRectMake(320*3, 0, 320, 480)];
[view4 setBackgroundColor:[UIColor yellowColor]];
[scrollView addSubview:view4];
[self.view addSubview:scrollView];
pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
pageControl.center = CGPointMake(160, 40);
pageControl.numberOfPages = 4;
[pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#import <UIKit/UIKit.h>
@interface CRViewController : UIViewController
{
UIScrollView * scrollView;
UIPageControl * pageControl;
}
@property(nonatomic,retain) UIScrollView * scrollView;
@property(nonatomic,retain) UIPageControl * pageControl;
@end
//
// CRViewController.m
// pageControl
//
// Created by chaoxiao zhuang on 12-12-22.
// Copyright (c) 2012年 taizhouxueyuan. All rights reserved.
//
#import "CRViewController.h"
@implementation CRViewController
@synthesize scrollView;
@synthesize pageControl;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(void)pageChanged:(UIPageControl*)page
{
int p = page.currentPage;
[scrollView scrollRectToVisible:CGRectMake(320*p, 0, 320, 480) animated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView*)scrollView
{
CGFloat pageWith = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWith/2)/pageWith)+1;
self.pageControl.currentPage = page;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollView.delegate = self;
[scrollView setContentSize:CGSizeMake(320*4, 480)];
//scrollView.showsHorizontalScrollIndicator = YES;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.pagingEnabled = YES;
[scrollView setBackgroundColor:[UIColor redColor]];
UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[view1 setBackgroundColor:[UIColor grayColor]];
[scrollView addSubview:view1];
UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(320, 0, 320, 480)];
[view2 setBackgroundColor:[UIColor blueColor]];
[scrollView addSubview:view2];
UIView * view3 = [[UIView alloc]initWithFrame:CGRectMake(320*2, 0, 320, 480)];
[view3 setBackgroundColor:[UIColor greenColor]];
[scrollView addSubview:view3];
UIView * view4 = [[UIView alloc]initWithFrame:CGRectMake(320*3, 0, 320, 480)];
[view4 setBackgroundColor:[UIColor yellowColor]];
[scrollView addSubview:view4];
[self.view addSubview:scrollView];
pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
pageControl.center = CGPointMake(160, 40);
pageControl.numberOfPages = 4;
[pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
相关文章推荐
- 28-scrollView以及pageControl,NSTimer的基本使用
- 【iOS开发-55】图片轮播案例:scrollView的分页、滚动条、利用代理控制定时器和Page Control以及多线程问题
- 31-使用scrollView以及pageControl,NSTimer实现图片轮播
- scrollView和pageControl的搭配使用
- IOS开发-UI学习-UIPageControl(页码控制器)的使用
- IOS学习 scrollView,pageControl,tableView在同一页面上处理
- IOS控件之ScrollView,PageControl---APP首次打开引导页面效果实现~
- ios菜鸟之路:uiscrollview和pagecontrol的使用方法
- iPhone开发笔记 (5) scrollView和pageControl的搭配使用
- pageControl与ScrollView的联合使用
- iOS customized PageControl show page number.自定义PageControl,使用页码代替dot
- IOS之PageControl和ScrollView搭配
- iOS中UIScrollView和UIPageControl以及NSTimer的综合使用
- IOS--UIPageControl使用
- IOS--UIPageControl的使用方法详细
- iPhone开发笔记 (5) scrollView和pageControl的搭配使用
- IOS系列——Scrollvoew和Pagecontrol的配套使用
- iOS--UIPageControl的使用方法详细
- IOS——使用ScrollView和PageController实现引导页分页效果
- iOS学习笔记---UIPageControl的简单使用(用于分页)