码迷,mamicode.com
首页 > 移动开发 > 详细

swanzhu学ios(四)之UIScrollView与UIPageControl

时间:2015-08-13 22:25:51      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:



素材地址    http://download.csdn.net/download/swanzhu/9002215


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


版权声明:本文为博主原创文章,未经博主允许不得转载。

swanzhu学ios(四)之UIScrollView与UIPageControl

标签:

原文地址:http://blog.csdn.net/swanzhu/article/details/47618415

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!