1. 程式人生 > >iOS collectionView相簿滑動

iOS collectionView相簿滑動

使用collectionView做一個簡單的相簿滑動demo


首先建立一個LineFlowLayout

LineFlowLayout.h

@interface LineFlowLayout : UICollectionViewFlowLayout

@end

-----------------------------------------------

#import "LineFlowLayout.h"

@implementation LineFlowLayout

//初始化時會呼叫

- (instancetype)init{

if(self = [superinit

]){

self.itemSize =CGSizeMake(150,150);

self.scrollDirection =UICollectionViewScrollDirectionHorizontal;

self.minimumInteritemSpacing =50;

self.minimumLineSpacing =50;

    }

returnself;

}

//UIcollectionView只要一滑動就會詢問是否重新整理介面

//重新整理介面時會呼叫 layoutAttributesForElementsInRect方法

- (BOOL)shouldInvalidateLayoutForBoundsChange:(

CGRect)newBounds{

returnYES;

}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{

//    CGFloat centerX = self.collectionView.center.x;

CGFloat screenCenterX =self.collectionView.contentOffset.x +self.collectionView.bounds.size.width * 0.5;

//

獲得所有cell的屬性

NSArray *array = [superlayoutAttributesForElementsInRect:rect];

for (UICollectionViewLayoutAttributes *attrsin array) {

//ABS 取絕對值方法

CGFloat offset = ABS(screenCenterX - attrs.center.x);

        offset = offset / 100;

CGFloat scale =1.0 + 0.5 * (1.0 - offset);

//形變屬性

        attrs.transform =CGAffineTransformMakeScale(scale, scale);

    }

return array;

}

@end

然後就在viewController裡呼叫 -----------------------------------------------

#import "ViewController.h"

#import "LineFlowLayout.h"

@interfaceViewController ()<UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

LineFlowLayout *layout = [[LineFlowLayoutalloc] init];

UICollectionView *collectionView = [[UICollectionViewalloc] initWithFrame:CGRectMake(0,200, self.view.frame.size.width,300) collectionViewLayout:layout];

    [self.viewaddSubview:collectionView];

    collectionView.dataSource =self;

NSString *ID = @"cell";

    [collectionView registerClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:ID];

}

#pragma mark -- UICollectionViewDataSource 資料來源方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return50;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

NSString *ID = @"cell";

UICollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:IDforIndexPath:indexPath];

    cell.backgroundColor = [UIColorredColor];

return cell;

}

@end



相關推薦

iOS collectionView相簿滑動

使用collectionView做一個簡單的相簿滑動demo 首先建立一個LineFlowLayout LineFlowLayout.h @interface LineFlowLayout : UICollectionViewFlowLayout @end

iOS--CollectionView添加類似TableView的TableViewHeaderView 和 SectionHeader 的置頂懸停效果

inter 頂部 edge .net 但是 con pan mint ont 1.懸停效果    http://blog.csdn.net/liangyc0129/article/details/51781722   主要參考此博客,使用 JHHeaderFlowLayou

ios浮層滑動不流暢解決方案

viewport -c tle log capable ext cap col 技術分享 前段時間做了一個浮層,但在ios上,浮層滑動不流暢,基本上是隨著手指的移動而移動,經研究加上-webkit-overflow-scrolling: touch即可 eg: <

iOS UITableView橫向滑動點擊UIButton放大

leg lec cli oid The indicator cor 進行 ace 上篇博客已經介紹了如何使用UITableView橫屏滑動,本篇博客加上了點擊放大的操作,是放在UITableViewCell的cell上的,因為可視化控件太多了,故此隱藏掉,使

週記7——ios中picker滑動穿透bug

  問題:在使用mint-ui的picker元件時,datepicker和picker在ios的webview(bug是在Hybrid App發現的)中都會出現滑動穿透的bug,導致整個頁面都會滾動,這使得使用者體驗很不好。    解決思路:由於picker元件的滾動是用touch事件

周記7——ios中picker滑動穿透bug

handle phone 頁面滾動 ole add auto web 禁用 min Bug:使用mint-ui的picker組件時,datepicker和picker在ios的webview(bug是在Hybrid App發現的)中會出現滑動穿透的現象,導致彈層後面的頁面也

