#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
RootViewController *rootVC = [[RootViewControlleralloc] init];
self.window.rootViewController = rootVC;
[rootVCrelease];
return YES;
}
//
// RootViewController.h
// UI-Homework-7
//
// Created by lanouhn on 15-8-5.
// Copyright (c) 2015年 尹江涛-UIScrollView-UIPageController. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "RootView.h"
@interface RootViewController :UIViewController
@property (nonatomic ,retain)RootView *rootView;
@end
//
// RootViewController.m
// UI-Homework-7
//
// Created by lanouhn on 15-8-5.
// Copyright (c) 2015年 尹江涛-UIScrollView-UIPageController. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()<UIScrollViewDelegate>
@end
@implementation RootViewController
- (void)loadView
{
[superloadView];
self.rootView = [[RootViewalloc] initWithFrame:[UIScreenmainScreen].bounds];
self.view =self.rootView;
}
- (void)viewDidLoad {
[superviewDidLoad];
//添加点击事件
//scrollView代理
self.rootView.scrollView.delegate = self;
//pageControl
[self.rootView.pageControladdTarget:selfaction:@selector(pageControlAction:)forControlEvents:UIControlEventValueChanged];
//定时器
[NSTimertimerWithTimeInterval:2target:selfselector:@selector(walk)userInfo:nilrepeats:YES];
// Do any additional setup after loading the view.
}
#pragma mark - scrollView的代理事件
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//获取偏移量
CGPoint offSet = scrollView.contentOffset;
//当前的点数的下标
NSInteger index = offSet.x /self.rootView.frame.size.width;
self.rootView.pageControl.currentPage = index;
}
- (void)pageControlAction:(UIPageControl *)sender
{
self.rootView.scrollView.contentOffset = CGPointMake(sender.currentPage *sender.frame.size.width,0);
}
- (void)walk
{
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[_rootView release];
[superdealloc];
}
@end
//
// RootView.h
// UI-Homework-7
//
// Created by lanouhn on 15-8-5.
// Copyright (c) 2015年 尹江涛-UIScrollView-UIPageController. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RootView :UIView
@property (nonatomic ,retain)UIPageControl *pageControl;
@property (nonatomic ,retain)UIScrollView *scrollView;
@end
//
// RootView.m
// UI-Homework-7
//
// Created by lanouhn on 15-8-5.
// Copyright (c) 2015年 尹江涛-UIScrollView-UIPageController. All rights reserved.
//
#import "RootView.h"
@implementation RootView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
[selfaddSubviews];
}
return self;
}
- (void)addSubviews
{
self.backgroundColor = [UIColorbrownColor];
// scrollView
self.scrollView = [[UIScrollViewalloc] initWithFrame:self.frame];
for (int i =0; i < 22; i++) {
// 创建image
UIImage *image = [UIImageimageNamed:[NSStringstringWithFormat:@"Zombie%d.tiff",i+1]];
// 创建imageView
UIImageView *imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(i *self.frame.size.width,0, self.frame.size.width,self.frame.size.height)];
//装载image
imageView.image = image;
[self.scrollViewaddSubview:imageView];
[imageViewrelease];
}
self.scrollView.contentSize =CGSizeMake(self.frame.size.width * 22, self.frame.size.height);
self.scrollView.pagingEnabled =YES;
[selfaddSubview:self.scrollView];
[self.scrollViewrelease];
//pageControl
self.pageControl = [[UIPageControlalloc] initWithFrame:CGRectMake(0,self.frame.size.height-45, self.frame.size.width,45)];
self.pageControl.numberOfPages =22;
self.pageControl.currentPageIndicatorTintColor = [UIColorredColor];
self.pageControl.pageIndicatorTintColor = [UIColorblueColor];
[selfaddSubview:self.pageControl];
[self.pageControlrelease];
}
- (void)dealloc
{
[_pageControl release];
[_scrollView release];
[superdealloc];
}
@end