【移動(dòng)應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中如何自定制圖片瀏覽器_第1頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中如何自定制圖片瀏覽器_第2頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中如何自定制圖片瀏覽器_第3頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中如何自定制圖片瀏覽器_第4頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中如何自定制圖片瀏覽器_第5頁(yè)
已閱讀5頁(yè),還剩6頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論