欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS Objective-c實(shí)現(xiàn)左右滑動切換頁面

 更新時間:2022年08月08日 11:22:21   作者:zyw658000  
這篇文章主要為大家詳細(xì)介紹了iOS Objective-c實(shí)現(xiàn)左右滑動切換頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS Objective-c實(shí)現(xiàn)左右滑動切換頁面的具體代碼,供大家參考,具體內(nèi)容如下

ScrollView + n個view

1.storyboard布局一個ScrollView

2.拖出兩個輸出口,定義三個屬性

@property (weak, nonatomic) IBOutlet UIScrollView *XMScrollView;
@property (weak, nonatomic) IBOutlet UIView *scrollContentView;


///第一次按下
@property (nonatomic) BOOL isBeginScroll;
///開始結(jié)束滑動scroll動畫
@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{
? ? ///開始滑動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];
? ? ///結(jié)束滑動scrollView
? ? self.isBeginScroll = NO;
? ? ///開始滑動動畫
? ? self.isBeginAnimationScroll = YES;
? ??
}

///結(jié)束滑動動畫
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
? ??
? ? if (self.isBeginAnimationScroll) {
? ? ? ??
? ? ? ? CGFloat currentX = scrollView.contentOffset.x;
? ? ? ??
? ? ? ? NSInteger page = currentX/self.view.frame.size.width;
? ? ? ? ///判斷到哪一頁了,加載數(shù)據(jù)
? ? ? ? switch (page) {
? ? ? ? ? ? ? ??
? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ??
??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ??
? ??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? ? ? default:
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? }
? ??
? ? self.isBeginAnimationScroll = NO;

}

5.在viewDidLoad中添加監(jiān)聽

///頁面切換ScrollView
? ? self.XMScrollView.delegate = self;
? ??
? ? [self addObserver:self forKeyPath:@"isBeginScroll" options:NSKeyValueObservingOptionNew context:nil];

6.通過監(jiān)聽實(shí)現(xiàn)滑動結(jié)束后自動滑動

-(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];

? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? }
? ??
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論