您的位置:首页 > 移动开发 > IOS开发

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: