iOS 点击图片放大效果的实现

所属分类: 软件编程 / IOS 阅读数: 879
收藏 0 赞 0 分享

今天带来的是图片点击放大效果,这种效果一般在微博,微信朋友圈中比较常见

当我点击其中一张图片时,就会进入详情

具体实现如下

首先创建个 Controller(PhotoViewController)

// 
// PhotoViewController.h 
// 点击图片放大效果 
// 
// Created by Amydom on 17/1/9. 
// Copyright © 2017年 Amydom. All rights reserved. 
// 
 
#import <UIKit/UIKit.h> 
 
@interface PhotoViewController : UIViewController 
 
//保存图片的数组 
@property (nonatomic, strong)NSMutableArray *photoArr; 
//图片 tag 
@property (nonatomic, assign)NSInteger imageTag; 
 
@end 
// 
// PhotoViewController.m 
// 点击图片放大效果 
// 
// Created by Amydom on 17/1/9. 
// Copyright © 2017年 Amydom. All rights reserved. 
// 
 
#import "PhotoViewController.h" 
 
@interface PhotoViewController () 
 
@end 
 
@implementation PhotoViewController 
 
- (void)viewDidLoad { 
  [super viewDidLoad]; 
   
  UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)]; 
   
  myScrollView.backgroundColor = [UIColor blackColor]; 
  myScrollView.pagingEnabled = YES; 
  myScrollView.bounces = NO; 
   
  [self.view addSubview:myScrollView]; 
  //根据tag 来获取当前点击的图片 
  myScrollView.contentOffset = CGPointMake(self.view.frame.size.width * self.imageTag, 10); 
   
  myScrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.photoArr.count, 667); 
  //创建 
  for (int i = 0; i < self.photoArr.count; i++) 
  { 
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i + 10, 0, self.view.frame.size.width - 20, self.view.frame.size.height)]; 
    NSString *imgName = self.photoArr[i]; 
    img.image = [UIImage imageNamed:imgName]; 
     
    [myScrollView addSubview:img]; 
     
    //自适应图片大小 
    img.contentMode = UIViewContentModeScaleAspectFit; 
     
  } 
   
  //轻拍跳出照片浏览 
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; 
   
  [myScrollView addGestureRecognizer:tap]; 
 
} 
 
- (void)tapAction 
{ 
  [self dismissViewControllerAnimated:YES completion:^{ 
     
     
  }]; 
} 
 
- (void)didReceiveMemoryWarning { 
  [super didReceiveMemoryWarning]; 
  // Dispose of any resources that can be recreated. 
} 
 
/* 
#pragma mark - Navigation 
 
// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
  // Get the new view controller using [segue destinationViewController]. 
  // Pass the selected object to the new view controller. 
} 
*/ 
 
@end 

然后在 ViewController 中创建四张小图片,添加轻拍手势

// 
// ViewController.m 
// 点击图片放大效果 
// 
// Created by Amydom on 17/1/9. 
// Copyright © 2017年 Amydom. All rights reserved. 
// 
 
#import "ViewController.h" 
#import "PhotoViewController.h" 
 
@interface ViewController (){ 
   
  NSMutableArray *array; 
   
} 
 
@end 
 
@implementation ViewController 
 
- (void)viewDidLoad { 
  [super viewDidLoad]; 
  self.view.backgroundColor = [UIColor whiteColor]; 
  array = [NSMutableArray arrayWithObjects:@"1.jpg", @"2.jpg",@"3.jpg",@"4.jpg", nil nil]; 
  for (int i = 0; i < array.count; i++) { 
     
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(20 + 880 * i, 100, 70, 70)]; 
    img.image = [UIImage imageNamed:[array objectAtIndex:i]]; 
     
    img.userInteractionEnabled = YES; 
     
    //截掉边框 
    img.clipsToBounds = YES; 
     
    img.tag = 1000 + i; 
     
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapAction:)]; 
     
    [img addGestureRecognizer:tap]; 
     
    [self.view addSubview:img]; 
  } 
 
} 
 
- (void)TapAction:(UITapGestureRecognizer *)tap{ 
   
  PhotoViewController *photoVC = [[PhotoViewController alloc] init]; 
  photoVC.imageTag = tap.view.tag - 1000 ;//获取当前被点击图片的 tag 
  photoVC.photoArr = array; 
  [photoVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];// 效果 
  [self presentModalViewController:photoVC animated:YES]; 
   
} 
- (void)didReceiveMemoryWarning { 
  [super didReceiveMemoryWarning]; 
  // Dispose of any resources that can be recreated. 
} 
 
 
@end 

这样就可以实现啦........当然这里只是单纯的实现功能,至于想要图片循环什么的还是需要根据需求自行添加..

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

详细整理iOS中UITableView的性能优化

最近在微博上看到一个很好的开源项目,是关于如何优化UITableView的,加上正好最近也在优化项目中的类似朋友圈功能这块,思考了很多关于UITableView的优化技巧,所以决定详细的整理下对优化UITableView的理解,需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

IOS开发中禁止NavigationController的向右滑动返回

这篇文章主要介绍了IOS开发中禁止NavigationController的向右滑动返回的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现微信/QQ显示最近拍摄图片的功能实例代码

如果你刚刚拍摄了图片,在使用微信/QQ发生消息时会显示“你可能要发送的图片”,这个功能非常人性化,怎么实现的呢?下面小编给大家分享iOS实现微信/QQ显示最近拍摄图片的功能实例代码,一起看看吧
收藏 0 赞 0 分享

iOS实现动态自适应标签

这篇文章主要为大家详细介绍了iOS动态自适应标签的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS实现图片存在本地、再从本地获取图片的功能

本文主要介绍了iOS实现图片存在本地、再从本地获取图片的功能的代码。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS视频录制(或选择)压缩及上传功能(整理)

最新做的一个功能涉及到了视频的录制、压缩及上传功能,经过大神的一番教导,终于倒腾清楚了,今天小编把问题经过记录一下分享到脚本之家平台,供大家参考
收藏 0 赞 0 分享

iOS中打包上传常见的错误与解决办法

关于打包上传至AppStore,大家都认为是最后一步了,其实到了这里往往会遇到很多的坑。对于踩过的坑我不想再踩第二遍,所以在此将我遇到的所有奇葩问题在此做一个记录,当作对自己的一个提醒,同时也分享给给位,需要的朋友可以参考下。
收藏 0 赞 0 分享

解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法

这篇文章主要介绍了关于解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法,需要的朋友可以参考下
收藏 0 赞 0 分享

Objective-C实现身份证验证的方法示例

这篇文章主要给大家分享了Objective-C实现身份证验证的方法,文中给出了详细的示例代码,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

ios启动页强制竖屏(进入App后允许横屏与竖屏)

最近工作遇到这样一个需要,当进入启动页需要强制竖屏,而进入APP后就允许横屏与竖屏,通过查找相关的资料找到了解决的方法,所以将实现的方法整理后分享出来,需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享
查看更多