最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS使用UICollectionView實現(xiàn)橫向滾動照片效果

 更新時間:2019年05月22日 10:14:55   作者:抬頭看見檸檬樹  
這篇文章主要為大家詳細介紹了iOS使用UICollectionView實現(xiàn)橫向滾動照片效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了iOS使用UICollectionView實現(xiàn)橫向滾動展示照片的具體代碼,供大家參考,具體內(nèi)容如下

這是Demo鏈接

效果圖

思路

1. 界面搭建

界面的搭建十分簡單,采用UICollectionView和自定義cell進行搭建即可。

// ViewController.m

// 下面使用到的宏和全局變量
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
static NSString *const cellID = @"cellID";

// 創(chuàng)建collectionView的代碼
- (void)setupCollectionView
{
 // 使用系統(tǒng)自帶的流布局(繼承自UICollectionViewLayout)
 UICollectionViewFlowLayout *layout = ({
 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
 // 每個cell的大小
 layout.itemSize   = CGSizeMake(180, 180);
 // 橫向滾動
 layout.scrollDirection  = UICollectionViewScrollDirectionHorizontal;
 // cell間的間距
 layout.minimumLineSpacing  = 40;

 //第一個cell和最后一個cell居中顯示(這里我的Demo里忘記改了我用的是160,最后微調(diào)數(shù)據(jù)cell的大小是180)
 CGFloat margin = (ScreenW - 180) * 0.5;
 layout.sectionInset  = UIEdgeInsetsMake(0, margin, 0, margin);

 layout;
 });

 // 使用UICollectionView必須設(shè)置UICollectionViewLayout屬性
 UICollectionView *collectionView = ({
 UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
 collectionView.center  = self.view.center;
 collectionView.bounds  = CGRectMake(0, 0, ScreenW, 200);
 collectionView.backgroundColor = [UIColor brownColor];
 // 這里千萬記得在interface哪里寫<UICollectionViewDataSource>?。。?
 collectionView.dataSource = self;
 [collectionView setShowsHorizontalScrollIndicator:NO];

 [self.view addSubview:collectionView];

 collectionView;
 });

 // 實現(xiàn)注冊cell,其中PhotoCell是我自定義的cell,繼承自UICollectionViewCell
 UINib *collectionNib = [UINib nibWithNibName:NSStringFromClass([PhotoCell class])
      bundle:nil];
 [collectionView registerNib:collectionNib
 forCellWithReuseIdentifier:cellID];
}

// UICollectionViewCellDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView
 numberOfItemsInSection:(NSInteger)section
{
 return 10;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
    cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
 PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID 
    forIndexPath:indexPath];

 // 圖片名是 0 ~ 9
 cell.imageName = [NSString stringWithFormat:@"%ld", (long)indexPath.row];

 return cell;
}
// 界面是一個xib文件,在cell里拖了個ImageView,約束上下左右都是10
// 圖片名是數(shù)字 0 ~ 9

// PhotoCell.h
@property (nonatomic, strong) NSString *imageName;

// PhotoCell.m
@interface PhotoCell ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation PhotoCell

- (void)awakeFromNib {
 [super awakeFromNib];
 // Initialization code
}

- (void)setImageName:(NSString *)imageName
{
 _imageName = imageName;

 self.imageView.image = [UIImage imageNamed:imageName];
}

到這里最基礎(chǔ)的效果就實現(xiàn)完了,一組大小相等的圖片cell。

2.大小變化已經(jīng)居中效果實現(xiàn)

由于系統(tǒng)的UICollectionViewFlowLayout無法實現(xiàn)我想要的效果,因此我重寫下該類中的某些方法。
在UICollectionViewLayout中有這樣兩句注釋:

1. Methods in this class are meant to be overridden and will be called by its collection view to gather layout information.
在這個類中的方法意味著被重寫(overridden),并且將要被它的 collection view 調(diào)用,用于收集布局信息

2. To get the truth on the current state of the collection view, call methods on UICollectionView rather than these.
要獲取 collection view 的當前狀態(tài)的真相,調(diào)用UICollectionView上的方法,而不是這些
其中有一點需要解釋下,collectionView的bounds的x和y實際上就是collectionView的內(nèi)容視圖的 x和y。關(guān)于這點,請看我寫的一篇博文的解釋:iOS bounds學習筆記以及仿寫UIScrollView的部分功能

