"Original" (iOS development) highlighted and selected two kinds of button state principle explore share

Source: Internet
Author: User

First, the following common sense: In short, the button has a click, non-click 2 States, click on the system will automatically invoke the method of setting the button highlighting state (sethighlighted:), non-click as normal state (normal). We can set different background images according to the needs of different states, Apple officially divided the button state into 3 kinds: highlight (highlighted), check (selected), Normal (normal), if you write code when the method is called (setselected :), the button is selected (selected).

3 button states, Apple officially named:

Uicontrolstatenorma (normal), uicontrolstatehighlighted (highlight), uicontrolstateselected (selected)

Next, the 2-button background image is used as a commentary:

Uicontrolstatenorma status selection gray picture Tabbar

Uicontrolstatehighlighted or uicontrolstateselected status selection golden picture Tabbarsel

again, when the button is clicked, the System method (sethighlighted:) is called, and the highlighted state picture is displayed as a pre-set, and if not previously set, the default display is to add a layer of gray to the original picture. As soon as you do not release the hand, you will remain highlighted state, release the hand to restore the normal state.

Now look at the following code:

Set Button picture background

NSString *name = [NSString stringwithformat:@ "TabBar"];

[Button Setbackgroundimage:[uiimage Imagenamed:name] forstate:uicontrolstatenormal];

NSString *selectedname = [NSString stringwithformat:@ "Tabbarsel"];

[Button setbackgroundimage: [UIImage imagenamed:selectedname] forstate: uicontrolstatehighlighted];// Line a

Monitor button click

[Button addtarget:self action: @selector (ButtonClick:) forcontrolevents: Uicontroleventtouchdown]; //Line E

Click the button to invoke the method

-(void) ButtonClick: (UIButton *) button

{

button.selected = YES; //Line B

}

Set the Highlight state method

-(void) sethighlighted: (BOOL) highlighted

{

[Super sethighlighted:highlighted]; //Line C

}

Set the check state method

-(void) setselected: (BOOL) selected

{

[Super setselected:selected]; //Line D

}

Code Explanation:

The code that is posted is a lite version that conveys a clear meaning. A, B, C, D, E to make different degrees of change, after running the click button will see different effects.

    1. comment out line b , click on the button to display the highlighted gold picture, release the gray image to restore the normal state. This is easy to understand.
    2. comment out line B, comment out line C , click the button, the button does not change anything. Because [Super sethighlighted:highlighted] is set to highlight state, after commenting out, the method does not do anything, so it does not change.
    3. Do not make any changes , click the button, the button in the original picture to add a layer of gray. Although the (setselected:) method is called, but because the background picture is not set in the selected state, it is only the effect of highlighting.
    4. change the uicontrolstatehighlighted of line A to uicontrolstateselected, add a gray highlight to the original picture when clicked, and let go of the golden picture that the hand is converted to the selected state. If you want to remove the highlight state, comment out the line C , click the direct change Gold image.

Note: If you change the uicontroleventtouchdown of line E to UIControlEventTouchUpInside, it will show a little difference when applied to the above 4 cases, The reason is estimated in Uicontroleventtouchdown is clicked on the call method ButtonClick:, and UIControlEventTouchUpInside is in the release of the hand before the call, the concrete effect you can try it yourself.

Original (iOS development) highlighted and selected two kinds of button state principle explore and share

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.