IOS basics-UIButton

Source: Internet
Author: User

IOS basics-UIButton

Think about it, UIButton is a button, that is, we usually click a control with our fingers, and then there are corresponding events generated. When we click it, it may also produce color changes, which is called UIbutton.

It can display text, images, and internal images and text dynamically. It can also be used to add attribute strings. It doesn't matter if you don't understand the attribute strings, I will talk about it later.

 

UIButton has three statuses:

  1. Normal)

    Default)

    Corresponding enumerated constant: UIControlStateNormal

     

    1. Highlighted)

      When the button is pressed (the finger is not released yet)

      Corresponding enumerated constant: UIConrolStateHighlighted

       

      1. Disabled (invalid or unavailable)

        If the enabled attribute is 0, it is in the disable state, indicating that the button cannot be clicked.

        Corresponding enumerated constant: UIControlStateDisabled

         

        The following describes the UIButton attributes and methods in the code.

         

        -(Void) viewDidLoad {[super viewDidLoad];/* typedef NS_ENUM (NSInteger, UIButtonType) {UIButtonTypeCustom = 0, // custom style UIButtonTypeSystem restart (7_0 ), // standard system button UIButtonTypeDetailDisclosure, // The blue arrow button, which describes UIButtonTypeInfoLight, // UIButtonTypeInfoDark, // dark exclamation point UIButtonTypeContactAdd, // plus button UIButtonTypeRoundedRect = UIButtonTypeSystem, // The system default rounded Rectangle Button }; * // Create a button of the rounded rectangle type, which is also the default UIButton * btn = [UIButton buttonWithType: UIButtonTypeRoundedRect]; // The frame contains the coordinates and width and height of the control. Here, the coordinates (20, 20) and width and height (100,100) btn are directly set. frame = CGRectMake (20, 20,100,100); // set the background color of the button btn. backgroundColor = [UIColor greenColor]; // sets the rendering color of the button btn. tintColor = [UIColor blackColor];/* set the status here. Generally, the status is normal and highlighted. Of course, there are other statuses available for UIControlStateNormal = 0, // normal state: UIControlStateHighlighted = 1 <0, // The highlighted state is UIControlStateDisabled = 1 <1, // The disabled state is UIControlStateSelected = 1 <2, // The selected state is UIControlStateApplication = 0x00FF0000, // when the application flag is set, UIControlStateReserved = 0xFF000000 // internal reserved * // fill the image with the set button [btn setImage: [UIImage imageNamed: @ "2.png"] forState: UIControlStateNormal]; // when the filling image of the set button is highlighted, that is, when it is clicked [btn setImage: [UIImage imageNamed: @" 1.png"] forState: UIControlStateHighlighted]; // set button The text displayed on [btn setTitle: @ "" forState: UIControlStateNormal]; // set the color of the text on the button [btn setTitleColor: [UIColor redColor] forState: UIControlStateNormal]; // set the shadow color of the text on the button [btn setTitleShadowColor: [UIColor blueColor] forState: UIControlStateNormal]; // set the background image of the button [btn setBackgroundImage: [UIImage imageNamed: @ ""] forState: UIControlStateNormal]; // set the button's property string. If you do not know what the attribute string is, you can skip NSAttributedString * attrString = [[NSAttri ButedString alloc] initWithString: @ "set attribute string"]; [btn setAttributedTitle: attrString forState: UIControlStateNormal]; // set the inner spacing of the button's internal image // top bottom left right or left/UIEdgeInsets inserts = UIEdgeInsetsMake (0, 0, 0, 0); // by default, when the button is highlighted, the color of the image will become deeper. If no is set here, the status btn will be canceled. adjustsImageWhenHighlighted = NO; // by default, when the button is disabled, the color of the image will become deeper. If no is set here, the status btn will be canceled. adjustsImageWhenDisabled = NO; // when the value is set to YES Will emit btn. showsTouchWhenHighlighted = YES;/* UIControlEventTouchDown // single-point touch event: when a user clicks the screen, or when a new finger falls, UIControlEventTouchDownRepeat // multi-point touch event, trigger count greater than 1: when the user presses the second, third, or fourth finger. UIControlEventTouchDragInside // when one touch is dragged in the control window. UIControlEventTouchDragOutside // when one touch is dragged outside the control window. UIControlEventTouchDragEnter // when a touch is dragged from outside the control window to the interior. UIControlEventTouchDragExit // when a touch is dragged from the inside of the control window to the outside. UIControlEventTouchUpInside // all the touch-up events within the control. UIControlEventTouchUpOutside // all events that are raised by the touch outside the control (the touch must start with the control before a notification is sent inside the control ). UIControlEventTouchCancel // all touch cancellation events, that is, one touch is canceled because too many fingers are put, or locked or the call is interrupted. UIControlEventTouchChanged // send a notification when the control value changes. It is used for slider, segment control, and other value controls. You can configure the slider control to send notifications when the slider is put down, or when the slider is dragged. UIControlEventEditingDidBegin // a notification is sent when the text control is edited. UIControlEventEditingChanged // a notification is sent when the text in the text control is changed. UIControlEventEditingDidEnd // a notification is sent when the editing ends in the text control. UIControlEventEditingDidOnExit // send a notification when the edit is completed by pressing the Enter key (or equivalent act) in the text control. UIControlEventAlltouchEvents // notify all touch events. UIControlEventAllEditingEvents // notify all events about text editing. UIControlEventAllEvents // notifies all events. * // Add a click event to the button [btn addTarget: self action: @ selector (btnClick) forControlEvents: UIControlEventTouchUpInside]; // delete all event processing in a certain state // [btn removeTarget: nil action: nil forControlEvents: UIControlEventTouchUpInside]; // Add a long-press event UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (btnLong :)]; // set how long the event will trigger the longPress event. minimumPressDuratio N = 1; // Add the event to the button [btn addGestureRecognizer: longPress]; // Add the button to the Controller view // note, never forget this step [self. view addSubview: btn];}-(void) btnClick {NSLog (@ "% s", _ func _);}-(void) btnLong :( UILongPressGestureRecognizer *) gestureRecognizer {if ([gestureRecognizer state] = running) {NSLog (@ "Long press event"); UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "message: @ "are you sure you want to delete it? "Delegate: self cancelButtonTitle: @" cancel "otherButtonTitles: @" delete ", nil]; [alert show];}


         

Related Article

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.