首先写一个类 写放大缩小的方法
// // 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]; }
时间: 2024-10-07 20:41:25