iOS Objective-c實(shí)現(xiàn)左右滑動(dòng)切換頁面
本文實(shí)例為大家分享了iOS Objective-c實(shí)現(xiàn)左右滑動(dòng)切換頁面的具體代碼,供大家參考,具體內(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; ? ? ? ? ///判斷到哪一頁了,加載數(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)滑動(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]; ? ? ? ? ? ? } ? ? ? ? ? ?? ? ? ? ? } ? ? ? ?? ? ? } ? ?? }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談iOS 數(shù)據(jù)結(jié)構(gòu)之鏈表
這篇文章主要介紹了淺談iOS 數(shù)據(jù)結(jié)構(gòu)之鏈表,本文詳細(xì)的介紹了單鏈表和雙鏈表,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09IOS開發(fā)之tableView點(diǎn)擊行跳轉(zhuǎn)并帶有“顯示”更多功能
這篇文章給大家介紹通過點(diǎn)擊城市中的tableView跳轉(zhuǎn)到旅游景點(diǎn)的tableView,下面會(huì)有“顯示”更多的功能,代碼簡(jiǎn)單易懂,對(duì)ios點(diǎn)擊tableview跳轉(zhuǎn)相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03iOS開發(fā)中Quartz2D繪圖路徑的使用以及條紋效果的實(shí)現(xiàn)
這篇文章主要介紹了iOS開發(fā)中Quartz2D繪圖路徑的使用以及條紋效果的實(shí)現(xiàn),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11IOS如何在Host App 與 App Extension 之間發(fā)送通知
這篇文章主要介紹了IOS如何在Host App 與 App Extension 之間發(fā)送通知 的相關(guān)資料,需要的朋友可以參考下2016-03-03