網站首頁 編程語言 正文
本文實例為大家分享了iOS Objective-c實現左右滑動切換頁面的具體代碼,供大家參考,具體內容如下
ScrollView + n個view
1.storyboard布局一個ScrollView
2.拖出兩個輸出口,定義三個屬性
@property (weak, nonatomic) IBOutlet UIScrollView *XMScrollView;
@property (weak, nonatomic) IBOutlet UIView *scrollContentView;
///第一次按下
@property (nonatomic) BOOL isBeginScroll;
///開始結束滑動scroll動畫
@property (nonatomic) BOOL isBeginAnimationScroll;
///開始坐標
@property (nonatomic) NSInteger beginX;
3.在viewDidAppear中重新設置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{
? ? ///開始滑動scrollView
? ? self.isBeginScroll = YES;
? ? ///開始滑動scrollView的位置
? ? self.beginX = scrollView.contentOffset.x;;
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
? ? ///停下自動滑動scrollView
? ? [self.XMScrollView setContentOffset:CGPointZero animated:YES];
? ? ///結束滑動scrollView
? ? self.isBeginScroll = NO;
? ? ///開始滑動動畫
? ? self.isBeginAnimationScroll = YES;
? ??
}
///結束滑動動畫
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
? ??
? ? if (self.isBeginAnimationScroll) {
? ? ? ??
? ? ? ? CGFloat currentX = scrollView.contentOffset.x;
? ? ? ??
? ? ? ? NSInteger page = currentX/self.view.frame.size.width;
? ? ? ? ///判斷到哪一頁了,加載數據
? ? ? ? switch (page) {
? ? ? ? ? ? ? ??
? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ??
??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ??
? ??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? ? ? default:
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? }
? ??
? ? self.isBeginAnimationScroll = NO;
}
5.在viewDidLoad中添加監聽
///頁面切換ScrollView
? ? self.XMScrollView.delegate = self;
? ??
? ? [self addObserver:self forKeyPath:@"isBeginScroll" options:NSKeyValueObservingOptionNew context:nil];
6.通過監聽實現滑動結束后自動滑動
-(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
相關推薦
- 2022-07-20 使用numpy.ndarray添加元素_python
- 2022-11-21 go語言算法題解二叉樹的最小深度_Golang
- 2024-03-08 SpringBoot開發中VO、DTO的作用,以及使用場景
- 2022-03-30 C++歸并排序算法詳解_C 語言
- 2022-05-24 python使用torch隨機初始化參數_python
- 2022-09-01 ASP.NET?Core通用主機的系統配置_實用技巧
- 2022-03-23 C語言實現簡單的三子棋游戲源碼_C 語言
- 2022-08-29 .Net?Core靜態文件資源的使用_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支