10.5 UIScrollview概念和用法:实现简单的相册功能

=======10.5 UIScrollview概念和用法:实现简单的相册功能====

思路:
在UIScrollView上再加上一个小的UIScrollView,而在小的UIScrollView上有ImageView支持图片缩放;


测试代码:

- (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];
    
    RootViewController *view = [[RootViewController alloc] init];
    self.window.rootViewController = view;
    
    return YES;
}


//
//  RootViewController.m
//  my_ScrollView
//
//  Created by fenghuo on 14-7-21.
//  Copyright (c) 2014年 fenghuo. All rights reserved.
//

#import "RootViewController.h"
#import "ImageScrollView.h"

@interface RootViewController () <UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    UIImageView  *_imageView;
}

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    // base scrollView
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, 460)];
    _scrollView.backgroundColor = [UIColor blackColor];
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.tag = INT_MAX;
    _scrollView.showsHorizontalScrollIndicator = NO;
    // 设置内容大小
    _scrollView.contentSize = CGSizeMake(340*4, 460);
    [self.view addSubview:_scrollView];
    
    int _x = 0;
    for (int index = 0; index < 4; index++) {
        ImageScrollView *imgScrollView = [[ImageScrollView alloc] initWithFrame:CGRectMake(0+_x, 0, 320, 460)];
        imgScrollView.tag = index;
        NSString *imgName = [NSString stringWithFormat:@"%d.jpg", index+1];
        imgScrollView.imageView.image = [UIImage imageNamed:imgName];
        [_scrollView addSubview:imgScrollView];
        _x += 340;
    }

    
}

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


// 记录之前的次数
int pre = 0;
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 当前第几张图片;
    int current = scrollView.contentOffset.x / 340;
    
    // 将图片缩放还原
    ImageScrollView *imgScrollView = (ImageScrollView *)[scrollView viewWithTag:pre];
    if (current != pre && imgScrollView.zoomScale > 1) {
        imgScrollView.zoomScale = 1;
    }
    
    pre = current;
}


@end


=======ImageScrollView.h===========
#import <UIKit/UIKit.h>

// 支持单张图片缩放
@interface ImageScrollView : UIScrollView <UIScrollViewDelegate>

@property (nonatomic, retain) UIImageView *imageView;

@end

=======ImageScrollView.m===========
//
//  ImageScrollView.m
//  my_ScrollView
//
//  Created by fenghuo on 14-7-23.
//  Copyright (c) 2014年 fenghuo. All rights reserved.
//

#import "ImageScrollView.h"



@implementation ImageScrollView

@synthesize imageView = _imageView;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // 设置scrollView的属性
        self.maximumZoomScale = 2.5;
        self.minimumZoomScale = 1;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        // 添加图片
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:_imageView];
        
        // 设置代理
        self.delegate = self;
        
        // 添加双击事件
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomInOrOut:)];
        doubleTap.numberOfTapsRequired = 2;
        [self addGestureRecognizer:doubleTap];
        
    }
    return self;
}


#pragma mark - UIScrollView Delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
}


#pragma mark - Target Action
- (void)zoomInOrOut:(UITapGestureRecognizer *)tapGesture
{
    if (self.zoomScale >= 2.5) {
        [self setZoomScale:1 animated:YES];
    }else {
        // 得到相应的双击区域,然后缩放相应的区域
        CGPoint point = [tapGesture locationInView:self];
        // -40,指缩放到相应的中心点
        [self zoomToRect:CGRectMake(point.x - 40, point.y - 40, 80, 80) animated:YES];
    }
}


@end



结果展示:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值