點擊圖片進行放大,再次點擊就縮小到原來的地方,放大縮小

來源:互聯網
上載者:User

點擊圖片進行放大,再次點擊就縮小到原來的地方,放大縮小
首先寫一個類  寫放大縮小的方法

////  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 BigImagestatic 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];}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.