日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

iOS?Objective-c實現左右滑動切換頁面_IOS

作者:zyw658000 ? 更新時間: 2022-10-03 編程語言

本文實例為大家分享了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

欄目分類
最近更新