Using button subclasses to encapsulate fake segmented controls _ custom controls

Source: Internet
Author: User

This article is written with a button to encapsulate a control inherited from the UI view, the effect is to appear to be segmented control, but you can customize the different status of the button in the background picture, you can set whether the need for underline identification, the way the proxy, Let the controller get the subscript of the currently selected button for the next action.

First, I defined an array in a class that inherits from the UI view: An array of control headers, and then defines the set method. Because I want to set the picture of the button, I also define a picture array group in normal and selected state, and if you need an underscore to identify the attribute

@interface Customcontrlbtnview:uiview
{
    nsarray *_menuarray;//header Array

    //whether the bottom underlined
    BOOL _isneedline is required;
}

//The background picture of the normal state
@property (nonatomic, strong) Nsarray *imgarr;

When selected background picture
@property (nonatomic, strong) Nsarray *selectimgarr;

Do you need to underline the bottom
@property BOOL isneedline;

Delegate
@property (nonatomic, strong) ID <MenuProtocol> mydelegate;

-(void) Setnamewitharray: (Nsarray *) Menuarray;  Set method

The attribute definition of type bool is also written in the. m file:

@synthesize isneedline = _isneedline;

The agent follows the protocol, so it defines a protocol

Protocol
@protocol Menuprotocol <NSObject>

@optional//optional

@required//Required

-(void) Gettag: ( Nsinteger) tag;//Gets the currently selected subscript

@end

Then, in the implementation method of the Set method, create the button separately, setting the button's frame:

-(void) Setnamewitharray: (Nsarray *) Menuarray {_menuarray = Menuarray;
    Calculate the width of each button cgfloat BTNW = self.frame.size.width/[_menuarray count];
        for (int i = 0; i < Menuarray.count i++) {UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];

        Btn.frame = CGRectMake (i * btnw, 0, BTNW, self.frame.size.height);

        Btn.backgroundcolor = [Uicolor Clearcolor];

        Set Tag Value Btn.tag = 1501 + i;

        Sets the default first check if (Btn.tag = 1501) {btn.selected = YES; //Determine if there is a background image if (Self.imgArr.count > 0) {//Set background picture [BTN setbackgroundimage:[
            UIImage Imagenamed:self.imgarr[i]] forstate:uicontrolstatenormal];
        [BTN Setbackgroundimage:[uiimage Imagenamed:self.selectimgarr[i]] forstate:uicontrolstateselected]; ///////(_isneedline) {////Set button font size color status (BTN Settitlecolor:kcontrl) based on there is no underline Color Forstate:uiconTrolstatenormal];

        [Btn Settitlecolor:kitemcolor forstate:uicontrolstateselected];
            }else {//Set the font size color state of the button [btn settitlecolor:kskybluecolor forstate:uicontrolstatenormal];
        [btn Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstateselected];

        }//Set title [Btn Settitle:_menuarray[i] forstate:uicontrolstatenormal];

        Add Click event [btn addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside];
    [Self addsubview:btn]; //Create a bottom dash (to determine if there is an underscore first) if (_isneedline) {UIView *markline = [[UIView alloc] initwithframe:cgrectmake (0, SE
        Lf.frame.size.height-2, BTNW, 2)];
        Markline.tag = 999;
        Markline.backgroundcolor = [Uicolor colorwithpatternimage:[uiimage imagenamed:@ "rectangular -2-3.png"]];
    [Self addsubview:markline]; }


}

Click the Event trigger method, change the selected state, if there is an underscore identification, animation movement, and then trigger the proxy method:

-(void) Btnclick: (UIButton *) button
{
    //traverse a child view array, set the selected button's check value to Yes, and the other to No for
    (UIView *subview in Self.subviews) {
        if ([Subview Iskindofclass:[uibutton class]]) {
            UIButton *subbtn = (UIButton *) Subview;
            if (Subbtn.tag = = Button.tag) {

                [subbtn setselected:yes];

            } else {

                [subbtn setselected:no];

    }}} if (_isneedline) {
        //animation moves the bottom line (judged)
        uiview *markview = (UIView *) [self viewwithtag:999];

        [UIView animatewithduration:0.2f animations:^{
            markview.frame = CGRectMake ((button.tag-1501) * ( Button.frame.size.width), Self.frame.size.height-2, Button.frame.size.width, 2);
        }

    Implement Proxy Method
    if ([Self.mydelegate respondstoselector: @selector (Gettag:)]) {
        [self.mydelegate Gettag: Button.tag];
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.