相册的实现

#import <UIKit/UIKit.h>


@class AblumView;

@protocol AblumViewDelegate <NSObject>


@optional

- (void)hiddenOrShow:(AblumView *)ablumView;


@end


@interface AblumView : UIView <UIScrollViewDelegate>

{

    UIScrollView *_scrollView;

    UIImageView  *_imgView;

}


@property (nonatomic, assign) id <AblumViewDelegate> delegate;

@property (nonatomic, copy) NSString *url;

@property (nonatomic, readonly) UIScrollView *scrollView;


- (void)downloadImage;


@end


#import "AblumView.h"

#import "common.h"

@implementation AblumView


-(instancetype)initWithFrame:(CGRect)frame

{

    self=[super initWithFrame:frame];

    if (self) {

        

        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];

        _scrollView.minimumZoomScale = 1;

        _scrollView.maximumZoomScale = 2.5;

        _scrollView.showsHorizontalScrollIndicator = NO;

        _scrollView.showsVerticalScrollIndicator = NO;

        _scrollView.scrollsToTop = NO;

        _scrollView.delegate = self;

        _scrollView.backgroundColor = [UIColor blackColor];

        [self addSubview:_scrollView];

        

        _imgView = [[UIImageView alloc] initWithFrame:_scrollView.frame];

        _imgView.userInteractionEnabled = YES;

        _imgView.contentMode = UIViewContentModeScaleAspectFit;

        [_scrollView addSubview:_imgView];

        

        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomOutOrIn:)];

        doubleTap.numberOfTapsRequired = 2;

        [self addGestureRecognizer:doubleTap];

        

        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenOrShow:)];

        [self addGestureRecognizer:singleTap];

        

        // 双击时忽略掉单击事件

        [singleTap requireGestureRecognizerToFail:doubleTap];

    }

    

    return self;

}

#pragma mark - Public Method

- (void)downloadImage

{

    if (_imgView.image == nil) {

        // 把图片异步请求下来

        [_imgView setImageWithURL:[NSURL URLWithString:_url]];

    }

}


#pragma mark - ScrollView Delegate

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

{

    return _imgView    ;

}


#pragma mark - target Method

- (void)zoomOutOrIn:(UITapGestureRecognizer *)tap

{

    // 获取到用户点击位置

    CGPoint point = [tap locationInView:_imgView];

    

    // NSLog(@"%@", NSStringFromCGPoint(point));

    

    if (_scrollView.zoomScale == 1) {

        

        [_scrollView zoomToRect:CGRectMake(point.x-40, point.y-40, 80, 80) animated:YES];

        

    }else {

        [_scrollView setZoomScale:1 animated:YES];

    }

}



- (void)hiddenOrShow:(UITapGestureRecognizer *)tap

{

    if ([self.delegate respondsToSelector:@selector(hiddenOrShow:)]) {

        [self.delegate hiddenOrShow:self];

    }

}

@end




#import <UIKit/UIKit.h>

#import "BaseViewController.h"

#import "AblumView.h"


@interface AblumViewController : BaseViewController <AblumViewDelegate, UIScrollViewDelegate>

{

@private

    UIImageView    *_navigationBar;

    UIScrollView   *_contentScrollView;

    UILabel        *_titleLabel;

    NSArray *_imagesData;

}


@end



#import "AblumViewController.h"

#import "ImageModel.h"

#import "AblumView.h"

#import "MainViewController.h"

#import "ImageModel.h"

#import "WXNetworkService.h"

#import "common.h"


#define kScrollViewGap 15

@interface AblumViewController ()

- (void)loadNavigationBar;


- (void)loadBaseScrollView;


- (void)loadTitleView;


- (void)requestData;


- (void)refreshUI;


- (void)downloadData:(NSInteger)i;

@end


@implementation AblumViewController


- (void)viewDidLoad {

    [super viewDidLoad];

      [self requestData];

    // Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//滑动做三项事情,第一把字改了,第二把上一个相册的图的比例还原了,第三个加载图片

#pragma mark - ViewController Life

- (void)loadView

{

    [super loadView];

    // 创建滑动视图

    [self loadBaseScrollView];

    

    // 自定义导航栏视图

    [self loadNavigationBar];

    

    // 创建标题视图(底部视图)

    [self loadTitleView];

}

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    

    if (self.navigationController.viewControllers.count == 2) {

        

        [self.navigationController setNavigationBarHidden:YES animated:YES];

    }

}


- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    

    if (self.navigationController.viewControllers.count == 1) {

        

        [self.navigationController setNavigationBarHidden:NO animated:YES];

        

        MainViewController *mainVC = (MainViewController *)self.tabBarController;

        [mainVC showOrHiddenTabBarView:NO];

    }

}


#pragma mark - Private Method

- (void)loadNavigationBar