// MyFlowLayout.h
#import <UIKit/UIKit.h>

// 注意!繼承自UICollectionViewFlowLayout,因為它繼承自UICollectionViewLayout。
@interface TWLayout : UICollectionViewFlowLayout

// MyFlowLayout.m
/**
 * The default implementation of this method returns NO. Subclasses can override it and return an appropriate value based on whether changes in the bounds of the collection view require changes to the layout of cells and supplementary views.
 此方法的默認實現(xiàn)返回NO。 子類可以覆蓋它,并根據(jù) collection view 的 bounds 中的更改是否需要更改 cells 和 supplementary views(補充視圖) 的布局返回適當?shù)闹怠?

 * If the bounds of the collection view change and this method returns YES, the collection view invalidates the layout by calling the invalidateLayoutWithContext: method.
 如果 collection view 的 bounds 更改并且此方法返回YES,則 collection view 通過調(diào)用invalidateLayoutWithContext:方法使布局更新。

 @param newBounds The new bounds of the collection view.
 @return YES if the collection view requires a layout update or NO if the layout does not need to change.
 */
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
 return YES;
}


/**
 * Returns the layout attributes for all of the cells and views in the specified rectangle.
 返回指定矩形中所有cells和views的布局屬性。

 @param rect * The rectangle (specified in the collection view's coordinate system) containing the target views.
  包含目標視圖的矩形(在集合視圖的坐標系中指定)。

 @return * An array of UICollectionViewLayoutAttributes objects representing the layout information for the cells and views. The default implementation returns nil.
  UICollectionViewLayoutAttributes對象數(shù)組,表示cell和view的布局信息。默認實現(xiàn)返回nil。
 */
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
 // 獲取collectionView的寬帶
 CGFloat collectionW = self.collectionView.bounds.size.width;

 // 獲取布局屬性數(shù)組
 NSArray<UICollectionViewLayoutAttributes *> *attrs = [super layoutAttributesForElementsInRect:self.collectionView.bounds];
 for (int i = 0; i < attrs.count; i++) {
 UICollectionViewLayoutAttributes *attr = attrs[i];

 //每個顯示的cell距離中心距離
 CGFloat margin = fabs((attr.center.x - self.collectionView.contentOffset.x) - collectionW * 0.5);

 // 縮放比例:(margin / (collectionW * 0.5))得出的結(jié)論相當于 0 ~ 1。而我們需要它的縮放比例是 1 ~ 0.65,這樣就是 (1 - 0)~(1 - 0.35)
 CGFloat scale = 1 - (margin / (collectionW * 0.5)) * 0.35;

 attr.transform = CGAffineTransformMakeScale(scale, scale);
 }

 return attrs;
}


/**
 * Returns the point at which to stop scrolling.
 * 關(guān)于這個方法,最終的偏移量,并不是由手指滑動過的偏移量決定的。如果手指滑動比較快,手指滑動過后,視圖還會多滾動一段距離;如果手指滑動緩慢,手指滑到何處,就停到何處。

 @param proposedContentOffset 建議的點(在集合視圖的內(nèi)容視圖的坐標空間中)用于可見內(nèi)容的左上角。 這表示集合視圖計算為在動畫結(jié)束時最可能使用的值。
 @param velocity 沿著水平軸和垂直軸的當前滾動速度。 該值以每秒點數(shù)為單位。
 @return 要使用的內(nèi)容偏移量。 此方法的默認實現(xiàn)返回proposedContentOffset參數(shù)中的值。
 */
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
 // 獲取collectionView的寬度
 CGFloat collectionW = self.collectionView.bounds.size.width;
 // 獲取當前的內(nèi)容偏移量
 CGPoint targetP = proposedContentOffset;

 // 獲取顯示cell的布局屬性數(shù)組,橫向滾動,所以只用考慮橫向的x和width,縱向不用考慮
 NSArray *attrs = [super layoutAttributesForElementsInRect:CGRectMake(targetP.x, 0, collectionW, MAXFLOAT)];

 // 距離中心點最近的cell的間距(中間那個cell距離最近,值可正可負)
 CGFloat minSpacing = MAXFLOAT;
 for (UICollectionViewLayoutAttributes *attr in attrs) {
 // 距離中心點的偏移量
 CGFloat centerOffsetX = attr.center.x - targetP.x - collectionW * 0.5;
 // fabs():CGFloat絕對值
 if (fabs(centerOffsetX) < fabs(minSpacing)) {
  minSpacing = centerOffsetX;
 }
 }
 targetP.x += minSpacing;

 return targetP;
}

