码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UICollectionView 入门 07 点击cell放大图片

时间:2015-03-19 22:08:48      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:ios   uicollectionview   

这一节,我们实现通过点击图片将图片放大显示的功能。

首先我们创建一个名为FlickrPhotoViewConroller的类,这个类继承于UIViewController。修改头文件内容如下:

#import <UIKit/UIKit.h>
@class FlickrPhoto;

@interface FlickrPhotoViewConroller : UIViewController
@property (nonatomic, strong) FlickrPhoto *flickrPhoto;
@end

声明outlet和action:

#import "FlickrPhotoViewConroller.h"
#import "Flickr.h"
#import "FlickrPhoto.h"

@interface FlickrPhotoViewConroller ()

@property (weak) IBOutlet UIImageView *imageView;
- (IBAction)done:(id)sender;

@end
打开storyboard,添加一个UIViewController到storyboard中,设置这个view的class为FlickrPhotoViewConroller。

从主view使用ctrl+drag创建一个到Flickr Photo view controller的modal segue。

技术分享

选中这个segue,设置其identifier为ShowFlickrPhoto

技术分享


添加一个toolbar和一个imageview到view中,修改toolbar按钮标题为Done,ctrl+drag连接按钮和FlickrPhotoViewController的done:方法。

再将imageView的outlet也连接上:

技术分享

技术分享

打开ViewController.m,添加以下代码到@interface区域中:

@property (nonatomic) BOOL sharing;

修改didSelectItemAtIndexPath方法:

    if (!self.sharing)
    {
        NSString *searchTerm = self.searchs[indexPath.section];
        FlickrPhoto *photo = self.searchResults[searchTerm][indexPath.row];
        [self performSegueWithIdentifier:@"ShowFlickrPhoto" sender:photo];
        [self.collectionView deselectItemAtIndexPath:indexPath animated:YES];
    }
    else
    {
        
    }
当用户点击某幅缩略图时,执行ShowFlickrPhoto segue。
#import "FlickrPhotoViewConroller.h"
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowFlickrPhoto"])
    {
        FlickrPhotoViewConroller *flickrPhotoViewController = segue.destinationViewController;
        flickrPhotoViewController.flickrPhoto = sender;
    }
}
实现viewWillAppear方法,显示大图片到view中:

-(void)viewDidAppear:(BOOL)animated { // 1
    if(self.flickrPhoto.largeImage)
    {
        self.imageView.image = self.flickrPhoto.largeImage;
    }
    else
    { // 2
        self.imageView.image = self.flickrPhoto.thumbnail;
        // 3
        [Flickr loadImageForPhoto:self.flickrPhoto thumbnail:NO completionBlock:^(UIImage *photoImage, NSError *error) {
            if(!error) { // 4
                dispatch_async(dispatch_get_main_queue(), ^{ self.imageView.image =
                    self.flickrPhoto.largeImage;
                });
            }
        }];
    }
}
实现done方法,通过点击Done按钮返回到主view:

- (void)done:(id)sender
{
    [self.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
}

程序执行效果如下:

技术分享




iOS UICollectionView 入门 07 点击cell放大图片

标签:ios   uicollectionview   

原文地址:http://blog.csdn.net/yamingwu/article/details/44462821

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!