码迷,mamicode.com
首页 > 其他好文 > 详细

点击图片进行放大,再次点击就缩小到原来的地方

时间:2015-04-08 13:14:50      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:图片   imageview   

首先写一个类  写放大缩小的方法
//
//  BigImage.h
//  TapImageBigAndSmall
//
//  Created by lxy on 15-4-7.
//  Copyright (c) 2015年 Shenzhen MSD Technology Co.,LTD. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface BigImage : NSObject

+(void)showImage:(UIImageView *)avatarImageView;
+(void)hideImage:(UITapGestureRecognizer*)tap;
@end

实现方法

//
//  BigImage.m
//  TapImageBigAndSmall
//
//  Created by lxy on 15-4-7.
//  Copyright (c) 2015年 Shenzhen MSD Technology Co.,LTD. All rights reserved.
//

#import "BigImage.h"

@implementation BigImage

static CGRect oldframe;

+(void)showImage:(UIImageView *)avatarImageView
{
    UIImage *image = avatarImageView.image;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake( 0,  0,  [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    oldframe = [avatarImageView convertRect:avatarImageView.bounds toView:window];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha = 0;
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:oldframe];
    imageView.image = image;
    imageView.tag = 1;
    [backgroundView addSubview:imageView];
    [window addSubview:backgroundView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [backgroundView addGestureRecognizer: tap];
    
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
        backgroundView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
   
}

+(void)hideImage:(UITapGestureRecognizer*)tap
{
    UIView *backgroundView = tap.view;
    UIImageView *imageView = (UIImageView*)[tap.view viewWithTag:1];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame = oldframe;
        backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
    }];
    
    
}
@end


在视图控制器中

//
//  ViewController.m
//  TapImageBigAndSmall
//
//  Created by lxy on 15-4-7.
//  Copyright (c) 2015年 Shenzhen MSD Technology Co.,LTD. All rights reserved.
//

#import "ViewController.h"
#import "BigImage.h"

@interface ViewController ()
@property (nonatomic,strong)UIImageView *image;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor magentaColor];
    self.image = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    _image.image = [UIImage imageNamed:@"me"];
    _image.userInteractionEnabled = YES;
    [self.view addSubview:_image];
    
    /**
     * 给图片添加手势  点击可放大图片 
     */
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
    [self.image addGestureRecognizer:tap];
    
    /**
     * 一张图片围绕着一个圆转圈
     */
//     [self AnimationStart];
    
    
    
    

}
- (void)tapClick
{
    [BigImage showImage:_image];

}
- (void)AnimationStart
{
    UIImage *image=[UIImage imageNamed:@"me"];
    CALayer *flyStarLayer=[CALayer layer];
    flyStarLayer.bounds=CGRectMake(0, 0, 20, 20);
    flyStarLayer.contents=(id)image.CGImage;
    [self.view.layer addSublayer:flyStarLayer];
    
    CAKeyframeAnimation *keyAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.duration=5.0;
    keyAnimation.removedOnCompletion=NO;
    //    NSNumber *number1=[NSNumber numberWithFloat:0.0];
    //    NSNumber *number2=[NSNumber numberWithFloat:0.2];
    //    NSNumber *number3=[NSNumber numberWithFloat:0.8];
    //    keyAnimation.keyTimes=[NSArray arrayWithObjects:number1,number2,number3, nil];
    keyAnimation.fillMode=kCAFillModeForwards;
    keyAnimation.repeatCount=1;
    
    CGMutablePathRef circlePath=CGPathCreateMutable();
    
    CGPathAddArc(circlePath, nil, 100, 150, 50, 0, 4*M_PI, 0);
    keyAnimation.path=circlePath;
    CGPathRelease(circlePath);
    [flyStarLayer addAnimation:keyAnimation forKey:nil];

}



点击图片进行放大,再次点击就缩小到原来的地方

标签:图片   imageview   

原文地址:http://blog.csdn.net/lixianyue1991/article/details/44938379

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