




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中如何自定制圖片瀏覽器
實(shí)現(xiàn)原理示例代碼#import
<UIKit/UIKit.h>
#import
"RHPhotoBrowser.h"
@interface
RHPhotoBrowserController
:
UIViewController
-
(instancetype)initWithType:(RHPhotoSourceType)type
imageArr:(NSArray
*)imageArr
selectIndex:(NSInteger)selectIndex;
@end#import
"RHPhotoBrowserController.h"
#import
"RHPhotoBrowserCell.h"
#define
Cell_PhotoBrowser
@"Cell_PhotoBrowser"
#define
PhotoSpace
10
//
圖片間距
@interface
RHPhotoBrowserController
()
<UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout>
@property
(nonatomic,
strong)
UICollectionView
*
collection;
@property
(nonatomic,
strong)
UIPageControl
*
pageControl;
@property
(nonatomic,
strong)
NSMutableArray
*
dataArr;
@property
(nonatomic,
assign)
RHPhotoSourceType
type;
@property
(nonatomic,
assign)
NSInteger
selectIndex;
@property
(nonatomic,
assign)
CGFloat
panCenterX;
@property
(nonatomic,
assign)
CGFloat
startOffsetX;
@property
(nonatomic,
assign)
CGFloat
offsetX;
@property
(nonatomic,
assign)
CGFloat
panX;
@end
@implementation
RHPhotoBrowserController
-
(instancetype)initWithType:(RHPhotoSourceType)type
imageArr:(NSArray
*)imageArr
selectIndex:(NSInteger)selectIndex
{
self
=
[super
init];
if
(self)
{
[self.dataArr
removeAllObjects];
[self.dataArr
addObjectsFromArray:imageArr];
_type
=
type;
_selectIndex
=
selectIndex;
}
return
self;
}
-
(void)viewDidLoad
{
[super
viewDidLoad];
//
Do
any
additional
setup
after
loading
the
view.
[self
addSubviews];
[self
makeConstraintsForUI];
}
#pragma
mark
-
add
subviews
-
(void)addSubviews
{
self.view.backgroundColor
=
[UIColor
blackColor];
[self.view
addSubview:self.collection];
[self.view
addSubview:self.pageControl];
}
-
(void)makeConstraintsForUI
{
[_collection
mas_makeConstraints:^(MASConstraintMaker
*make)
{
make.top.left.right.bottom.mas_equalTo(0);
}];
[_pageControl
mas_makeConstraints:^(MASConstraintMaker
*make)
{
make.left.right.mas_equalTo(0);
make.bottom.mas_equalTo(-SS(50));
make.height.mas_equalTo(20);
}];
[self
performSelector:@selector(setCollectionContentOffset)
withObject:nil
afterDelay:0.1];
}
-
(void)setCollectionContentOffset
{
RHWeakSelf;
dispatch_async(dispatch_get_main_queue(),
^{
[weakSelf.collection
setContentOffset:CGPointMake((Screen_Width
+
PhotoSpace)
*
_selectIndex,
0)
animated:NO];
weakSelf.pageControl.numberOfPages
=
weakSelf.dataArr.count;
weakSelf.pageControl.currentPage
=
_selectIndex;
});
_startOffsetX
=
_collection.contentOffset.x;
}
#pragma
mark
-
GestureRecognizer
event
-
(void)panCollection:(UIPanGestureRecognizer
*)pan
{
_panCenterX
=
[pan
translationInView:self.collection].x;
if
(pan.state
==
UIGestureRecognizerStateBegan)
{
_startOffsetX
=
_collection.contentOffset.x;
_offsetX
=
0;
_panX
=
0;
}
if
(_selectIndex
==
0)
{
if
(_panCenterX
>
0)
{
CGFloat
s
=
(Screen_Width
-
_panCenterX)
/
Screen_Width;
_offsetX
+=
(_panCenterX
-
_panX)
*
s;
_panX
=
_panCenterX;
[self.collection
setContentOffset:CGPointMake(-_offsetX,
0)
animated:NO];
}
else
{
if
(self.dataArr.count
==
1)
{
CGFloat
s
=
(Screen_Width
+
_panCenterX)
/
Screen_Width;
_offsetX
+=
(_panCenterX
-
_panX)
*
s;
_panX
=
_panCenterX;
[self.collection
setContentOffset:CGPointMake(-_offsetX,
0)
animated:NO];
}
else
{
[self.collection
setContentOffset:CGPointMake(_startOffsetX
-
_panCenterX,
0)
animated:NO];
}
}
}
else
if
(_selectIndex
==
self.dataArr.count
-
1)
{
if
(_panCenterX
<
0)
{
CGFloat
s
=
(Screen_Width
+
_panCenterX)
/
Screen_Width;
_offsetX
+=
(_panCenterX
-
_panX)
*
s;
_panX
=
_panCenterX;
[self.collection
setContentOffset:CGPointMake(_startOffsetX
-
_offsetX,
0)
animated:NO];
}
else
{
[self.collection
setContentOffset:CGPointMake(_startOffsetX
-
_panCenterX,
0)
animated:NO];
}
}
else
{
[self.collection
setContentOffset:CGPointMake(_startOffsetX
-
_panCenterX,
0)
animated:NO];
}
if
(pan.state
==
UIGestureRecognizerStateEnded)
{
if
([self
absoluteValue:_panCenterX]
>
Screen_Width/3)
{
if
(_panCenterX
<
0)
{
_selectIndex
+=
1;
}
else
{
_selectIndex
-=
1;
}
if
(_selectIndex
==
self.dataArr.count)
{
_selectIndex
=
self.dataArr.count
-
1;
}
else
if
(_selectIndex
==
-1)
{
_selectIndex
=
0;
}
[self.collection
setContentOffset:CGPointMake((Screen_Width
+
PhotoSpace)
*
_selectIndex,
0)
animated:YES];
self.pageControl.currentPage
=
_selectIndex;
}
else
{
[self.collection
setContentOffset:CGPointMake(_startOffsetX,
0)
animated:YES];
}
}
}
-
(void)swipeCollection:(UISwipeGestureRecognizer
*)swipe
{
if
(swipe.direction
==
UISwipeGestureRecognizerDirectionLeft)
{
_selectIndex
+=
1;
}
else
if
(swipe.direction
==
UISwipeGestureRecognizerDirectionRight)
{
_selectIndex
-=
1;
}
if
(_selectIndex
==
self.dataArr.count)
{
_selectIndex
=
self.dataArr.count
-
1;
}
else
if
(_selectIndex
==
-1)
{
_selectIndex
=
0;
}
self.pageControl.currentPage
=
_selectIndex;
[self.collection
setContentOffset:CGPointMake((Screen_Width
+
PhotoSpace)
*
_selectIndex,
0)
animated:YES];
}
//
返回value的絕對(duì)值
-
(CGFloat)absoluteValue:(CGFloat)value
{
if
(value
<
0)
{
return
-value;
}
return
value;
}
#pragma
mark
-
collection
delegate
-
(NSInteger)collectionView:(UICollectionView
*)collectionView
numberOfItemsInSection:(NSInteger)section
{
return
self.dataArr.count;
}
-
(UICollectionViewCell
*)collectionView:(UICollectionView
*)collectionView
cellForItemAtIndexPath:(NSIndexPath
*)indexPath
{
RHPhotoBrowserCell
*
cell
=
[collectionView
dequeueReusableCellWithReuseIdentifier:Cell_PhotoBrowser
forIndexPath:indexPath];
if
(indexPath.row
<
self.dataArr.count)
{
if
(_type
==
RHPhotoSourceTypeImage)
{
UIImage
*
image
=
[self.dataArr
objectAtIndex:indexPath.row];
[cell
configCellWithImage:image];
}
else
if
(_type
==
RHPhotoSourceTypeUrl)
{
NSString
*
url
=
[self.dataArr
objectAtIndex:indexPath.row];
[cell
configCellWithUrl:url];
}
else
if
(_type
==
RHPhotoSourceTypeFilePath)
{
NSString
*
filePath
=
[self.dataArr
objectAtIndex:indexPath.row];
[cell
configCellWithFilePath:filePath];
}
else
if
(_type
==
RHPhotoSourceTypeFileName)
{
NSString
*
fileName
=
[self.dataArr
objectAtIndex:indexPath.row];
[cell
configCellWithFileName:fileName];
}
}
return
cell;
}
-
(CGSize)collectionView:(UICollectionView
*)collectionView
layout:(UICollectionViewLayout
*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath
*)indexPath
{
return
CGSizeMake(Screen_Width,
Screen_Height);
}
-
(CGFloat)collectionView:(UICollectionView
*)collectionView
layout:(UICollectionViewLayout
*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return
PhotoSpace;
}
-
(CGFloat)collectionView:(UICollectionView
*)collectionView
layout:(UICollectionViewLayout
*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return
0;
}
-
(void)collectionView:(UICollectionView
*)collectionView
didSelectItemAtIndexPath:(NSIndexPath
*)indexPath
{
[self
dismissViewControllerAnimated:YES
completion:nil];
}
#pragma
mark
-
setter
and
getter
-
(UICollectionView
*)collection
{
if
(!_collection)
{
UICollectionViewFlowLayout
*
layout
=
[[UICollectionViewFlowLayout
alloc]
init];
layout.scrollDirection
=
UICollectionViewScrollDirectionHorizontal;
UICollectionView
*
cv
=
[[UICollectionView
alloc]
initWithFrame:CGRectZero
collectionViewLayout:layout];
cv.backgroundColor
=
[UIColor
blackColor];
cv.delegate
=
self;
cv.dataSource
=
self;
cv.showsHorizontalScrollIndicator
=
NO;
[cv
registerClass:[RHPhotoBrowserCell
class]
forCellWithReuseIdentifier:Cell_PhotoBrowser];
UIPanGestureRecognizer
*
pan
=
[[UIPanGestureRecognizer
alloc]
initWithTarget:self
action:@selector(panCollection:)];
[cv
addGestureRecognizer:pan];
UISwipeGestureRecognizer
*
swipeL
=
[[UISwipeGestureRecognizer
alloc]
initWithTarget:self
action:@selector(swipeCollection:)];
swipeL.direction
=
UISwipeGestureRecognizerDirectionLeft;
[cv
addGestureRecognizer:swipeL];
UISwipeGestureRecognizer
*
swipeR
=
[[UISwipeGestureRecognizer
alloc]
initWithTarget:self
action:@selector(swipeCollection:)];
swipeR.direction
=
UISwipeGestureRecognizerDirectionRight;
[cv
addGestureRecognizer:swipeR];
_collection
=
cv;
}
return
_collection;
}
-
(UIPageControl
*)pageControl
{
if
(!_pageControl)
{
UIPageControl
*
pageControl
=
[[UIPageControl
alloc]
init];
pageControl.pageIndicatorTintColor
=
[[UIColor
lightGrayColor]
colorWithAlphaComponent:0.9];
pageControl.currentPageIndicatorTintColor
=
[UIColor
whiteColor];
pageControl.userInteractionEnabled
=
NO;
_pageControl
=
pageControl;
}
return
_pageControl;
}
-
(NSMutableArray
*)dataArr
{
if
(!_dataArr)
{
_dataArr
=
[NSMutableArray
array];
}
return
_dataArr;
}
@end#import
<Foundation/Foundation.h>
typedef
NS_ENUM(NSUInteger,
RHPhotoSourceType)
{
RHPhotoSourceTypeImage
=
0,
RHPhotoSourceTypeUrl
=
1,
RHPhotoSourceTypeFilePath
=
2,
RHPhotoSourceTypeFileName
=
3
};
@interface
RHPhotoBrowser
:
NSObject
+
(RHPhotoBrowser
*)shared;
-
(void)browseImageWithType:(RHPhotoSourceType)type
imageArr:(NSArray
*)imag
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權(quán)】 ISO 10713:2025 EN Jewellery and precious metals - Gold alloy coatings
- T-ZHAQ 8-2024 小葉牛大力種植技術(shù)規(guī)程
- 二零二五年度應(yīng)屆大學(xué)生人力資源實(shí)習(xí)合同
- 二零二五年度股票投資風(fēng)險(xiǎn)控制與合規(guī)監(jiān)督協(xié)議
- 二零二五年度個(gè)人債權(quán)轉(zhuǎn)讓協(xié)議書(關(guān)于專利權(quán)轉(zhuǎn)讓)
- 高管二零二五年度勞動(dòng)合同及離職交接程序
- 二零二五年度路橋工程土地征用與拆遷合同
- 美容院合伙人投資回報(bào)與風(fēng)險(xiǎn)控制協(xié)議書(2025年度)
- 2025年度金融借款合同違約起訴流程及費(fèi)用結(jié)算合同
- 2025年度餐飲企業(yè)跨界合作合伙經(jīng)營(yíng)合同
- 《職業(yè)技能等級(jí)評(píng)價(jià)規(guī)范編制指南編制說(shuō)明》
- 畜禽養(yǎng)殖場(chǎng)惡臭污染物排放及其處理技術(shù)研究進(jìn)展
- 超聲內(nèi)鏡引導(dǎo)下穿刺活檢術(shù)的配合及護(hù)理
- 新生兒常見的產(chǎn)傷及護(hù)理
- 代寫回憶錄合同
- 2024年10月自考00149國(guó)際貿(mào)易理論與實(shí)務(wù)試題及答案
- 2024年下半年教師資格考試《中學(xué)教育知識(shí)與能力》真題及答案解析
- 物業(yè)保潔常用藥劑MSDS
- 《跨文化溝通》課件
- (一模)長(zhǎng)春市2025屆高三質(zhì)量監(jiān)測(cè)(一)數(shù)學(xué)試卷
- 2024-2025學(xué)年湖北省武漢市華中師大一附中高三上學(xué)期10月檢測(cè)英語(yǔ)試題及答案
評(píng)論
0/150
提交評(píng)論