iOS開發筆記:實現點選圖片放大全屏
阿新 • • 發佈:2019-02-16
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
//設定放大後圖片的寬高,為了省時間,偷了下懶,建議最好結合實際做下運算
#define BIG_IMG_WIDTH 325
#define BIG_IMG_HEIGHT 325
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *minImageView;
@end
@implementation ViewController{
UIView *background;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//允許使用者互動
_minImageView.userInteractionEnabled = YES;
//新增點選手勢
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
[_minImageView addGestureRecognizer:tapGesture];
}
//點選圖片後的方法(即圖片的放大全屏效果)
- (void) tapAction{
//建立一個黑色背景
//初始化一個用來當做背景的View。我這裡為了省時間計算,寬高直接用的5s的尺寸
UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 600)];
background = bgView;
[bgView setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:bgView];
//建立顯示影象的檢視
//初始化要顯示的圖片內容的imageView(這裡位置繼續偷懶...沒有計算)
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, BIG_IMG_WIDTH, BIG_IMG_HEIGHT)];
//要顯示的圖片,即要放大的圖片
[imgView setImage:[UIImage imageNamed:@"dog"]];
[bgView addSubview:imgView];
imgView.userInteractionEnabled = YES;
//新增點選手勢(即點選圖片後退出全屏)
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)];
[imgView addGestureRecognizer:tapGesture];
[self shakeToShow:bgView];//放大過程中的動畫
}
-(void)closeView{
[background removeFromSuperview];
}
//放大過程中出現的緩慢動畫
- (void) shakeToShow:(UIView*)aView{
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.duration = 0.5;
NSMutableArray *values = [NSMutableArray array];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
animation.values = values;
[aView.layer addAnimation:animation forKey:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end