UI06_UIScrollView

来源:互联网 发布:2015年程序员考试真题 编辑:程序博客网 时间:2024/03/29 20:00
////  AppDelegate.h//  UI06_UIScrollView////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end
<pre name="code" class="objc">////  AppDelegate.m//  UI06_UIScrollView////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 flg. All rights reserved.//#import "AppDelegate.h"#import "MainViewController.h"@interface AppDelegate ()@end@implementation AppDelegate-(void)dealloc{    [_window release];    [super dealloc];}- (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];    [_window release];    MainViewController *mainVC=[[MainViewController alloc] init];    self.window.rootViewController=mainVC;    [mainVC 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


</pre><pre name="code" class="objc"><pre name="code" class="objc">////  MainViewController.h//  UI06_UIScrollView////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface MainViewController : UIViewController@end


</pre><pre name="code" class="objc"><pre name="code" class="objc">////  MainViewController.m//  UI06_UIScrollView////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 flg. All rights reserved.//#import "MainViewController.h"#define WIDTH self.view.frame.size.width#define HEIGHT self.view.frame.size.height@interface MainViewController ()<UIScrollViewDelegate>@end@implementation MainViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor=[UIColor lightGrayColor];////  UIScrollView 继承于uiview,,使用uiview的创建方法进行创建    UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];    scrollView.backgroundColor=[UIColor orangeColor];    [self.view addSubview:scrollView];    [scrollView release];    //重要的属性,可以让scrollView滚动起来    //contentSize设置scrollView的滚动范围    scrollView.contentSize=CGSizeMake(WIDTH * 10, HEIGHT);    //按页进行滚动    scrollView.pagingEnabled=YES;    //前面加一张    UIImageView *view0=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"8.jpg"] ];    view0.frame=CGRectMake(0, 0, WIDTH, HEIGHT);    [scrollView addSubview:view0];    [view0 release];    for (NSInteger i=1; i<9; i++) {        NSString *picName=[NSString stringWithFormat:@"%ld.jpg",i];        //通过图片名来创建UIImage        UIImageView *imageview=[[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]];        imageview.frame=CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT);        //把imageview放到scrollView上        [scrollView addSubview:imageview];        //释放        [imageview release];    }    //后面加一张    UIImageView *view5=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];    view5.frame=CGRectMake(WIDTH * 9, 0, WIDTH, HEIGHT);    [scrollView addSubview:view5];    [view5 release];    //偏移量    scrollView.contentOffset=CGPointMake(WIDTH * 1, 0);    //关掉边界回弹效果/.默认是YES    scrollView.bounces = NO;    //水平和垂直滚动条会作为两个子视图添加到scrollView的子视图里,如果把滚动条的效果关闭,这两个视图就不会添加到scrollView的子视图里    //NSLog(@"%@",scrollView.subviews);   //关闭滚动条    scrollView.showsHorizontalScrollIndicator=NO;    scrollView.showsVerticalScrollIndicator=NO;    //设置代理人    scrollView.delegate=self;    //给scrollView设置tag值    scrollView.tag=1000;    //创建一个计时器    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];}#pragma mark  只要scrollView滚动就会触发的方法//-(void)scrollViewDidScroll:(UIScrollView *)scrollView{//    //NSLog(@"开始滚动");////}#pragma mark  scrollView减速停止会触发的方法-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    if (scrollView.contentOffset.x==WIDTH*9) {        scrollView.contentOffset=CGPointMake(WIDTH * 1, 0);    }    else if (scrollView.contentOffset.x==WIDTH*0) {        scrollView.contentOffset=CGPointMake(WIDTH * 8, 0);    }    NSLog(@"滚动停止");}-(void)changeImage{  ///自动滚动    //通过tag值找 scrollView    UIScrollView *scrollView=(UIScrollView *)[self.view viewWithTag:1000];//    scrollView.contentOffset=CGPointMake(scrollView.contentOffset.x+WIDTH, 0);    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x+WIDTH, 0) animated:YES];    //对新的偏移量进行判断    if (scrollView.contentOffset.x==WIDTH*9) {        scrollView.contentOffset=CGPointMake(WIDTH * 1, 0);    }}- (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



0 0
原创粉丝点击