[objc]-ios 分頁控制器實現

來源:互聯網
上載者:User

標籤:selector   targe   enable   black   orm   height   ase   als   sid   

如下 上方的滑條 根據頁面可以滑動

 

思路: 

下方灰色的部分是一個scrollview 其中放置了2個view。

上方的綠色滑條是uilable控制項。

綠色滑條下面是2個button。

 

實現:

在所有的controller外部套了一層NavigationController,所以會有UINavigationBar。

 

//首先viewController.h  建立控制項 並且繼承  <UIScrollViewDelegate>

@interface ViewController : BaseViewController<UIScrollViewDelegate>//scrollview@property (nonatomic,strong)UIScrollView * scrollView;//移動的滑條@property(nonatomic,strong)UILabel * navSlideLable; //選擇按鈕@property(nonatomic,strong)UIButton * view1Btn;@property(nonatomic,strong)UIButton * view2Btn;@end

 

//在viewController.m 檔案中實現控制項的懶載入。

#pragma mark Lazyout-(UIScrollView *)scrollView{    if (!_scrollView) {        //距離頭的的高度        CGFloat scrollViewHeight = 100;        //scrollview的大小位置         _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, scrollViewHeight, SK_ScreenWidth,SK_ScreenHeight- scrollViewHeight - self.navigationController.navigationBar.frame.size.height -22)];        _scrollView.showsHorizontalScrollIndicator = NO;        _scrollView.pagingEnabled = YES;        CGSize size = _scrollView.frame.size;        //scrollview的寬度 = 頁面的個數* 單個頁面的寬度        size.width *=2; //2個        _scrollView.contentSize = CGSizeMake(size.width, 0);        _scrollView.backgroundColor = [UIColor redColor];        _scrollView.delegate = self; //設定代理            }    return _scrollView;}//移動的滑條-(UILabel *)navSlideLable{    if (!_navSlideLable) {                    _navSlideLable = [[UILabel alloc] init];        _navSlideLable.backgroundColor = [UIColor colorWithRed:32/255.0 green:178/255.0 blue:170/255.0 alpha:1];    }    return _navSlideLable;}//Button-(UIButton *)view1Btn{    if (!_view1Btn) {            _view1Btn = [[UIButton alloc]init];        //給Btn一個target scrollview會根據tag 來移動位置        _view1Btn.tag =(NSInteger) (0 / (self.view.frame.size.width / 2));        [_view1Btn setFrame:CGRectMake(0, 0, SK_ScreenWidth/2, TOP_VIEW_ITEM_HEIGHT)];        [_view1Btn setTitle:@"view1" forState:UIControlStateNormal];        [_view1Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];        //給Btn添加相應方法        [_view1Btn addTarget:self action:@selector(switchview1Btn:) forControlEvents:UIControlEventTouchUpInside];    }    return _view1Btn;}-(UIButton *)view2Btn{        if (!_view2Btn) {            _view2Btn = [[UIButton alloc]init];        _view2Btn.tag =(NSInteger) (SK_ScreenWidth/2 / (self.view.frame.size.width / 2));        [_view2Btn setFrame:CGRectMake(SK_ScreenWidth/2, 0, SK_ScreenWidth/2, TOP_VIEW_ITEM_HEIGHT)];        [_view2Btn setTitle:view2 forState:UIControlStateNormal];        [_view2Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];        [_view2Btn addTarget:self action:@selector(switchview2Btn:) forControlEvents:UIControlEventTouchUpInside];      }    return _view2Btn;}

 

//viewDidLoad 添加控制項到頁面上

scrollview中的視圖我另外建立了2個UIView的類 FirstView 和SecondView 方便再自訂視圖

滑條和按鈕的位置是可以自己修改的 我寫的是在上方 也可以自己寫到下方或者其他的地方改變一下位置就好啦

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.        self.view.backgroundColor  = [UIColor whiteColor];    //添加移動滑條    [self.view addSubview: self.navSlideLable];    //移動滑條的初始位置 (應為只有2個按鈕所以寬度直接是螢幕的一半 多個自己調節就好)    [self.navSlideLable setFrame:CGRectMake(0,0, SK_ScreenWidth/2, TOP_VIEW_LINE_HEIGHT)];    //添加按鈕     [self.view addSubview:self.view1Btn];    [self.view addSubview:self.view2Btn];    //添加scrollview 中的視圖    //第一個頁面的位置是 0 0    FirstView * firstView = [[FirstView alloc]initWithFrame:CGRectMake(0, 0, SK_ScreenWidth, self.scrollView.frame.size.height)];    //第二個頁面的起始位置就是加上第一個頁面的寬度 依次類推    SecondView * secondView = [[SecondView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width, 0, SK_ScreenWidth, self.scrollView.frame.size.height)];    //添加scrollview 中的視圖    [self.scrollView addSubview:firstView];    [self.scrollView addSubview:firstView];    //添加scrollview        [self.view addSubview: self.scrollView];    }

 

 //實現Button的回應程式法

//這裡點擊後不需要改動滑條的位置,按鈕一點擊就會改動scrollview的位置,會自己響應scrollview的代理方法去改變滑條的位置

-(void)switchview1Btn:(UIButton *)button{    //點擊Button 根據button的tag 來改變scrollview 滑動的位置    [self.scrollView setContentOffset:CGPointMake(button.tag * self.view.frame.size.width, 0) animated:YES];}-(void)switchview2Btn:(UIButton *)button{    //點擊Button 根據button的tag 來改變scrollview 滑動的位置    [self.scrollView setContentOffset:CGPointMake(button.tag * self.view.frame.size.width, 0) animated:YES];    }

 

//實現scrollview的代理方法行動裝置檢視的位置

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {    CGPoint offset = scrollView.contentOffset;    //應為只有2個按鈕和頁面所以 處以2 3個就處以3    CGFloat x = offset.x / 2;    if (x !=0 &&x < scrollView.frame.size.width) {       //移動滑條的位置        CGRect frame = self.navSlideLable.frame;        frame.origin.x = x;        self.navSlideLable.frame = frame;    }}

 

 

以上????

 

 

 

 

 

 

 

 

 

 

[objc]-ios 分頁控制器實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.