網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(shí)例為大家分享了iOS Objective-c實(shí)現(xiàn)左右滑動(dòng)切換頁(yè)面的具體代碼,供大家參考,具體內(nèi)容如下
ScrollView + n個(gè)view
1.storyboard布局一個(gè)ScrollView
2.拖出兩個(gè)輸出口,定義三個(gè)屬性
@property (weak, nonatomic) IBOutlet UIScrollView *XMScrollView;
@property (weak, nonatomic) IBOutlet UIView *scrollContentView;
///第一次按下
@property (nonatomic) BOOL isBeginScroll;
///開始結(jié)束滑動(dòng)scroll動(dòng)畫
@property (nonatomic) BOOL isBeginAnimationScroll;
///開始坐標(biāo)
@property (nonatomic) NSInteger beginX;
3.在viewDidAppear中重新設(shè)置scrollContentView的布局寬和tableVIew大小和位置
///遍歷布局
? ? for (NSLayoutConstraint *constraint in self.scrollContentView.constraints) {
? ? ? ?///判斷布局是不是自己想要的 NSLayoutAttribute類型
? ? ? ? if (constraint.firstAttribute == NSLayoutAttributeWidth) {
? ? ? ? ? ??
? ? ? ? ? ? [constraint setConstant:self.view.frame.size.width*3];
? ? ? ? ? ??
? ? ? ? }
? ? ?
? ? }
?? ?[self.tableView1 setFrame:CGRectMake(0, 0, self.view.frame.size.width, scrollViewContentViewFrame.size.height)];
?? ? ? ? ? ?
?? ?[self.tableView2 setFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, scrollViewContentViewFrame.size.height)];
?? ? ? ? ? ?
?? ?[self.tableView3 setFrame:CGRectMake(self.view.frame.size.width*2, 0, self.view.frame.size.width, scrollViewContentViewFrame.size.height)];
4.添加scrollView的代理方法
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
? ? ///開始滑動(dòng)scrollView
? ? self.isBeginScroll = YES;
? ? ///開始滑動(dòng)scrollView的位置
? ? self.beginX = scrollView.contentOffset.x;;
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
? ? ///停下自動(dòng)滑動(dòng)scrollView
? ? [self.XMScrollView setContentOffset:CGPointZero animated:YES];
? ? ///結(jié)束滑動(dòng)scrollView
? ? self.isBeginScroll = NO;
? ? ///開始滑動(dòng)動(dòng)畫
? ? self.isBeginAnimationScroll = YES;
? ??
}
///結(jié)束滑動(dòng)動(dòng)畫
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
? ??
? ? if (self.isBeginAnimationScroll) {
? ? ? ??
? ? ? ? CGFloat currentX = scrollView.contentOffset.x;
? ? ? ??
? ? ? ? NSInteger page = currentX/self.view.frame.size.width;
? ? ? ? ///判斷到哪一頁(yè)了,加載數(shù)據(jù)
? ? ? ? switch (page) {
? ? ? ? ? ? ? ??
? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ??
??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ??
? ??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? ? ? default:
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? }
? ??
? ? self.isBeginAnimationScroll = NO;
}
5.在viewDidLoad中添加監(jiān)聽
///頁(yè)面切換ScrollView
? ? self.XMScrollView.delegate = self;
? ??
? ? [self addObserver:self forKeyPath:@"isBeginScroll" options:NSKeyValueObservingOptionNew context:nil];
6.通過(guò)監(jiān)聽實(shí)現(xiàn)滑動(dòng)結(jié)束后自動(dòng)滑動(dòng)
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
? ? if (!self.isBeginScroll) {
? ? ? ??
? ? ? ? CGFloat offSetX = self.XMScrollView.contentOffset.x;
? ? ? ??
? ? ? ? NSInteger scale = (int)(offSetX/self.view.frame.size.width);
? ?
? ? ? ? if (offSetX >= self.beginX) {
??
? ? ? ? ? ? ? ? [self.XMScrollView setContentOffset:CGPointMake((scale+1)*self.view.frame.size.width, 0) animated:YES];
? ??
? ? ? ? }
? ? ? ??
? ? ? ? if (offSetX < self.beginX) {
? ? ? ? ? ??
? ? ? ? ? ? if (self.beginX >= self.view.frame.size.width){
? ? ? ?
? ? ? ? ? ? ? ? [self.XMScrollView setContentOffset:CGPointMake((scale)*self.view.frame.size.width, 0) animated:YES];
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? }
? ??
}
原文鏈接:https://blog.csdn.net/qq_41586150/article/details/104351414
相關(guān)推薦
- 2022-09-17 Makefile構(gòu)建Golang項(xiàng)目示例詳解_Golang
- 2023-07-15 react全局scss變量
- 2022-03-16 C++中的Lambda函數(shù)詳解_C 語(yǔ)言
- 2023-04-12 如何徹底解決python?NameError:name?'__file__'?is?not?defi
- 2022-03-26 Qt?多語(yǔ)言程序設(shè)計(jì)的實(shí)現(xiàn)_C 語(yǔ)言
- 2022-06-24 python文件讀取和導(dǎo)包的絕對(duì)路徑、相對(duì)路徑詳解_python
- 2022-06-20 React函數(shù)組件與類組件使用及優(yōu)劣對(duì)比_React
- 2022-08-31 Android無(wú)障礙監(jiān)聽通知的實(shí)戰(zhàn)過(guò)程_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支