Skip to content
9miao edited this page Oct 11, 2014 · 2 revisions

CASegmentedControl

Class Description

Segmented button, segmentedControl is composed of one or multi segment, different info is displayed by clicking different segment and it’s only allowed to select one segment at a time.

Effect Picture


Base Class

CAControl

Attribute

Access modifier

Attribute name

Description

public

Title

title

public

BackGroundImage

background image

public

BackgroundView

background view

public

Image

foreground image

public

ImageColor

image color

public

TitleColor

title color

public

TitleFontName

title font

protected

selectedIndex

segment’s index value

Method

Access modifier

Method name

Description

public

addTarget

add click event

public

removeTarget

remove click event

public

insertSegmentWithTitle

insert new segment

public

insertSegmentWithBackgroundImage

insert new segment

public

insertSegmentWithImage

insert new segment

public

removeSegmentAtIndex

remove a certain segment from segment

public

removeAllSegments

remove all segments

public

getItemCount

obtain segment’s count

public

addTargetAtForbidSelected

add click event for disable-selected item

public

removeTargetAtForbidSelected

remove click event

public

setForbidSelectedAtIndex

set forbid-to-be-selected item

public

setAllowSelectedAtIndex

allow a certain item to be selected

Attribute Description

Title
Type: const char*
Descripiton: segment title

BackGroundImage
Type: CAImage*
Descripiton: segment’s background image

BackgroundView
Type: CAView*
Descripiton: background view

Image
Type: CAImage*
Descripiton: segment’s foreground image

ImageColor
Type: CAColor4B*
Descripiton: foreground image color

TitleColor
Type: CAColor4B
Descripiton: segment’s title color

TitleFontName
Type: string
Descripiton: title font

selectedIndex
Type: int
Descripiton: read-only property, index value corresponding to segment

Method Description

void addTarget(CAObject target, SEL_CAControl selector)*
Return value: void
Parameter:

Type

Parameter name

Description

CAObject*

target

current object

SEL_CAControl

selector

function callback

Descripiton: segment’s click event

void removeTarget(CAObject target, SEL_CAControl selector)*
Return value: void
Descripiton: remove function callback

virtual bool insertSegmentWithTitle(const char title, int index, CAControlState controlState*
Return value: bool
Parameter:

Type

Parameter name

Description

const char*

title

segment’s displayed title

int

index

insert segment’s location

CAControlState

controlState

segment status

Descripiton: insert new segment in current segmentedControl and the position is the transferred index parameter (segment’s index value starts from 0 by default). Note: if the transferred index value is same with existing segment’s, then adding one on existing segment’s index value and all subsequent index value, and this rule applies to segment’s all insert methods.

Example:
CCRect segmentedControl_rect = CCRect(size. width/2, off_Y * 3 + 60, size. width * 0. 8f, height);
segmentedControl = CASegmentedControl: : createWithCenter(segmentedControl_rect, 2);
segmentedControl->insertSegmentWithTitle("2", 1, CAControlStateAll);
segmentedControl->insertSegmentWithTitle("3", 3, CAControlStateAll);
this->getView()->addSubview(segmentedControl);

Build a segmentedControl which includes two segments, and index and title value numbers from 0 by default, in addition, if you insert a new segment, its subsequent segment’s position will change.

virtual bool insertSegmentWithBackgroundImage(CAImage *image, int index, CAControlState controlState)
Return value: bool
Parameter:

Type

Parameter name

Description

CAImage*

image

segment’s background image

int

index

location where segment is inserted

CAControlState

controlState

segment status

Descripiton: insert segment and set background image according to transferred parameter, and title is blank by default.

*virtual bool insertSegmentWithImage(CAImage image, int index, CAControlState controlState
Return value: bool
Parameter:

Type

Parameter name

Description

CAImage*

image

segment’s foreground image

int

index

location where segment is inserted

CAControlState

controlState

segment status

Descripiton: insert segment and set foreground image according to transferred parameter, and title is blank by default.

virtual void removeSegmentAtIndex(int index)
Return value: void
Parameter:

Type

Parameter name

Description

int

index

segment’s serial number

Descripiton: remove designated segment according to index value

virtual void removeAllSegments()
Return value: void
Descripiton: remove all segments

int getItemCount() const
Return value: int
Descripiton: obtain segment’s count

Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

 CCRect segmentedControl_rect = CCRect(size.width/2, off_Y * 3 + 60, size.width * 0.8f, height);

    segmentedControl = CASegmentedControl::createWithCenter(segmentedControl_rect, 2);

    segmentedControl->insertSegmentWithTitle("2", 1, CAControlStateAll);

    segmentedControl->insertSegmentWithTitle("3", 3, CAControlStateAll);

    this->getView()->addSubview(segmentedControl);

         segmentedControl->addTarget(this,CAControl_selector(FirstViewController::segmentCallback));

void FirstViewController::segmentCallback(CAControl* btn, CCPoint point)

{        

         char desc[50];

          

         if (content)

         {

                   this->getView()->removeSubview(content);

         }

         content = CALabel::createWithCenter(CCRect(size.width*0.5, 450, 150, 80));

         content->setFontSize(30);

         content->setTextAlignment(CATextAlignmentCenter);

         this->getView()->addSubview(content);

  

         for (int i = 0; i getItemCount(); i++)

         {

                   if (i == segmentedControl->getselectedIndex())

                   {

                            sprintf(desc,"当前选择为%d",i);

                            content->setText(desc);

                            content->setColor(ccc4(51,204,255,255));

                   }

         }

}



void addTargetAtForbidSelected(CAObject target, SEL_CAControl selector)*
Return value: void
Parameter:

Type

Parameter name

Description

CAObject*

target

current object

SEL_CAControl

selector

function callback

Descripiton: add click event for disable-selected segment and enable these segment receive click event even though they cannot be selected.

void removeTargetAtForbidSelected(CAObject target, SEL_CAControl selector)*
Return value: void
Descripiton: remove function callback

void setForbidSelectedAtIndex(int index)
Return value: void
Parameter:

Type

Parameter name

Description

int

index

segment index value

Descripiton: set disable-selected segment according to transferred index value.

void setAllowSelectedAtIndex(int index)
Return value: void
Descripiton: allow the forbidden segment to be selected according to transferred index value.

Clone this wiki locally