- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(j * 120 + 100, i * 150 + 200, 120, 150)];
imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",j+1+i+i]];
imgView.tag = 100 + j+i+i;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(large:)];
imgView.userInteractionEnabled = YES;
[imgView addGestureRecognizer:tap];
[self.view addSubview:imgView];
}
}
}
- (void)large:(UITapGestureRecognizer *)tap
{
//获取四张图片交互都关掉
for(int i = 0; i < 4; i++)
{
UIImageView *imgView = (UIImageView *)[self.view viewWithTag:100+i];
imgView.userInteractionEnabled = NO;
}
//打开被点击的图片的交互
tap.view.userInteractionEnabled = YES;
//判断是否放大
if (_isLarge == NO) {
//放大前记录视图原先的frame
_frame = tap.view.frame;
[UIView animateWithDuration:1.0 animations:^{
//把点击的图片放到父视图的最上层
[self.view bringSubviewToFront:tap.view];
tap.view.frame = self.view.bounds;
} completion:nil];
}else
{
[UIView animateWithDuration:1.0 animations:^{
tap.view.frame = _frame;
} completion:^(BOOL finished) {
//在图片缩小完成的时候打开所有图片的交互
for(int i = 0; i < 4; i++)
{
UIImageView *imgView = (UIImageView *)[self.view viewWithTag:100+i];
imgView.userInteractionEnabled = YES;
}
}];
}
_isLarge = !_isLarge;
}
图片点击放大效果
最新推荐文章于 2023-07-12 19:49:54 发布