First, last.
I wrote an infinite slider class to inherit UIView
Use the Init method directly to define the color of each heading and the color of the selected color and the font of the frame title
Directly on the code:
Ej_sliderheader.h
#import <UIKit/UIKit.h>
@interface Ej_sliderheader:uiview
-(Instancetype) Initwitharray: (nsarray*) Data Withslidercolor: (uicolor*) Slidercolor Withtitlecolor: (UIColor*) Titlecolor Withtitlefont: (uifont*) font withblock: (void (^) (Nsinteger)) Block;
/**
Infinitely stretched slider title
@param frame Entire space frame
@param Data string array
@param slidercolor underline and check the title color
@param titlecolor title Color
@param font Title
@param Block callback method
@return Instances
*/
-(Instancetype) initWithFrame: (CGRect) frame Witharray: (nsarray*) Data Withslidercolor: (uicolor*) Slidercolor Withtitlecolor: (uicolor*) Titlecolor Withtitlefont: (uifont*) font withblock: (void (^) (Nsinteger)) Block;
@property (nonatomic,copy) void (^clicktosendback) (Nsinteger index);
/**
Jump to a specified location
@param index subscript represents the title to be selected
*/
-(void) Changeindex: (Nsinteger) index;
-(void) Indexpathrowadd;
-(void) indexpathrowcut;
@end
. m file
Ej_sliderheader.m
//
Ej_sliderheader.m
Ejlshop
//
Created by Yunfutai on 2018/2/7.
Copyright? 2018 Jasongor. All rights reserved.
//
#import "Ej_sliderheader.h"
@interface Ej_sliderheader ()
@property (Nonatomic,strong) uicolor * BTNTITLECOLOR;
@property (Nonatomic,strong) uicolor * SLIDERCOLOR;
@property (Nonatomic,strong) uifont * Btnfont;
@end
@implementation ej_sliderheader{
Uiscrollview * _SCROLLERBACKGROUND;
Nsarray * DataArray;
UIView * Sliderline;
int selecedindex;
}
#define BUTTONWIDTH 83
#define PAGESCROLLERCOUNT 4
-(Instancetype) Initwitharray: (Nsarray *) Data Withslidercolor: (Uicolor *) Slidercolor Withtitlecolor: (UIColor *) Titlecolor Withtitlefont: (Uifont *) font withblock: (void (^) (Nsinteger)) block{
if (self = [super Initwithframe:cgrectmake (0, Adaiphonex (), EJSCREENW, 45)]) {
DataArray = Data;
_btntitlecolor = Titlecolor;
_slidercolor = Slidercolor;
_btnfont = font;
Self.clicktosendback = Block;
[Self makeupsubview];
}
return self;
}
-(Instancetype) initWithFrame: (CGRect) frame Witharray: (Nsarray *) Data Withslidercolor: (Uicolor *) Slidercolor Withtitlecolor: (Uicolor *) Titlecolor Withtitlefont: (Uifont *) font withblock: (void (^) (Nsinteger)) block{
if (self = [Super Initwithframe:frame]) {
DataArray = Data;
_btntitlecolor = Titlecolor;
_slidercolor = Slidercolor;
_btnfont = font;
Self.clicktosendback = Block;
_scrollerbackground = [[Uiscrollview alloc]initwithframe:cgrectmake (0, 0, Frame.size.width, frame.size.height)];
_scrollerbackground.pagingenabled = NO;
_scrollerbackground.showshorizontalscrollindicator = NO;
[Self addsubview:_scrollerbackground];
[Self makeupsubview];
Self.layer.shadowColor = [Uicolor blackcolor]. Cgcolor;
self.layer.shadowOpacity = 0.2f;
Self.layer.shadowOffset = Cgsizemake (1, 4);
}
return self;
}
-(void) makeupsubview{
CGFloat btnwidth;
if (Dataarray.count>pagescrollercount) {
Btnwidth =buttonwidth;
_scrollerbackground.contentsize = Cgsizemake (btnwidth* (Dataarray.count), self.height);
_scrollerbackground.backgroundcolor = [Uicolor Whitecolor];
}else{
Btnwidth = Self.width/dataarray.count;
}
Selecedindex = 0;
for (int i=0; i<dataarray.count; i++) {
UIButton * btn =[uibutton new];
[_scrollerbackground ADDSUBVIEW:BTN];
Btn.sd_layout
. Topequaltoview (_scrollerbackground)
. Leftspacetoview (_scrollerbackground, I*btnwidth)
. Widthis (Btnwidth)
. Heightis (_scrollerbackground.height);
Btn.tag=i;
[BTN Settitle:dataarray[i] forstate:uicontrolstatenormal];
[Btn.titlelabel Setfont:_btnfont];
if (i==0) {
[Btn Settitlecolor:_slidercolor Forstate:uicontrolstatenormal];
}else{
[Btn Settitlecolor:_btntitlecolor Forstate:uicontrolstatenormal];
}
[Btn addtarget:self Action: @selector (Btnmethod:) forcontrolevents:uicontroleventtouchupinside];
[Btn.titlelabel Setfont:ejfont_diy6 (12)];
Btn.backgroundcolor = [Uicolor Whitecolor];
}
for (int i =1; i<dataarray.count; i++) {
UIView * line = [[UIView alloc]initwithframe:cgrectmake (btnwidth*i, 0, 0.5, self.height)];
Line.backgroundcolor = RGBA (241, 241, 241, 1);
[_scrollerbackground Addsubview:line];
}
UIView * Bomline = [[UIView alloc]initwithframe:cgrectmake (0, self.height-0.5, EJSCREENW, 0.5)];
Bomline.backgroundcolor = RGBA (241, 241, 241, 1);
[_scrollerbackground Addsubview:bomline];
Sliderline = [[UIView alloc]initwithframe:cgrectmake (0, Self.height-1, Btnwidth, 2)];
Sliderline.backgroundcolor = _slidercolor;
[_scrollerbackground Addsubview:sliderline];
}
#pragma mark-button click event
-(void) Btnmethod: (uibutton*) sender{
For (UIView * Sub in _scrollerbackground.subviews) {
if ([Sub Iskindofclass:[uibutton class]]) {
UIButton * subbtn = (uibutton*) sub;
[Subbtn Settitlecolor:_btntitlecolor Forstate:uicontrolstatenormal];
}
}
[Sender Settitlecolor:_slidercolor Forstate:uicontrolstatenormal];
CGFloat btnwidth;
if (Dataarray.count>pagescrollercount) {
Btnwidth = Buttonwidth;
}else{
Btnwidth = Self.width/dataarray.count;
}
Selecedindex = (int) sender;
[UIView animatewithduration:0.3f animations:^{
sliderline.x = Sender.tag *btnwidth;
} completion:^ (BOOL finished) {
Self.clicktosendback (Sender.tag);
}];
}
#pragma mark-Change line according to subscript
-(void) Changeindex: (Nsinteger) index{
For (UIView * Sub in self.subviews) {
if ([Sub Iskindofclass:[uibutton class]]) {
UIButton * subbtn = (uibutton*) sub;
[Subbtn Settitlecolor:_btntitlecolor Forstate:uicontrolstatenormal];
if (Subbtn.tag = = index) {
[Subbtn Settitlecolor:_slidercolor Forstate:uicontrolstatenormal];
}
}
}
Selecedindex = (int) index;
CGFloat btnwidth = Self.width/dataarray.count;
[UIView animatewithduration:0.3f animations:^{
sliderline.x = Index *btnwidth;
} completion:^ (BOOL finished) {
Self.clicktosendback (index);
}];
}
-(void) Indexpathrowadd {
Selecedindex + +;
if (Selecedindex = = Dataarray.count) {
selecedindex=0;
}
For (UIView * Sub in self.subviews) {
if ([Sub Iskindofclass:[uibutton class]]) {
UIButton * subbtn = (uibutton*) sub;
[Subbtn Settitlecolor:_btntitlecolor Forstate:uicontrolstatenormal];
if (Subbtn.tag = = Selecedindex) {
[Subbtn Settitlecolor:_slidercolor Forstate:uicontrolstatenormal];
}
}
}
CGFloat btnwidth = Self.width/dataarray.count;
[UIView animatewithduration:0.3f animations:^{
sliderline.x = Selecedindex *btnwidth;
} completion:^ (BOOL finished) {
Self.clicktosendback (Selecedindex);
}];
}
-(void) Indexpathrowcut {
Selecedindex--;
if (Selecedindex < 0) {
selecedindex= (int) (DATAARRAY.COUNT-1);
}
For (UIView * Sub in self.subviews) {
if ([Sub Iskindofclass:[uibutton class]]) {
UIButton * subbtn = (uibutton*) sub;
[Subbtn Settitlecolor:_btntitlecolor Forstate:uicontrolstatenormal];
if (Subbtn.tag = = Selecedindex) {
[Subbtn Settitlecolor:_slidercolor Forstate:uicontrolstatenormal];
}
}
}
CGFloat btnwidth = Self.width/dataarray.count;
[UIView animatewithduration:0.3f animations:^{
sliderline.x = Selecedindex *btnwidth;
} completion:^ (BOOL finished) {
Self.clicktosendback (Selecedindex);
}];
}
@end
IOS infinitely added title slider