{

    _navigationBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 44)];

    _navigationBar.userInteractionEnabled = YES;

    _navigationBar.image = [UIImage imageNamed:@"nav_bg_all"];

    [self.view addSubview:_navigationBar];

    

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

    backButton.frame = CGRectMake(2, 0, 44, 44);

    [backButton setBackgroundImage:[UIImage imageNamed:@"backItem"] forState:UIControlStateNormal];

    [backButton addTarget:self action:@selector(backRootVC:) forControlEvents:UIControlEventTouchUpInside];

    [_navigationBar addSubview:backButton];

    

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 200, 44)];

    title.text = @"电影图片";

    title.textAlignment = NSTextAlignmentCenter;

    title.textColor = [UIColor whiteColor];

    title.backgroundColor = [UIColor clearColor];

    title.font = [UIFont boldSystemFontOfSize:22];

    [_navigationBar addSubview:title];

}


- (void)loadBaseScrollView

{

    _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth+kScrollViewGap, kDeviceHeight-20)];

    _contentScrollView.pagingEnabled = YES;

    _contentScrollView.delegate = self;

   _contentScrollView.tag = INT_MAX;

    _contentScrollView.showsHorizontalScrollIndicator = NO;

    [self.view addSubview:_contentScrollView];

}


- (void)loadTitleView

{

    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kDeviceHeight-49-20, kDeviceWidth, 49)];

    _titleLabel.textAlignment = NSTextAlignmentCenter;

    _titleLabel.backgroundColor = [UIColor blackColor];

    _titleLabel.textColor = [UIColor whiteColor];

    _titleLabel.font = [UIFont boldSystemFontOfSize:16];

    [self.view addSubview:_titleLabel];

}



//- (void)requestData

//{

//    NSArray *data = [WXNetworkService newsImageData];

//    

//    _imagesData = [[NSMutableArray alloc] initWithCapacity:data.count];

//    for (id dic in data) {

//        

//        ImageModel *imgModel = [[ImageModel alloc] initWithContent:dic];

//        [_imagesData addObject:imgModel];

//    }

//    

//    [self refreshUI];

//}

-(void)requestData

{

    NSArray *data=[WXNetworkService newsImageData];

    _imagesData=[[NSArray alloc]init];

    _imagesData=[ImageModel modelArrayWithDictArray:data];

    [self refreshUI];

}


- (void)refreshUI

{

    // 设置滑动视图的内容大小

    _contentScrollView.contentSize = CGSizeMake((kDeviceWidth + kScrollViewGap) * _imagesData.count, 0);

    

    // 设置第一个标题的内容

    ImageModel *imageModel = _imagesData[0];

    _titleLabel.text = imageModel.title;

    

    int x = 0;

    for (int index = 0; index < _imagesData.count; index++) {

        

        // 取出数据

        ImageModel *imageModel = _imagesData[index];

        

        // 创建视图

        AblumView *ablumView = [[AblumView alloc] initWithFrame:CGRectMake(0+x, 0, kDeviceWidth, kDeviceHeight-20)];

        ablumView.tag = index;

        if (index < 2) {

            

            ablumView.url = imageModel.url1;

            [ablumView downloadImage];

        }

        ablumView.delegate = self;

        [_contentScrollView addSubview:ablumView];

        

        x += (kDeviceWidth + kScrollViewGap);

    }

}



- (void)downloadData:(NSInteger)i

{

    AblumView *_ablumView = (AblumView *)[_contentScrollView viewWithTag:i];

    ImageModel *imageModel = _imagesData[i];

    _ablumView.url = imageModel.url1;

    [_ablumView downloadImage];

}






#pragma mark - Action Method

- (void)backRootVC:(UIButton *)button

{

    [self.navigationController popViewControllerAnimated:YES];

}



#pragma mark - ScrollView Delegate

static int lastIndex = 0;

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    int index = scrollView.contentOffset.x / (kDeviceWidth+kScrollViewGap);

    

    if (index >= _imagesData.count || index < 0) {

        return;

    }

    

    // 设置标题

    ImageModel *imageModel = _imagesData[index];

    _titleLabel.text = imageModel.title;

    

    // 还原上一个视图比例

    AblumView *ablumView = (AblumView *)[_contentScrollView viewWithTag:lastIndex];

    if (ablumView.scrollView.zoomScale >= 1 && lastIndex != index) {

        

        ablumView.scrollView.zoomScale = 1;

    }

    

    if (index == 0) {

        // ... noting 2张图片(1 2

        for (int i = 0; i <= index+1; i++) {

            // NSLog(@"before : %d", i);

            

        }

        

    }else if (index == _imagesData.count-1) {

        // 2张图片(最后一张和倒数第二章)

        for (int i = index; i >= index-1; i--) {

            NSLog(@"end :i : %d", i);

            [self downloadData:i];

        }

    }else {

        // 3 4 5

        for (int i = index+1; i >= index-1; i--) {

            NSLog(@"middle : %d", i);

            [self downloadData:i];

        }

    }

    

    lastIndex = index;

}


#pragma mark - AblumView Delegate

- (void)hiddenOrShow:(AblumView *)ablumView

{

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.35];

    if (_navigationBar.alpha == 1 && _titleLabel.alpha == 1) {

        _navigationBar.alpha = 0;

        _titleLabel.alpha = 0;

    }else {

        _navigationBar.alpha = 1;

        _titleLabel.alpha = 1;

    }

    [UIView commitAnimations];

}


@end



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值