UIScrollView基本用法和代理方法
- (
void
)viewDidLoad
{
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(320, 460*10);
[scrollView flashScrollIndicators];
scrollView.directionalLockEnabled = YES;
[self.view addSubview:scrollView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 40)];
label.backgroundColor = [UIColor yellowColor];
label.text = @
"学习scrolleview"
;
[scrollView addSubview:label];
[label release];
}
#pragma mark -
- (
BOOL
)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
return
YES;
}
- (
void
)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
NSLog(@
"scrollViewDidScrollToTop"
);
}
- (
void
)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@
"scrollViewDidScroll"
);
}
- (
void
)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@
"scrollViewWillBeginDragging"
);
}
- (
void
)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(
BOOL
)decelerate
{
NSLog(@
"scrollViewDidEndDragging"
);
}
- (
void
)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@
"scrollViewWillBeginDecelerating"
);
}
- (
void
)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@
"scrollViewDidEndDecelerating"
);
}
@interface RootViewController : UIViewController<UIScrollViewDelegate>
{
UIScrollView *_scrollView;
NSMutableArray *slideImages;
UIPageControl *_page;
}
@end
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if
(self) {
}
return
self;
}
- (
void
)viewDidLoad
{
[super viewDidLoad];
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 320, 240)];
_scrollView.bounces = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
_scrollView.contentOffset = CGPointMake(320, 0);
_scrollView.contentSize = CGSizeMake(1920,240);
_scrollView.showsVerticalScrollIndicator =NO;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.userInteractionEnabled = YES;
[self.view addSubview:_scrollView];
[_scrollView release];
slideImages = [[NSMutableArray alloc]initWithObjects:@
"1.jpeg"
,@
"2.jpg"
,@
"3.jpeg"
,@
"4.jpeg"
, nil];
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]];
imageView.frame = CGRectMake(0, 0, 320, 240);
[_scrollView addSubview:imageView];
[imageView release];
for
(
int
i = 0;i<[slideImages count];i++) {
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
imageView.frame = CGRectMake(320*i+320, 0, 320, 240);
imageView.userInteractionEnabled = YES;
[_scrollView addSubview:imageView];
[imageView release];
}
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];
imageView.frame = CGRectMake(320*5, 0, 320, 240);
[_scrollView addSubview:imageView];
[imageView release];
_page = [[UIPageControl alloc]initWithFrame:CGRectMake(240, 230, 70, 30)];
_page.numberOfPages = 4;
_page.currentPage = 0;
[_page addTarget:self action:@selector(pageAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_page];
[_page release];
}
- (
void
)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int
currentPage = (_scrollView.contentOffset.x - _scrollView.frame.size.width
/ ([slideImages count]+2)) / _scrollView.frame.size.width + 1;
NSLog(@
"%d"
,currentPage);
if
(currentPage==0) {
[_scrollView scrollRectToVisible:CGRectMake(320*4, 0, 320, 240) animated:NO];
}
else
if
(currentPage==([slideImages count]+1)) {
[_scrollView scrollRectToVisible:CGRectMake(320, 0, 320, 240) animated:NO];
}
}
- (
void
)scrollViewDidScroll:(UIScrollView *)sender
{
int
page = _scrollView.contentOffset.x/320-1;
_page.currentPage = page;
}
-(
void
)pageAction
{
int
page = _page.currentPage;
[_scrollView setContentOffset:CGPointMake(320 * (page+1), 0)];
}
- (
void
)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (
void
)dealloc {
[slideImages release];
[super dealloc];
}
@end