一 大图片展示
#import "MJViewController.h"
@interface MJViewController ()
{
UIScrollView *_scrollView;//方但全局调用定义成成员变量
}
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.创建UIScrollView
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = CGRectMake(0, 0, 250, 250); // frame中的size指UIScrollView的可视范围
scrollView.backgroundColor = [UIColor grayColor];
[self.view addSubview:scrollView];
// 2.创建UIImageView(图片)
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"big.jpg"];
CGFloat imgW = imageView.image.size.width; // 图片的宽度
CGFloat imgH = imageView.image.size.height; // 图片的高度
imageView.frame = CGRectMake(0, 0, imgW, imgH);
[scrollView addSubview:imageView];
// 3.设置scrollView的属性
// 设置UIScrollView的滚动范围(内容大小)
scrollView.contentSize = imageView.image.size;
// 隐藏水平滚动条
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
// 用来记录scrollview滚动的位置
// scrollView.contentOffset = ;
// 去掉弹簧效果
// scrollView.bounces = NO;
// 增加额外的滚动区域
// top left bottom right
scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
_scrollView = scrollView;
}
- (IBAction)down:(UIButton *)sender {
[UIView animateWithDuration:1.0 animations:^{
这个方法为按钮控制身下的滚动效果,这里就是改变_scrollView.contentOffset的Y 值
// CGPoint offset = _scrollView.contentOffset;
// offset.y += 150;
// _scrollView.contentOffset = offset;
_scrollView.contentOffset = CGPointMake(0, 0);
}];
}
@end
二 图片缩放
//
// MJViewController.m
// 02-UIScrollView02-图片缩放
//
// Created by apple on 13-11-27.
// Copyright (c) 2013年 itcast. All rights reserved.
//
#import "MJViewController.h"
@interface MJViewController () <UIScrollViewDelegate>
{
UIImageView *_imageView;
}
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.添加UIScrollView
UIScrollView *scrollView = [[UIScrollView alloc] init];
// scrollView.frame = CGRectMake(0, 0, 320, 460);
scrollView.frame = self.view.bounds;
[self.view addSubview:scrollView];
// 2.添加图片
UIImage *image = [UIImage imageNamed:@"big.jpg"];
// 调用initWithImage:方法创建出来的UIImageView,它的宽高默认跟图片的宽高一样
_imageView = [[UIImageView alloc] initWithImage:image];
// // 设置图片
// imageView.image = [UIImage imageNamed:@"big.jpg"];
//
// // 设置frame
// imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);
[scrollView addSubview:_imageView];
// 设置内容范围
scrollView.contentSize = image.size;
// 设置scrollview的代理对象
scrollView.delegate = self;
// 设置最大伸缩比例
scrollView.maximumZoomScale = 2.0;
// 设置最小伸缩比例
scrollView.minimumZoomScale = 0.2;
}
#pragma mark - UIScrollView 的 代理方法
#pragma mark 这个方法返回的控件就能进行捏合手势缩放操作
#pragma mark 当UIScrollView尝试进行缩放的时候就会调用
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return _imageView;
}
#pragma mark 当缩放完毕的时候调用
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
// NSLog(@"结束缩放 - %f", scale);
}
#pragma mark 当正在缩放的时候调用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
// NSLog(@"-----");
}
@end