博客> 九宫格图片点击放大显示
九宫格图片点击放大显示
2017-11-22 17:13 评论:0 阅读:154 飞翔信念
ios 九宫格图片点击放大显示

这段要写一个九宫格图片放大显示的功能,就自己定义了个view简单实现了一下,完整代码已放在了github上,链接: https://github.com/15294871542/PictureDetail.git

自定义代码可以在git上自己看,这里直接上调用代码:

**导入头文件** #import "PictureView.h"

**声明代理** <complentDelegate>

UIImageView * creatImageView;//新创建的imageview
@property (strong,nonatomic) PictureView * pictureView;//图片背景视图

viewwilldidload里创建详情视图:
UIWindow * window=[UIApplication sharedApplication].delegate.window;
    self.pictureView=[[PictureView alloc]initWithFrame:CGRectMake(DeviceWidth, 0, DeviceWidth, window.frame.size.height)];
    self.pictureView.delegate=self;
    [window addSubview:self.pictureView];

//图片点击事件
-(void)selectedRow:(NSInteger)row item:(MyCollectionViewCell *)item index:(NSInteger)index
{
    //获取当前点击的图片并创建一模一样的一个
    UIWindow * window=[UIApplication sharedApplication].delegate.window;
    creatImageView = [[UIImageView alloc] init];
    CGRect new_rect = [item.theImageView convertRect:item.theImageView.bounds toView:window];
    creatImageView.image = item.theImageView.image;
    creatImageView.frame = new_rect;
    [window addSubview:creatImageView];

    //更新图片背景视图
    self.pictureView.frame=CGRectMake(0, 0, DeviceWidth, window.frame.size.height);

    //关闭
    __weak typeof (self) weakSelf = self;
    __weak typeof (creatImageView) weakImageView = creatImageView;
    self.pictureView.backBlock = ^ (NSInteger theIndex) {

        weakSelf.pictureView.frame=CGRectMake(DeviceWidth, 0, DeviceWidth, window.frame.size.height);

        //获取当前图片所在区行
        MyTableViewCell * cell=(MyTableViewCell *)[weakSelf.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]];
        MyCollectionViewCell * theCell=(MyCollectionViewCell *)[cell.myCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:theIndex inSection:0]];
        CGRect theRect = [theCell.theImageView convertRect:theCell.theImageView.bounds toView:window];
        weakImageView.image = theCell.theImageView.image;

        //放大新创建的图片
        [UIView animateWithDuration:0.3 animations:^{

            weakImageView.frame = theRect;

        } completion:^(BOOL finished) {

            [weakSelf.pictureView.picScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [weakImageView removeFromSuperview];

        }];

    };

    float imageHeight=creatImageView.image.size.height/creatImageView.image.size.width*DeviceWidth;

    //放大新创建的图片
    [UIView animateWithDuration:0.3 animations:^{

        creatImageView.bounds = CGRectMake(0, 0, DeviceWidth, imageHeight);
        creatImageView.center=CGPointMake(DeviceWidth/2, window.frame.size.height/2);

    } completion:^(BOOL finished) {

        //要展示的图片数组
        self.pictureView.picArray=dataArray[index];
        self.pictureView.currentIndex=row;

    }];
}

-(void)complent
{
    UIWindow * window=[UIApplication sharedApplication].delegate.window;
    [window bringSubviewToFront:self.pictureView];
}****

我的图片是tableview的cell上嵌套了个collectionview,这个方法是定义的一个代理方法,具体看github上的完整代码,如有bug欢迎指出。
收藏
1
sina weixin mail 回到顶部