APP中圖片瀏覽功能是比較常見的,為了使用方便,自己封裝了一個(gè),僅供大家參考。主要的功能有:
1、動(dòng)畫效果放大先看幾張效果圖吧。

2、點(diǎn)擊指定區(qū)域放大

3、下拉退出

一、集成方法
1、pod集成
pod 'CKYPhotoBrowser'
如果提示not found之類的提示,先使用 pod search CKYPhotoBrowser 去查找一下,若提示
[!] Unable to find a pod with name, author, summary, or description matching `CKYPhotoBrowser`
此時(shí)需要更新本地的pod空間,終端執(zhí)行指令 pod repo update,成功之后,在執(zhí)行pod search CKYPhotoBrowser,若還是沒有找到,需刪除pod本地緩存,執(zhí)行rm ~/Library/Caches/CocoaPods/search_index.json,之后在執(zhí)行pod search CKYPhotoBrowser。如果還是沒有成功,那就請留言吧,一般情況,到這里一定是成功的了。
2、源碼集成
github下載地址:https://github.com/cky113999742/KYPhotoBrowser
下載完成,直接拖進(jìn)工程里,如果工程中沒有YYWebImage,需要手動(dòng)去添加這個(gè)庫,如果使用SDWebImage,可以直接修改源碼,替換掉YYWebImage,個(gè)人覺得YYWebImage性能更好一些,尤其是在iPhone 7上的表現(xiàn)。
二、簡單使用
使用時(shí),只需要引用 KYPhotoBrowserController.h 即可。
NSArray *images = @[@"http://ohc6xoujj.bkt.clouddn.com/image_1.jpg", @"http://ohc6xoujj.bkt.clouddn.com/image_2.jpg"];
[KYPhotoBrowserController showPhotoBrowserWithImages:images currentImageIndex:indexPath.item delegate:self];
如果需要實(shí)現(xiàn)動(dòng)畫出現(xiàn)和消失回到原位的效果,需要實(shí)現(xiàn)代理方法:
// 動(dòng)畫消失的目標(biāo)frame
- (UIImageView *)sourceImageViewForIndex:(NSInteger)index;
返回指定位置的 UIImageView,這個(gè)UIImageView就是當(dāng)前正在顯示的那張圖片,只是用于獲取他在試圖中的坐標(biāo)位置。使用就是這幾行代碼就可以了, KYPhotoBrowserController 類中暴露了兩個(gè)只讀屬性scrollView和pageLabel,暴露的目的是如果使用者不需要頁碼指示器時(shí)候,可以通過實(shí)現(xiàn) customUIBlock 進(jìn)行相應(yīng)的隱藏或者調(diào)整坐標(biāo), scrollView是方便用戶對手勢進(jìn)行擴(kuò)展,比如需要添加長按手勢,手勢可以添加在scrollView上,在外部實(shí)現(xiàn)自己的手勢方法,達(dá)到擴(kuò)展的需求。
三、 代碼結(jié)構(gòu)
1、KYPhotoBrowserController
#import <UIKit/UIKit.h>
#import "KYPhotoModel.h"
@class KYPhotoBrowserController;
typedef void(^KYCustomUIBlock)(KYPhotoBrowserController *vc);
@protocol KYPhotoBrowserControllerDelegate <NSObject>
@optional
// 動(dòng)畫消失的目標(biāo)frame
- (UIImageView *)sourceImageViewForIndex:(NSInteger)index;
// 獲取圖片展示占位圖
- (UIImage *)photoBrowserPlaceholderImage;
@end
@interface KYPhotoBrowserController : UIViewController
@property (nonatomic, copy) KYCustomUIBlock customUIBlock; /**< 在viewDidLoad的最后調(diào)用,方便用戶自定義UI */
@property (nonatomic, weak) id <KYPhotoBrowserControllerDelegate> delegate;
@property (nonatomic, strong, readonly) UIScrollView *scrollView;
@property (nonatomic, strong, readonly) UILabel *pageLabel;
/**
* 當(dāng)前顯示的圖片位置索引 , 默認(rèn)是0
*/
@property (nonatomic, assign) NSInteger currentImageIndex;
/**
* 瀏覽的圖片數(shù)量,大于0
*/
@property (nonatomic, assign) NSInteger imageCount;
/**
圖片數(shù)據(jù) 數(shù)組內(nèi)可以是 KYPhotoModel, NSImage, NSString, NSData
*/
@property (nonatomic, strong) NSArray *images;
/**
初始化的方法
@param images 圖片數(shù)據(jù) 數(shù)組內(nèi)可以是 KYPhotoModel, NSImage, NSString, NSData
@param currentImageIndex 當(dāng)前顯示的位置
*/
+ (instancetype)showPhotoBrowserWithImages:(NSArray *)images currentImageIndex:(NSInteger)currentImageIndex;
/**
初始化的方法 如需實(shí)現(xiàn)動(dòng)畫 必須實(shí)現(xiàn)代理方法
@param images 圖片數(shù)據(jù) 數(shù)組內(nèi)可以是 KYPhotoModel, NSImage, NSString, NSData
@param currentImageIndex 當(dāng)前顯示的位置
@param delegate 代理
*/
+ (instancetype)showPhotoBrowserWithImages:(NSArray *)images currentImageIndex:(NSInteger)currentImageIndex delegate:( id <KYPhotoBrowserControllerDelegate>)delegate;
/**
移除方法
@param animation 動(dòng)畫
*/
- (void)dismissAnimation:(BOOL)animation;
@end
這個(gè)類是使用者直接調(diào)用的類,內(nèi)部持有scrollView實(shí)現(xiàn)橫滑效果,scrollView上添加的是KYPhotoZoomView,這個(gè)控件是繼承自UIScrollView,內(nèi)部持有一個(gè)UIImageView用于實(shí)現(xiàn)圖片的展示和放大功能。KYPhotoBrowserController初始化時(shí)傳遞的圖片數(shù)組,數(shù)據(jù)類型支持KYPhotoModel, NSImage, NSString, NSData。
2、KYPhotoModel
@property (nonatomic, strong) NSData *imageData; /**< 圖片數(shù)據(jù) */
@property (nonatomic, strong) UIImage *image; /**< 圖片數(shù)據(jù) */
@property (nonatomic, strong) NSString *thumbURLString; /**< 普通圖下載鏈接 */
@property (nonatomic, strong) NSString *originURLString; /**< 原圖下載鏈接 */
@property (nonatomic, assign) CGFloat originImageSize; /**< 原圖的大小,單位為 B */
存放圖片數(shù)據(jù)的模型類,圖片的加載順序?yàn)椋合葯z測原圖,如果本地已經(jīng)存在原圖數(shù)據(jù),直接加載原圖數(shù)據(jù),如果不存在原圖數(shù)據(jù),直接去檢測普通圖片,如果存在網(wǎng)址,加載普通圖片,如果不存在,加載ImageData和Image的數(shù)據(jù)。原圖的數(shù)據(jù),只有在用戶點(diǎn)擊了加載原圖的按鈕,才會(huì)去加載。
3、KYPhotoZoomView
#import <UIKit/UIKit.h>
#import "KYPhotoModel.h"
#if __has_include(<YYWebImage/YYWebImage.h>)
#import <YYWebImage/YYWebImage.h>
#else
#import "YYWebImage.h"
#endif
typedef NS_ENUM(NSInteger, ShowImageState) {
ShowImageStateSmall, // 初始化默認(rèn)是小圖
ShowImageStateBig, // 全屏的正常圖片
ShowImageStateOrigin // 原圖
};
@class KYPhotoZoomView;
@protocol KYPhotoZoomViewDelegate <NSObject>
- (CGRect)dismissRect;
- (UIImage *)photoZoomViewPlaceholderImage;
@end
@interface KYPhotoZoomView : UIScrollView <UIScrollViewDelegate>
@property (nonatomic, weak) id <KYPhotoZoomViewDelegate> zoomDelegate;
@property (nonatomic, strong, readonly) UIImageView *imageView;
@property (nonatomic, assign, readonly) ShowImageState imageState;
@property (nonatomic, assign) CGFloat process;
- (void)resetScale;
- (void)showImageWithPhotoModel:(KYPhotoModel *)photoModel;
- (void)dismissAnimation:(BOOL)animation;
@end
這個(gè)類是圖片展示類,內(nèi)部持有一個(gè)ImageView,這個(gè)類的主要作用是實(shí)現(xiàn)圖片的展示、放大縮小的效果。
4、KYPhotoGestureHandle
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@class KYPhotoZoomView, KYPhotoGestureHandle;
@protocol KYPhotoGestureHandleDelegate <NSObject>
// 獲取當(dāng)前展示的圖片對象
- (KYPhotoZoomView *)currentDetailImageViewInPhotoPreview:(KYPhotoGestureHandle *)handle;
// 圖片對象去移除的代理
- (void)detailImageViewGotoDismiss;
// 控制圖片控制器中,照片墻,更多等小組件的隱藏/顯示
- (void)photoPreviewComponmentHidden:(BOOL)hidden;
@end
@interface KYPhotoGestureHandle : NSObject
@property (nonatomic, weak) id <KYPhotoGestureHandleDelegate> delegate;
- (instancetype)initWithScrollView:(UIScrollView *)scrollView coverView:(UIView *)coverView;
@end
這個(gè)類是下拉退出效果的手勢處理類,下拉圖片跟隨移動(dòng)縮小的功能的實(shí)現(xiàn),是通過下拉開始時(shí),改變圖片的錨點(diǎn),以達(dá)到圖片跟手縮小移動(dòng)的效果,在用戶松手的時(shí)候,如果縮放比例大于0.80,或者下拉速度大于800,就會(huì)退出界面。手勢結(jié)束的時(shí)候,修改錨點(diǎn)為默認(rèn)的0.5,如果是退出,執(zhí)行退出的動(dòng)畫,如果不是退出,還原圖片。
5、KYPhotoBrowserManager
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
/**
圖片瀏覽管理類
*/
@interface KYPhotoBrowserManager : NSObject
@property (nonatomic, strong) UIWindow *photoWindow;
+ (instancetype)sharedManager;
- (void)presentWindowWithController:(UIViewController *)controller;
- (void)dismissWindow:(BOOL)animation;
@end
這個(gè)類用于PhotoBrowser的展示和退出管理,PhotoBrowser是單獨(dú)使用一個(gè)Window進(jìn)行退出的,不需要使用者提供任何的控制器用于推出界面,方便使用者使用。
基本的介紹就這些了,如果有什么好的建議,請留言,希望這個(gè)控件能幫助到大家。
GitHub直通車:直通車入口-系好安全帶