PageControl

//
//  RootView.m
//  UI7PageControl
///Users/lanouhn/Downloads
//  Created by lanouhn on 15-8-5.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "RootView.h"

@implementation RootView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    
    
    //创建scrollView
    self.scroller = [[UIScrollView alloc] initWithFrame:self.frame];
    //循环拿到image 并加到imageView上
    for (int i = 0 ; i < 23; i++) {
        
        //创建image
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"tu5377_%d.jpg" , i + 2]];
        
        //创建imageView
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        
        //把图片贴到贴到imageView
        imageView.image = image;
        
        //imageView添加到scrollerView 上
        [self.scroller addSubview:imageView];
        [imageView release];
        
    }
    
    //设置scrollView的内容视图大小
   
    self.scroller.contentSize = CGSizeMake(23 * self.frame.size.width, self.frame.size.height);
    
    
    //设置整页滑动
    self.scroller.pagingEnabled = YES;
    
    
    [self addSubview:self.scroller];
    [self.scroller release];
    
    
    //pch
    //$(SRCROOT)/UI7PageControl/MMM.pch
    
    
    //添加黑色背景
    /*
    self.blackView = [[UIView alloc] initWithFrame:self.frame];
    self.blackView.backgroundColor = [UIColor blackColor];
    [self addSubview:self.blackView];
    [self.blackView release];
    */
    
    //创建pageControl
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - kPageControlHeight, self.frame.size.width, kPageControlHeight)];
    
    self.pageControl.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:self.pageControl];
    
    //pageControl   默认0个点
    //给pageControl设置点的个数
    self.pageControl.numberOfPages = 23;
    
    //设置,默认选中的点
//    self.pageControl.currentPage = 0;
    //设置 当前选中点的颜色
    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    //设置未选中的颜色
    self.pageControl.pageIndicatorTintColor = [UIColor blackColor];
    
    //添加点击事件
//    [self.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    
    //默认交互关闭
    self.pageControl.userInteractionEnabled = YES;
    
    
}

//#pragma mark - pageControl点击事件
//- (void)pageControlAction:(UIPageControl *)sender
//{
//    
//}
/*
 
 The quick brown fox jumps over a lazy dog
 The quick brown fox jumps over a lazy dog
 
 
 
 */



- (void)dealloc
{
    [_scroller release];
    [_blackView release];
    [_pageControl release];
    [super dealloc];
}



@end

//
//  RootViewController.m
//  UI7PageControl
//
//  Created by lanouhn on 15-8-5.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UIScrollViewDelegate>

@end

@implementation RootViewController


- (void)loadView
{
    [super loadView];
    self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    //给scrollerView设置代理对象
    self.rootView.scroller.delegate = self;
    
    [self.rootView.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    
   
}

#pragma mark - ScrollerView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"减速结束");
    //拿到偏移量
    CGPoint offset = scrollView.contentOffset;
    //算出偏移了几个
    NSInteger currentIndex = offset.x / self.rootView.frame.size.width;
    //根据currentIndex 修改pageControl显示点得位置
    self.rootView.pageControl.currentPage = currentIndex;
    
    
    
    
    
    
}

- (void)pageControlAction:(UIPageControl *)sender
{
    //获取到当前pageControl显示的下标
    NSInteger currentIndex =  self.rootView.pageControl.currentPage;
    
    //根据下标算出 scollerView 的偏移量
    NSInteger x = currentIndex * self.rootView.frame.size.width;
    
    //根据偏移量设置scollerView 的contentOffset     这个对象的改变只能通过          CGPointMake
    self.rootView.scroller.contentOffset = CGPointMake(x, 0);
    
    
    self.rootView.scroller.contentOffset = CGPointMake(sender.currentPage * self.rootView.frame.size.width, 0);
    
    
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"拖拽结束");
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"滑动中.....");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"将要开始减速");
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"将要开始拖拽");
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    NSLog(@"将要结束拖拽");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

//
//  AppDelegate.m
//  UI7PageControl
//
//  Created by lanouhn on 15-8-5.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    
    
    //用NSUserDefult从文件里取内容 跟创建的对象无关
    NSUserDefaults *sss = [NSUserDefaults standardUserDefaults];
    NSString *str = [sss objectForKey:@"key1"];
    NSLog(@"str = %@" , str);
    
    
    NSLog(@"**************");
    NSLog(@"%@" , NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject);
    NSLog(@"**************");
    
    //判断是否是第一次运行在该模拟器上
    if ([str isEqualToString:@"刘敦辉"]) {
        
        //用来存储应用程序的一些小的配置信息
        NSUserDefaults *stand = [NSUserDefaults standardUserDefaults];
        
        //向文件里边存储 key - valve 值
        [stand setObject:@"刘敦辉" forKey:@"key1"];
        [stand setObject:@"泰迪" forKey:@"key2"];
        [stand setObject:@"宪政" forKey:@"key3"];
        
        //同步操作  马上把这个键值对保存  文件夹查看  command + shift + G
        [stand synchronize];
        
        RootViewController *rootVC = [[RootViewController alloc] init];
        self.window.rootViewController = rootVC;
        [rootVC release];

    }else{
        
        NSLog(@"程序非第一次运行在该模拟器上运行");
        
    }
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值