UI - UIPageControl

<AppDelegate.m>

#import "AppDelegate.h"
#import "RootViewController.h"

@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];
    
    RootViewController *rootVC = [[RootViewController alloc ]init ];
    self.window.rootViewController = rootVC;
    [rootVC release];
    
    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


<RootViewController.h>

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end


<RootViewController.m>

#import "RootViewController.h"

@interface RootViewController ()<UIScrollViewDelegate,UIAlertViewDelegate>

@property (nonatomic,retain)UIPageControl *pageControl;
@property (nonatomic,retain)UIScrollView *scrollView;
@property (nonatomic,retain)UIImageView *imageView;

@end

@implementation RootViewController
-(void)dealloc
{
    self.pageControl = nil;
    self.scrollView = nil;
    self.imageView = nil;
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //偏好设置
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    //用户第一次打开此程序,@"first"为 YES 才可进入此
 
    if (![user boolForKey:@"first"]) {

    //布局 pageControl
    [self layoutPageControl];
    //布局 scrollView
    [self layoutScrollView];
    //布局 imageview
    [self layoutImageView];
    
    [self.view bringSubviewToFront:_pageControl];
    
        [user setBool:YES forKey:@"first"];
    }else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你已经不是第一次启动了" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];
        [alertView show];
        [alertView release];
    }
    
}
//布局 pageControl
-(void)layoutPageControl
{
    //1.创建对象
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(50, self.view.bounds.size.height - 68, 220, 30)];
    //2.配置属性
    //(1)设置页数
    _pageControl.numberOfPages = 6;
    //(2)设置背景颜色
//    _pageControl.backgroundColor = [UIColor yellowColor];
    //(3)设置当前页点的颜色
    _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    //(4)设置其他点得颜色
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    //(5)设置默认当前页数
    _pageControl.currentPage = 1;
    //(6)添加事件
    [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    
    
    //3.添加父视图
    [self.view addSubview:_pageControl];
    //4.释放所有权
    [_pageControl release];
    

}

//事件
-(void)changePage:(UIPageControl *)pageControl
{
    NSLog(@"%d",pageControl.currentPage);
    _scrollView.contentOffset = CGPointMake(320 * pageControl.currentPage, 0);
 
}
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
//{
//    _pageControl.currentPage = scrollView.contentOffset.x /320;
//}
//已经结束减速时触发的方法      将要减速时触发不能实现
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _pageControl.currentPage = scrollView.contentOffset.x /320;
}
//布局 scrollView
-(void)layoutScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    //设置显示内容的大小
    _scrollView.contentSize = CGSizeMake(320 * 6, 568);
    //去掉滚动条
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    //设置整屏滑动
    _scrollView.pagingEnabled = YES;
    //设置偏移量  初始的偏移量
    _scrollView.contentOffset = CGPointMake(320 * _pageControl.currentPage, 0);
    //设置代理
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    [_scrollView release];
    
}
//布局 imageview
-(void)layoutImageView
{
    for (int i = 0 ; i < 6; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, 568)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"v6_guide_%d.png",i + 1]];
        [_scrollView addSubview:imageView];
        [imageView release];
        if (i == 5) {
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(welcome:)];
            [imageView addGestureRecognizer:tap];
            imageView.userInteractionEnabled = YES;
            [tap release];
        }
    }
}


-(void)welcome:(UITapGestureRecognizer *)tap
{
    NSLog(@"adsfdsafsdfa");
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"欢迎使用" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
    [alertView release];
}
//alertview 按钮点击的触发事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        
    [_pageControl removeFromSuperview];
    [_scrollView removeFromSuperview];
    [_imageView removeFromSuperview];
    }else {
        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


图片资源:



你可能感兴趣的:(UIPageControl,UIPageControl总结)