實現(xiàn)滾動與點擊按鈕的聯(lián)動

這個實現(xiàn)用到第三方框架 HMSegmentControl 具體實現(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)注",@"收藏",@"我的",@"首頁",@"聯(lián)動",@"購物"]];

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)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌二跋,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異管怠,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)缸榄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來祝拯,“玉大人甚带,你說我怎么就攤上這事她肯。” “怎么了鹰贵?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵赵讯,是天一觀的道長饲鄙。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么说莫? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮舔糖,結(jié)果婚禮上愈犹,老公的妹妹穿的比我還像新娘。我一直安慰自己阻荒,他們只是感情好挠锥,可當(dāng)我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著侨赡,像睡著了一般蓖租。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上羊壹,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天蓖宦,我揣著相機(jī)與錄音,去河邊找鬼油猫。 笑死稠茂,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的眨攘。 我是一名探鬼主播主慰,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鲫售!你這毒婦竟也來了共螺?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤情竹,失蹤者是張志新(化名)和其女友劉穎藐不,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體秦效,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡雏蛮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了阱州。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片挑秉。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖苔货,靈堂內(nèi)的尸體忽然破棺而出犀概,到底是詐尸還是另有隱情立哑,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布姻灶,位于F島的核電站铛绰,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏产喉。R本人自食惡果不足惜捂掰,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望曾沈。 院中可真熱鬧这嚣,春花似錦、人聲如沸晦譬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽敛腌。三九已至卧土,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間像樊,已是汗流浹背尤莺。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留生棍,地道東北人颤霎。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像涂滴,于是被迫代替她去往敵國和親友酱。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容