標籤: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 分頁控制器實現