
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
UINavigationController* nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
self.window.rootViewController = nav;
return YES;
}
#import "ViewController.h"
#define Width [UIScreen mainScreen].bounds.size.width
#define Heigth [UIScreen mainScreen].bounds.size.heigth
@interface ViewController ()<UIScrollViewDelegate>
@property (retain)UIScrollView* scrollView;
@property (retain)UIPageControl* pageControl;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake((Width-320)/2, 65, 320, 480)];
self.scrollView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.scrollView];
self.scrollView.delegate=self;
for (NSInteger i = 0; i < 4; i++) {
UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, 480)];
[self.scrollView addSubview:imageView];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"美女0%ld.jpg",i+1]];
self.automaticallyAdjustsScrollViewInsets = NO;
self.scrollView.contentSize = CGSizeMake(320*4, 480);
self.scrollView.bounces = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.pagingEnabled = YES;
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((Width-100)/2, 450+64, 100, 30)];
self.pageControl.backgroundColor = [UIColor clearColor];
self.pageControl.numberOfPages = 4;
self.pageControl.currentPage = 0;
[self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.pageControl];
}
UIButton* leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(0, 0, 60, 30);
leftBtn.backgroundColor = [UIColor grayColor];
[leftBtn setTitle:@"上一页" forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
UIButton* rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtn.frame = CGRectMake(0, 0, 60, 30);
rightBtn.backgroundColor = [UIColor grayColor];
[rightBtn setTitle:@"下一页" forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}
-(void)leftAction:(UIBarButtonItem*)sender
{
NSLog(@"上一页被点!!!!");
NSInteger page = self.pageControl.currentPage;
if (page > 0) {
page--;
[UIView animateWithDuration:0.5 animations:^{
self.pageControl.currentPage = page;
self.scrollView.contentOffset = CGPointMake(320*page, 0);
}];
}
}
-(void)rightAction:(UIBarButtonItem*)sender
{
NSLog(@"下一页被点!!!!");
NSInteger page = self.pageControl.currentPage;
if (page < 3) {
page++;
[UIView animateWithDuration:0.5 animations:^{
self.pageControl.currentPage = page;
self.scrollView.contentOffset = CGPointMake(320*page, 0);
}];
}
}
-(void)pageAction:(UIPageControl*)page
{
NSLog(@"%ld",(long)page.currentPage);
[UIView animateWithDuration:0.5 animations:^{
self.scrollView.contentOffset = CGPointMake(page.currentPage*320, 0);
}];
}
#pragma mark-UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"hehe");
CGPoint point = scrollView.contentOffset;
NSLog(@"%@",NSStringFromCGPoint(point));
NSInteger page = point.x/300;
self.pageControl.currentPage = page;
}