最后,記得把 UICollectionViewFlowLayout 改成你自定義的FlowLayout對象??!

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS實現(xiàn)相冊多選圖片上傳功能

    iOS實現(xiàn)相冊多選圖片上傳功能

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)相冊多選圖片上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • swift 常用高階函數(shù)分享

    swift 常用高階函數(shù)分享

    Swift是一門面向協(xié)議的語言,在使用Swift時我們已經(jīng)充分享受到了面向協(xié)議編程帶給我們的便利,但是Swift相比Obj-C還有一個更重要的優(yōu)點,那就是對函數(shù)式編程提供了很好的支持,其中Swift提供了map,filter,reduce這三個高階函數(shù)Higher Order function作為對容器的支持
    2017-12-12
  • 淺談iOS開發(fā)如何適配暗黑模式(Dark Mode)

    淺談iOS開發(fā)如何適配暗黑模式(Dark Mode)

    這篇文章主要介紹了淺談iOS開發(fā)如何適配暗黑模式(Dark Mode),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • WKWebView、WebView和JS的交互方式詳解

    WKWebView、WebView和JS的交互方式詳解

    這篇文章主要給大家介紹了關(guān)于WKWebView、WebView和JS的交互方式,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2018-04-04
  • iOS中自定義彈出pickerView效果(DEMO)

    iOS中自定義彈出pickerView效果(DEMO)

    這篇文章主要介紹了iOS中自定義簡單彈出pickerView(DEMO)的實例代碼,需要的朋友可以參考下
    2017-01-01
  • 使用Xcode為iOS應(yīng)用項目創(chuàng)建PCH文件的方法及應(yīng)用示例

    使用Xcode為iOS應(yīng)用項目創(chuàng)建PCH文件的方法及應(yīng)用示例

    這篇文章主要介紹了使用Xcode為iOS應(yīng)用項目創(chuàng)建PCH文件的方法及應(yīng)用示例,PCH文件可以保留應(yīng)用的很多的基礎(chǔ)設(shè)置信息以供復(fù)用,需要的朋友可以參考下
    2016-03-03
  • Objective-C 自定義漸變色Slider的實現(xiàn)方法

    Objective-C 自定義漸變色Slider的實現(xiàn)方法

    系統(tǒng)提供UISlider,但在開發(fā)過程中經(jīng)常需要自定義,本次需求內(nèi)容是實現(xiàn)一個擁有漸變色的滑動條,且漸變色隨著手指touch的位置不同改變區(qū)域,這篇文章主要介紹了Objective-C 自定義漸變色Slider,需要的朋友可以參考下
    2024-07-07
  • IOS上iframe的滾動條失效的解決辦法

    IOS上iframe的滾動條失效的解決辦法

    這篇文章主要為大家詳細介紹了IOS上iframe的滾動條失效的解決辦法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Objective-C 實現(xiàn)2048算法類

    Objective-C 實現(xiàn)2048算法類

    本文給大家介紹的是使用Objective-C 實現(xiàn)的IOS版小游戲2048算法類,十分的實用,有需要的小伙伴可以參考下。
    2015-06-06
  • iOS獲取cell中webview的內(nèi)容尺寸

    iOS獲取cell中webview的內(nèi)容尺寸

    這篇文章主要介紹了iOS獲取cell中webview內(nèi)容尺寸,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評論

乐平市| 武山县| 库车县| 雅江县| 乡宁县| 承德县| 嘉荫县| 马龙县| 荃湾区| 昭平县| 玉山县| 万载县| 土默特右旗| 岗巴县| 通海县| 濮阳市| 大悟县| 体育| 临夏市| 神农架林区| 凤冈县| 安吉县| 靖边县| 郴州市| 太原市| 固始县| 乌恰县| 杭锦后旗| 平武县| 兰坪| 鄄城县| 阜平县| 奉化市| 石阡县| 汉阴县| 沧源| 宁晋县| 南宫市| 辽源市| 磐石市| 泉州市|