iOS 點(diǎn)擊圖片放大效果的實(shí)現(xiàn)
今天帶來(lái)的是圖片點(diǎn)擊放大效果,這種效果一般在微博,微信朋友圈中比較常見(jiàn)

當(dāng)我點(diǎn)擊其中一張圖片時(shí),就會(huì)進(jìn)入詳情

具體實(shí)現(xiàn)如下
首先創(chuàng)建個(gè) Controller(PhotoViewController)
// // PhotoViewController.h // 點(diǎn)擊圖片放大效果 // // Created by Amydom on 17/1/9. // Copyright © 2017年 Amydom. All rights reserved. // #import <UIKit/UIKit.h> @interface PhotoViewController : UIViewController //保存圖片的數(shù)組 @property (nonatomic, strong)NSMutableArray *photoArr; //圖片 tag @property (nonatomic, assign)NSInteger imageTag; @end
//
// PhotoViewController.m
// 點(diǎn)擊圖片放大效果
//
// 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];
//根據(jù)tag 來(lái)獲取當(dāng)前點(diǎn)擊的圖片
myScrollView.contentOffset = CGPointMake(self.view.frame.size.width * self.imageTag, 10);
myScrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.photoArr.count, 667);
//創(chuàng)建
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];
//自適應(yīng)圖片大小
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 中創(chuàng)建四張小圖片,添加輕拍手勢(shì)
//
// ViewController.m
// 點(diǎn)擊圖片放大效果
//
// 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 ;//獲取當(dāng)前被點(diǎn)擊圖片的 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
這樣就可以實(shí)現(xiàn)啦........當(dāng)然這里只是單純的實(shí)現(xiàn)功能,至于想要圖片循環(huán)什么的還是需要根據(jù)需求自行添加..
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS實(shí)現(xiàn)點(diǎn)擊圖片放大和長(zhǎng)按保存圖片的示例
- 解決iOS11圖片下拉放大出現(xiàn)信號(hào)欄白條的bug問(wèn)題
- 利用iOS手勢(shì)與scrollView代理實(shí)現(xiàn)圖片的放大縮小
- iOS tableView實(shí)現(xiàn)頂部圖片拉伸效果
- iOS tableview實(shí)現(xiàn)頂部拉伸效果
- iOS TableView頭視圖根據(jù)偏移量下拉縮放效果
- iOS tableView實(shí)現(xiàn)頭部拉伸并改變導(dǎo)航條漸變色
- iOS應(yīng)用開(kāi)發(fā)中UITableView的分割線的一些設(shè)置技巧
- IOS UITableView和UITableViewCell的幾種樣式詳細(xì)介紹
- iOS tableView實(shí)現(xiàn)下拉圖片放大效果
相關(guān)文章
IOS給xcode工程關(guān)聯(lián)pod的實(shí)例詳解
這篇文章主要介紹了IOS給xcode工程關(guān)聯(lián)pod的實(shí)例詳解的相關(guān)資料,希望大家通過(guò)本文能實(shí)現(xiàn)這樣的需求,需要的朋友可以參考下2017-09-09
iOS實(shí)現(xiàn)頂部標(biāo)簽式導(dǎo)航欄及下拉分類(lèi)菜單
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)頂部標(biāo)簽式導(dǎo)航欄及下拉分類(lèi)菜單的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
解決iOS調(diào)起微信支付顯示系統(tǒng)繁忙問(wèn)題
這篇文章主要介紹了解決iOS調(diào)起微信支付顯示系統(tǒng)繁忙問(wèn)題,需要的朋友可以參考下2016-12-12
iOS開(kāi)發(fā)中使用屏幕旋轉(zhuǎn)功能的相關(guān)方法
這篇文章主要介紹了iOS開(kāi)發(fā)中使用屏幕旋轉(zhuǎn)功能的相關(guān)方法,包括Transform變化矩陣原理的講解,需要的朋友可以參考下2015-09-09
IOS開(kāi)發(fā)之JSON轉(zhuǎn)PLIST實(shí)例詳解
這篇文章主要介紹了IOS開(kāi)發(fā)之JSON轉(zhuǎn)PLIST實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11
Reactnative-iOS回調(diào)Javascript的方法
這篇文章主要介紹了Reactnative-iOS回調(diào)Javascript的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
iOS搭建簡(jiǎn)易購(gòu)物車(chē)頁(yè)面
這篇文章主要為大家詳細(xì)介紹了iOS搭建簡(jiǎn)易購(gòu)物車(chē)頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

