實(shí)現(xiàn)滾動(dòng)與點(diǎn)擊按鈕的聯(lián)動(dòng)

這個(gè)實(shí)現(xiàn)用到第三方框架 HMSegmentControl 具體實(shí)現(xiàn)看代碼?

#import "ViewController.h"#import "HMSegmentedControl.h"#define Width self.view.bounds.size.width@interface ViewController (){

UIScrollView *_scrollView;

HMSegmentedControl *_seg;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self createSegmentControl];

[self createScrollView];

}

- (void)createScrollView {

_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, Width, self.view.bounds.size.height - 100)];

_scrollView.contentSize = CGSizeMake(7 * Width, 0);

_scrollView.pagingEnabled = YES;

[self.view addSubview:_scrollView];

_scrollView.delegate = self;

UIView *lastView = nil;

for (int i = 0; i < 7; i++) {

UIView *view = [[UIView alloc] init];

view.backgroundColor = [self randomColor];

view.frame = CGRectMake(lastView?(lastView.frame.origin.x + lastView.bounds.size.width) : 0,0, Width, _scrollView.bounds.size.height);

[_scrollView addSubview:view];

lastView = view;

}

}

- (void)createSegmentControl {

HMSegmentedControl *seg = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"消息",@"關(guān)注",@"收藏",@"我的",@"首頁(yè)",@"聯(lián)動(dòng)",@"購(gòu)物"]];

seg.frame = CGRectMake(0, 64, self.view.bounds.size.width, 36);

[self.view addSubview:seg];

//? ? seg.backgroundColor = [UIColor grayColor];

seg.selectedSegmentIndex = 0;

// 正常的屬性

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

dict[NSForegroundColorAttributeName] = [UIColor greenColor];

dict[NSFontAttributeName] = [UIFont systemFontOfSize:14];

seg.titleTextAttributes = dict;

// 選中的屬性

NSMutableDictionary *selectedDict = [NSMutableDictionary dictionary];

selectedDict[NSFontAttributeName] = [UIFont systemFontOfSize:16];

selectedDict[NSForegroundColorAttributeName] = [UIColor redColor];

seg.selectedTitleTextAttributes = selectedDict;

// 指示條

seg.selectionIndicatorColor = [UIColor purpleColor];

seg.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;

seg.selectionIndicatorHeight = 2.0f;

// 垂直線

seg.verticalDividerEnabled = YES;

seg.verticalDividerColor = [UIColor blackColor];

seg.verticalDividerWidth = 2.0f;

// 選擇類型

seg.selectionStyle = HMSegmentedControlSelectionStyleBox;

// 設(shè)置segment的偏移量

seg.segmentEdgeInset = UIEdgeInsetsMake(0, 20, 0, 20);

[seg addTarget:self action:@selector(selectedChangeValue:) forControlEvents:UIControlEventValueChanged];

_seg = seg;

}

- (void)selectedChangeValue:(HMSegmentedControl *)seg {

NSLog(@"select index:%ld",seg.selectedSegmentIndex);

// 設(shè)置偏移量

[_scrollView setContentOffset:CGPointMake(Width * seg.selectedSegmentIndex, 0) animated:YES];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

NSInteger selectedIndex = scrollView.contentOffset.x / Width;

// 設(shè)置選中的索引

[_seg setSelectedSegmentIndex:selectedIndex animated:YES];

}

- (UIColor *)randomColor {

CGFloat redValue = arc4random_uniform(255);

CGFloat greenValue = arc4random_uniform(255);

CGFloat blueValue = arc4random_uniform(255);

UIColor *color = [UIColor colorWithRed:redValue/255 green:greenValue/255 blue:blueValue/255 alpha:1];

return color;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容