Xcode控件使用笔记三:UIScrollView 查看大图、缩放、分页

来源:互联网 发布:二维数组申明 编辑:程序博客网 时间:2024/04/30 00:12

1、简单实用

    UIImage *image = [UIImage imageNamed:@"ipad.png"];    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];    // 设置内容的宽高(滚动范围)    self.scroll.contentSize = image.size;        // 增加滚动范围    self.scroll.contentInset = UIEdgeInsetsMake(0,0,0,40);    self.scroll.backgroundColor = [UIColor grayColor];    // 可视范围    //self.scroll.frame.size    [self.scroll addSubview:imageView];


2、缩放:用到代理 UIScrollViewDelegate协议

 // 创建UIScrollView    UIScrollView *scroll = [[UIScrollView alloc] init];    scroll.frame = self.view.bounds;    scroll.backgroundColor = [UIColor grayColor];    [self.view addSubview:scroll];        // 添加图片    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ipad.png"]];        [scroll addSubview:imageView];        // 设置scroll的滚动范围    scroll.contentSize = imageView.frame.size;    // 增加滚动范围--额外的    //self.scroll.contentInset = UIEdgeInsetsMake(0, 0, 0, 40);    //self.scroll.backgroundColor = [UIColor grayColor];    // 设置代理    scroll.delegate = self;    self.imageView = imageView;    // 最大缩放比例是2    scroll.maximumZoomScale = 2;    // 最小缩放比例0.5    scroll.minimumZoomScale = 0.5;


#pragma mark - UIScrollView的代理方法#pragma mark 返回需要进行缩放的控件(必须是UIScrollView的子控件)- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{    return self.imageView;}


3、分页 

////  ViewController.m//  02-UIScrollView-分页////  Created by T-60 on 15/5/13.//  Copyright (c) 2015年 com.zhang. All rights reserved.//#define kCount 5#import "ViewController.h"@interface ViewController (){    UIPageControl *_control;    UIScrollView *_scroll;}@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    // 1.添加UIScrollView    UIScrollView *scroll = [[UIScrollView alloc] init];    scroll.frame = self.view.bounds;    [self.view addSubview:scroll];    _scroll = scroll;        CGFloat scrollWdith = scroll.frame.size.width;    CGFloat scrollHeight = scroll.frame.size.height;        // 2.添加所有的ImageView    for (int i = 1; i<=kCount; i++) {        // 加载图片        NSString *imageName = [NSString stringWithFormat:@"pages.bundle/%d.jpg", i];        UIImage *image = [UIImage imageNamed:imageName];        // 创建UIImageView        UIImageView *imageView = [[UIImageView alloc] init];        imageView.image = image;                CGFloat x = (i - 1) * scrollWdith;        imageView.frame = CGRectMake(x, 0, scrollWdith, scrollHeight);                // 添加ImageView        [scroll addSubview:imageView];    }        // 3.设置滚动范围    scroll.contentSize = CGSizeMake(kCount * scrollWdith, 0);        // 隐藏水平滚动条    scroll.showsHorizontalScrollIndicator = NO;        // 4.开启分页功能    scroll.pagingEnabled = YES;            // 5.添加PageControl    UIPageControl *pageControl = [[UIPageControl alloc] init];    pageControl.bounds = CGRectMake(0, 0, 150, 50);    pageControl.center = CGPointMake(scrollWdith * 0.5, scrollHeight - 50);    // 设置页数    pageControl.numberOfPages = kCount;    // 当前选中页码的颜色    pageControl.currentPageIndicatorTintColor = [UIColor redColor];    // 其他页码的颜色    pageControl.pageIndicatorTintColor = [UIColor blackColor];        // 页码改变了,就会调用self的pageChange方法    [pageControl addTarget:self action:@selector(pageChange) forControlEvents:UIControlEventValueChanged];            // 添加到控制器的view    [self.view addSubview:pageControl];    _control = pageControl;    //pageControl.currentPage = 2;        // 6.设置代理    scroll.delegate = self;        // 不需要弹簧效果    scroll.bounces = NO;}- (void)pageChange{    NSLog(@"%d",_control.currentPage);    CGFloat offsetX = _control.currentPage * self.view.frame.size.width;        [UIView beginAnimations:nil context:nil];    // 设置滚动位置    _scroll.contentOffset = CGPointMake(offsetX, 0);        [UIView commitAnimations];    }#pragma mark - 滚动代理#pragma mark scrollview减速完毕就会调用- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    //  计算页码    int pageNo = scrollView.contentOffset.x/scrollView.frame.size.width;    // 设置页码    _control.currentPage = pageNo;}@end


0 0
原创粉丝点击