scollView中实现缩放

本文介绍了如何在iOS开发中,通过UIScrollView实现图片的浏览与缩放功能,详细讲解了RootViewController和ImageScrollView的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RootViewController.m

#import "RootViewController.h"
#import "RootView.h"
#import "ImagescrollView.h"

@interface RootViewController () <UIScrollViewDelegate>
{
    NSInteger _startIndex;
}

#pragma mark - 声明私有属性
@property (nonatomic, retain) RootView *rootView;
@end

@implementation RootViewController

#pragma mark - 自定义视图 代替 自带视图
- (void)loadView
{
    self.rootView = [[[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    self.view = _rootView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //设置代理
    _rootView.scrollView.delegate = self;
    
    
}

#pragma mark - 代理方法
#pragma mark - 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _startIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
}


#pragma mark - 结束自由滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    //获取当前页面
    NSInteger currentIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    //如果不是当前页面
    if (currentIndex == _startIndex) {
        return;
    }
    
    
    //给数组中的每个元素都发送 setScaleone消息   perform执行完成
    [scrollView.subviews makeObjectsPerformSelector:@selector(setScaleOne)];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 重写
#pragma mark - dealloc
-(void)dealloc
{
    [_rootView release];
    [super dealloc];
}

RootView.m


#import "RootView.h"
#import "ImageScrollView.h"

@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor cyanColor];
        [self addAllViews];
    }
    return self;
}

#pragma mark - 添加全部控件
- (void)addAllViews
{
    // 添加scrollView
    self.scrollView = [[[UIScrollView alloc] initWithFrame:self.frame] autorelease];
    [self addSubview:_scrollView];
    
    // 图片总数
    NSInteger count = 9;
    
    // for循环,添加ImageScrollView
    for (int i = 0; i < count; i++) {
        
        NSString *imgName = [NSString stringWithFormat:@"bg_%d.JPG", i];
        UIImage *image = [UIImage imageNamed:imgName];
        
        ImageScrollView *imageScrollView = [[ImageScrollView alloc] initWithFrame:CGRectMake(i * _scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height) image:image];
        
        [_scrollView addSubview:imageScrollView];
        [imageScrollView release];
    }
    
    // 设置scrollView的一些属性
    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * count, _scrollView.frame.size.height);
    _scrollView.pagingEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    
}


#pragma mark - 重写
#pragma mark dealloc
- (void)dealloc
{
    [_scrollView release];
    
    [super dealloc];
}

ImageScollView.m


#import "ImageScrollView.h"

@interface ImageScrollView () <UIScrollViewDelegate>

@property (nonatomic, retain) UIImageView *imageView;

@end

@implementation ImageScrollView


- (instancetype)initWithFrame:(CGRect)frame
                        image:(UIImage *)image
{
    if (self = [super initWithFrame:frame]) {
        
        // 初始化图片
        self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
        _imageView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [self addSubview:_imageView];
        
        // 设置自身为代理
        self.delegate = self;
        
        // 设置最大和最小缩放比例
        self.minimumZoomScale = 0.5;
        self.maximumZoomScale = 2;
    }
    return self;
}




#pragma mark - 代理方法
#pragma mark 设置缩放的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
}

#pragma mark 缩放结束,修改到中心
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView
                       withView:(UIView *)view
                        atScale:(CGFloat)scale
{
    CGRect frame = view.frame;
    
    if (scrollView.frame.size.width < scrollView.contentSize.width) {
        frame.origin.x = 0;
    } else {
        frame.origin.x = (scrollView.frame.size.width - scrollView.contentSize.width) / 2;
    }
    
    if (scrollView.frame.size.height < scrollView.contentSize.height) {
        frame.origin.y = 0;
    } else {
        frame.origin.y = (scrollView.frame.size.height - scrollView.contentSize.height) / 2;
    }
    
    view.frame = frame;
}

#pragma mark 设置视图恢复到原来的位置
- (void)setScaleOne
{
    CGRect frame = _imageView.frame;
    frame.origin.x = 0;
    frame.origin.y = 0;
    _imageView.frame = frame;
    
    self.zoomScale = 1;
}




#pragma mark - 重写
#pragma mark dealloc
- (void)dealloc
{
    [_imageView release];
    
    [super dealloc];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值