//
// CollectionViewController.m
// 图片显示
//
// Created by mac on 15/11/17.
// Copyright
2015年 mac. All rights reserved.
//
#import "CollectionViewController.h"
#import "UIKit+AFNetworking.h"
@interface CollectionViewController ()
{
UIScrollView *_scrollView;
int _index;//记住下标
}
@end
@implementation CollectionViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载视图
[self loadScroll];
//把item 移动到当前的视图上,并且放大视图
CGRect newFrame=[self.currentItem.superview convertRect:self.currentItem.frame toView:self.view];
//转换完坐标系,并换成新的坐标
[self.currentItem setFrame:newFrame];
[self.view addSubview:self.currentItem];
}
- (void)loadScroll
{
_scrollView=[[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.contentSize=CGSizeMake(self.view.frame.size.width *self.data.count, self.view.frame.size.height);
_scrollView.pagingEnabled=YES;
_scrollView.delegate=self;
[self.view addSubview:_scrollView];
_scrollView.hidden=YES;
for (int i=0; i<self.data.count; i++) {
//创建imgV
UIImageView *imgV=[[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width *i, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
imgV.userInteractionEnabled=YES;
imgV.contentMode=UIViewContentModeScaleAspectFit;
//加载图片---重新获取图片
[imgV setImageWithURL:[NSURL URLWithString:self.data[i]]];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
tap.numberOfTapsRequired=1;
[imgV addGestureRecognizer:tap];
[_scrollView addSubview:imgV];
}
}
- (void)dismiss{
//获取当前的index 对应的item。放大,最后回味
CustomerImageItem *item=[self.imgView.items objectAtIndex:_index];
[item.superview bringSubviewToFront:item];
CGPoint point=[self.view convertPoint:CGPointZero toView:self.imgView];
[item setFrame:CGRectMake(point.x, point.y, self.view.bounds.size.width, self.view.bounds.size.height)];
[self dismissViewControllerAnimated:NO completion:^{
[UIView animateWithDuration:.25 animations:^{
[item setFrame:item.originFrame];
}];
}];
}
#pragma mark---当视图将要出现加载
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self zoomin];
}
//放大动画
- (void)zoomin
{
[UIView animateWithDuration:.25 animations:^{
[self.currentItem setFrame:self.view.bounds];
}completion:^(BOOL finished) {
//完成需完成的动作--恢复到原先的视图上
[self.currentItem setFrame:self.currentItem.originFrame];
[self.imgView addSubview:self.currentItem];
//显示滚动视图
_scrollView.hidden=NO;
[_scrollView scrollRectToVisible:CGRectMake(self.view.bounds.size.width*self.currentItem.index, 0, self.view.bounds.size.width, self.view.bounds.size.height) animated:NO];
}];
}
#pragma mark--scrollDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
_index=scrollView.contentOffset.x/scrollView.frame.size.width;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end