iOS 多頁面滑動

2021-07-22 01:22:01 字數 4640 閱讀 7591

最近先來有空,寫了乙個多頁面滑動的框架,先上圖:

思路:1.標題使用uicollectionview實現,有兩種模式,標題居中平均長度,另外一種模式是標題靠左

2.下面的頁面切換使用乙個uipageviewcontroller

標頭檔案

//

// hrscrollpagecontroller.h

// 多頁滑動

//// created by chen heren on 16/8/22.

//#import @inte***ce hrpagecontroller : uiviewcontroller

@property (nonatomic, strong) uicolor *titlebackgroundcolor; //標題欄背景顏色

@property (nonatomic, strong) uicolor *titlecolor; //標題顏色

@property (nonatomic, strong) uicolor *indicatorcolor; //標題選中指示器顏色

@property (nonatomic) bool ismiddle; // 標題居中

-(instancetype)initwithcontrollers:(nsarray *)controllers andtitles:(nsarray *)titles;

-(instancetype)initwithcontrollers:(nsarray *)controllers andtitles:(nsarray *)titles ismiddle:(bool)ismiddle;

@end

私有屬性:

#import "titleviewcell.h"

#import "hrpagecontroller.h"

#define kwidth [uiscreen mainscreen].bounds.size.width

#define kheight [uiscreen mainscreen].bounds.size.height

static nsstring *titlecellidentifier = @"titlecell";

@inte***ce hrpagecontroller ()@property (nonatomic, strong) nsarray *controllers; //子控制器

@property (nonatomic, strong) nsarray *titles; //標題

@property (nonatomic) nsuinteger nextindex; //即將切換到的頁面序號

@property (nonatomic, strong) uicollectionview *titlecollectionview; //標題列表

@property (nonatomic, strong) uipageviewcontroller *pageviewcontroller; //實現多頁面切換

@end

標題列表初始化

-(void)setuptitles
實現標題集合檢視的先關**

#pragma mark - uicolectionviewdatasource delegate

-(nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview

-(nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section

-(uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath

#pragma mark - uicollectionviewlayout

-(cgfloat)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout minimumlinespacingforsectionatindex:(nsinteger)section

-(cgsize)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout sizeforitematindexpath:(nsindexpath *)indexpathelse

}-(uiedgeinsets)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout insetforsectionatindex:(nsinteger)section

#pragma mark - uicollectionview deleaget

-(void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath

實現uipageviewcontroller相關**

#pragma mark - uipageviewconrtroller datssource delegate

-(uiviewcontroller *)viewcontrolleratindex:(nsuinteger)index

return self.controllers[index];

}-(nsuinteger)indexofviewcontroller:(uiviewcontroller *)viewcontroller

-(uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerbeforeviewcontroller:(uiviewcontroller *)viewcontroller

//迴圈滑動

if (index ==0 )

index --;

return [self viewcontrolleratindex:index];

}-(uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerafterviewcontroller:(uiviewcontroller *)viewcontroller

index ++;

//迴圈滑動

if (index == self.controllers.count)

if (index > self.controllers.count)

return [self viewcontrolleratindex:index];

}-(void)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller willtransitiontoviewcontrollers:(nsarray *)pendingviewcontrollers

-(void)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller didfinishanimating:(bool)finished previousviewcontrollers:(nsarray *)previousviewcontrollers transitioncompleted:(bool)completed

}

編寫兩個方法:標題cell選中和取消的狀態

//設定標題選中狀態,一定要在檢視載入之後設定

-(void)selectedtitle:(nsuinteger)index

//標題取消選中

-(void)deselecttitle:(nsuinteger)index

%%%最後實現初始化

-(instancetype)initwithcontrollers:(nsarray *)controllers andtitles:(nsarray *)titles

return self;

}

注:這算是第乙個版本,因為加入childcontrollers較多的話,採取上文的寫法是一下子全部載入,比較耗記憶體

打算第二個版本採用**的方法  叫 :hrpagecontrollerdatasourcedelegate 哈哈!!!

demo傳送門:

ios 橫向滾輪效果 ios橫向選單 頁面滑動

ios橫向選單 頁面滑動 檢視次數 9122 大小 11 b demo 橫向選單 頁面滑動 例子uiviewcontroller vc1 uiviewcontroller alloc init vc1.view setbackgroundcolor uicolor redcolor uiviewco...

ios手機頁面滑動卡頓問題

說起來真是件悲傷的事情,開發了這麼多移動端頁面,今天犯了乙個低階,而我卻不知道的錯誤。因為現在移動裝置真是賊辣多,手機螢幕的高度,寬度各式各樣。所以我們有一些頁面高度不夠長,在iphone8x 三星長屏手機等頁面顯示不全,所以我開發的時候,有時候會給html和body都設定上height 100 所...

Appium scroll滑動頁面

方法介紹 舉個例子 encoding utf 8 from time import sleep desired caps driver webdriver.remote desired caps sleep 15 獲取手機螢幕寬 高 x driver.get window size width y ...