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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

iOS?Objective-c實(shí)現(xiàn)左右滑動(dòng)切換頁(yè)面_IOS

作者:zyw658000 ? 更新時(shí)間: 2022-10-03 編程語(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

欄目分類
最近更新