IOS-UIScrollView

来源:互联网 发布:国考申论 知乎 编辑:程序博客网 时间:2024/06/11 03:59

//

//  MainViewController.m

//  UIScrollView

//

//  Created by Lanou on 14-4-11.

//  Copyright (c) 2014李杨. All rights reserved.

//

//作业1:实现相册

//2.查询相关apiuiscrollviewbounces属性,让scrollview的滚动条消失,改变uipagecontrol的点的颜色,

//测试uitextfield的协议,实现如下效果:有两个textfield,点击第一个的时候键盘右下角为next,点击next后让光标显示在第二个textfieldB里面,并且键盘右下角为完成按钮,点击完成后收回键盘


//3.实现循环滚动相册。

#import "MainViewController.h"


@interface MainViewController ()


@end


@implementation MainViewController

-(void)dealloc{

    [_scroll release];

    _scroll = nil;

    [_page release];

    _page = nil;

    [super dealloc];


}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

//   建立UIScrollView对象

    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];

    [_scroll setBackgroundColor:[UIColor blueColor]];

    [_scroll setContentSize:CGSizeMake(320*6, 200)];

//    设置起始的位置

    _scroll.contentOffset = CGPointMake(640, 0);

//    给这个类添加协议

    _scroll.delegate = self;

//    设置放大缩小的倍数 就是放大后停在哪个倍数

//    [_scroll setMaximumZoomScale:2.0f];

//    [_scroll setMinimumZoomScale:0.5f];

//    设置是否显示边框

//    [_scroll setBounces:NO];

//   显示水平条-----Horizontal

//    [_scroll setShowsHorizontalScrollIndicator:NO];

//   显示垂直条-----Vertical

//    [_scroll setShowsVerticalScrollIndicator:NO];

//    把每一屏幕设置成一页一页

    [_scroll setPagingEnabled:YES];

    [self.view addSubview:_scroll];

    [_scroll release];

    

    for (int i = 0; i < 6; i++) {

//       在大的UIScrollView上添加一个个小的UIScrollView

        UIScrollView * scr = [[UIScrollView alloc] initWithFrame:CGRectMake(320*i, 0, 320, 200)];

        [scr setBackgroundColor:[UIColor blackColor]];

//       边框放大缩小可以不

//        scr.bouncesZoom = NO;

        [scr setMaximumZoomScale:2];

        [scr setMinimumZoomScale:0.5];

        scr.delegate = self;

        [_scroll addSubview:scr];

        [scr release];

//       UIScrollView上田间UIImageView

        NSString * str = [NSString stringWithFormat:@"%d",i+1];

        UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:str]];

        [image setFrame:CGRectMake(0,0, 320, 200)];

        [scr addSubview:image];

        [image release];

    }

//   添加UIPageController

    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 270, 320, 40)];

    [_page setBackgroundColor:[UIColor redColor]];

//   设置几页面

    [_page setNumberOfPages:6];

    [_page setPageIndicatorTintColor:[UIColor yellowColor]];

    [_page setCurrentPageIndicatorTintColor:[UIColor blackColor]];

//   添加点击事件

    [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

//    初始化page的位置为当前页面

    [_page setCurrentPage:_scroll.contentOffset.x/320];

    [self.view addSubview:_page];

    [_page release];

    

}

//   标记代码用途

#pragma mark -

#pragma mark pageControl action


-(void)pageAction:(id)sender{

    if ([sender isKindOfClass:[UIPageControl class]]) {

//        UIPageControl *page = (UIPageControl *)sender;

//       取得当前页数 并转换为ContentOffset 显示到这个位置

        [_scroll setContentOffset:CGPointMake(_page.currentPage*320, 0)];

    }

    

}


#pragma mark -

#pragma mark scrollview delegate


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

//    NSLog(@"%@",scrollView);

    

//    结构体的打印方法

    

    NSLog(@"content off set == %@",NSStringFromCGPoint(scrollView.contentOffset));

    

//    从偏移量取得显示的页码

    [_page setCurrentPage:_scroll.contentOffset.x/320];


}


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

//   重新设定上面小的UIScrollView的图片的伸缩

    for (UIScrollView * s in scrollView.subviews) {

        if ([s isKindOfClass:[UIScrollView class]]) {

            [s setZoomScale:1];

        }

    }

}



//   改变scrollView的大小

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

//   取得scrollView上的image

    NSArray * arr = [scrollView subviews];

    return [arr objectAtIndex:0];

}


@end



0 0
原创粉丝点击