iOS 讀取相簿二維碼

   iOS讀取相簿二維碼請使用官方的方法.現在開始練習起來. func beginQRImage(image:UIImage) -> Void { let detector = CIDetector(ofType: CIDet

iOS 掃描相簿圖片二維碼

級別:★★☆☆☆ 標籤:「iOS CIDetector」「CIQRCodeFeature」「識別相簿圖片」 作者: Xs·H 審校: QiShare團隊 接上篇 iOS 掃描二維碼/條形碼,本文補充介紹掃描相簿圖片上二維碼的實現方式。先看看QiQRCode中的示例效果:

解決IOS因iframe滑動引起的橡皮筋效果

這個在上一篇文章也提過,IOS應用有一個功能需要預覽郵件內容,郵件內容是iframe巢狀,並且使用scale進行縮放,在iframe上使用mailbox包含這個ifrme;在box上添加了相應的樣式,使得這個iframe可以進行滑動;但是這樣就將我們之前禁止的橡皮筋效果有恢復了,而且只有這個頁面才有

swiper 立體3D相簿滑動效果

<link rel="stylesheet" type="text/css" href="https://www.swiper.com.cn/dist/css/swiper.min.css"> body { margin: 0; pa

IOS開發中滑動頁面時NSTimer停止的問題

我們在做倒計時的時候,發現當你手指按著螢幕不放,拖動tableView滑動的時候,寫在cell上得倒計時停止倒計時,鬆開繼續倒計時。研究發現就是拖動tableView滑動時,NSTimer停止了。 這其實就是runloop的mode在做怪。runloop可以理解為cocoa下的一種訊息迴圈機制,用來處理各種

iOS collectionView Section Header 設定

#import "Cell.h" static NSString *ident = @"Cell"; #import "HeaderCollectionReusableView.h" static NSString *head = @"HeaderCollection

開啟/關閉 iOS 原生 左滑動 pop 檢視功能

-(void) baseInteractivePopGestureRecognizerEnable:(BOOL) enable{ if ([self.navigationController respondsToSelector:@selector(in

iOS 瀏覽相簿功能實現 —— HERO部落格

iOS 瀏覽相簿功能實現,可縮放,畫筆標記,快取圖片,記錄下載進度。 首先看一下效果圖:        下面簡述下主要思路及相關程式碼: HWPhotoVC(控制器,用collcetView展示縮圖,點選cell展示大圖片): #import <UIKit/UI

iOS相簿中匯入二維碼圖片識別

使用的第三方庫ZXingObjC(https://github.com/TheLevelUp/ZXingObjC)進行識別,主要程式碼如下: CGImageRef imageToDecode = image.CGImage; // Given a CGImage in

仿ios簡訊列表滑動出現刪除按鈕

最近還沒找到工作所以在宿舍有點閒,所以呢就自己寫了這麼一個例子。之前網上有很多人寫過類似的文章或demo,github上面也有開源專案。但是,老是copy別人程式碼也怪沒意思的,於是就自己嘗試著自己寫唄。廢話多了先來看下效果咯: 執行效果: 1、實現思路

iOS開啟相簿多選、圖片瀏覽器、圖片裁剪

資源類 PHPhotoLibrary 是一個資源庫。能夠獲取相簿許可權以及對相簿的操作,與AL不同,它不能獲取資源物件哦. PHFetchResult 是一個結果集,一個泛型類。通過方法獲取到的相簿或者資源組就是被封裝成該類返回. PHAssetCollecti

iOS相機相簿使用許可權

<!-- 相簿 -->   <key>NSPhotoLibraryUsageDescription</key>   <string>App需要您的同意

iOS CollectionView重新整理indexPath變化問題

今天出了一個很奇怪的問題:使用CollectionView,網路請求更新資料,使用[collectionView reloadData]方法,結果indexPath變化,導致資料錯亂問題解決辦法:使用針

[iOS]從相簿獲取以及用相機拍攝視訊並快取到沙盒

[iOS]從相簿獲取以及用相機拍攝視訊並快取到沙盒 從標題很容易發現,本文只是為了獲取視訊資料來源,以便後面能方便完成視訊上傳等操作 需要匯入AVFoundation.framework(獲取資料來源), MediaPlayer.framework