1. 程式人生 > >swanzhu學ios(四)之UIScrollView與UIPageControl

swanzhu學ios(四)之UIScrollView與UIPageControl

//
//  ZYAppDelegate.m
//  ScrollView_Page
//
//  Created by mac on 15-8-13.
//  Copyright (c) 2015年 baidu. All rights reserved.
//

#import "ZYAppDelegate.h"

@implementation ZYAppDelegate

- (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];
    
    
    
    
// 滑動檢視   

    scrollview=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, 320, 200)];
    
    for (int i=0; i<3; i++) {
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(320*i, 0, 320, 200)];
        NSString *imgName=[NSString stringWithFormat:@"%d.jpg",i+1];
        
        imageView.image=[UIImage imageNamed:imgName];
        
        [scrollview addSubview:imageView];
    }
    
    
    scrollview.contentSize=CGSizeMake(320*3, 200);
    scrollview.pagingEnabled=YES;
    [self.window addSubview:scrollview];
    
//    設定代理
    scrollview.delegate=self;
    
    
    
    
//  pageControl
    pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(100, 180, 120, 40)];
    pageControl.numberOfPages=3;
    [self.window addSubview:pageControl];
    
    [pageControl addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventValueChanged];
    
    [self.window makeKeyAndVisible];
    
    
    
    [NSTimer  scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    
    
    
    
    
    
    return YES;
}


-(void)onTimer
{
    

    
   static int count=320;
    [scrollview setContentOffset:CGPointMake(scrollview.contentOffset.x+count, 0) animated:YES];
    

    
    
    if (scrollview.contentOffset.x==320) {
        count=-count;
    }
    
    
    
  
}
-(void)changeValue
{


//    scrollview.contentOffset.x=pageControl.currentPage*320;
//    scrollview.contentOffset=CGPointMake(pageControl.currentPage*320, 0);

    [scrollview setContentOffset:CGPointMake(pageControl.currentPage*320, 0) animated:YES];
}

//結束減速之後呼叫的方法

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{


   pageControl.currentPage =scrollView.contentOffset.x/320;

}





- (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