1. Contents Buttons are very important components in all UI systems. The use of UIButton in iOS is also very flexible. This article will introduce the use of UIButton from the following points (based on Swift 2.0):
2. UIButton Basics 2.1 Creation UIButton provides a simple construction method
This method requires us to pass in a UIButtonType enumeration type. The usage code is as follows:
Tips: 1. When setting the button title, be sure to pass
Not passable
This method will automatically change the title to the text in the Normal state of the setTitleColor method when clicked. 2.2 Image Usage UIButton provides the following two interfaces to use images:
(1) The interface setImage is used to set the button image. By default, it will be aligned horizontally with the button text. (2) The interface setBackgroundImage is used to set the background image of the button. Both setImage and the button text will be displayed on the background image. Here we focus on the setBackgroundImage interface. Many times, the button looks like this: These buttons have the same background, but different sizes. Let's talk about how to reuse this type of image resources. 2.2.1 Code method 2.2.1.1 Principle Description In the UIImage interface, there are the following methods
When using this method, you need to pass UIEdgeInsets as a parameter and create the interface as follows: This method provides up, down, left, and right parameters to create a variable area, as shown in the following figure (Tips: The variable area shown in the figure below is different from the view's inner margin) In the figure, the blue mark indicates the variable area, and the green mark indicates the constant area. The top and bottom properties of the UIEdgeInsets structure are a pair, which are used to specify the vertical variable area (black dotted rectangle), and the left and right properties are a pair, which are used to specify the horizontal variable area (white dotted rectangle). When the size of UIButton/UIImageView is larger than the size of UIImage, the size of the variable area in the image will be adjusted to cover the entire control. The specific adjustment rules are as follows: (1) The width of the control is greater than the width of the image, so the white dotted rectangle is stretched (2) If the control height is greater than the image height, stretch the black dotted rectangle (3) When the control width is smaller than the image width, the entire horizontal area is reduced (the ratio of the variable area to the constant area remains unchanged) (4) When the control height is smaller than the image height, the overall vertical reduction (the ratio of the variable area to the constant area remains unchanged) The iOS system will automatically load 1x, 2x, and 3x images according to the device's resolution, and the top, bottom, left, and right in the resizableImageWithCapInsets method are in pixels. This requires that when using it, the corresponding margins should be set according to the x-fold image, for example:
#p# 2.2.1.2 Performance vs. Variable Region Size (1) Performance Optimization: When the variable area is 1 pixel wide or high, the drawing is done by stretching 1 pixel (2) Better performance: The variable area is the entire image, the parameter of the resizableImageWithCapInsets method is UIEdgeInsetsZero, and the entire image is tiled when drawing. (3) Poor performance: When the width or height of the variable area is greater than 1 pixel, the drawing is done in a tiling manner. This method has poor performance, but it is also the most commonly used method in actual development. Tips In some applications, the application has some non-pure color backgrounds, which will be used in multiple interfaces. Due to the differences in device resolution and interface control size, it is required to create images of multiple sizes, which will cause the ipa package to become larger and memory usage to increase. Here, combined with the above (2) setting the variable area to the entire image, this problem can be solved. For the principle, please refer to Seamless Mapping. The sample code is as follows:
Xcode provides an Asset Catalogs method to manage image resources. Asset Catalogs provides a visual interface to set the variable area of the image. It is easy to operate and simple to use. Click Show Slicing in the lower right corner. After entering the edit mode, there will be a Start Slicing button in the middle of the image. After clicking it, we will be asked to select the stretching method, as shown below: The functions of the three buttons
The horizontal and vertical stretching processes are the same. Here we take the horizontal stretching as an example. After selecting the horizontal stretching button 1, three operation lines will be provided to specify the variable area and the deletion area. Variable area: The area specified by operation line 1 and operation line 2. When stretching, the size of this area will change according to the final size. Delete area: The area specified by operation line 2 and operation line 3 (white semi-transparent layer). It can be simply understood that this area will be directly deleted when stretching. The usage is the same as that of ordinary pictures. The code is as follows:
#p# 3. Other uses of UIButton 3.1 Rounded Corner Buttons Sometimes, we need a round button, such as an avatar:
3.2 Checkbox Button What if there is no checkbox component in UIKit?
3.3 Countdown button (flickering problem can also be easily solved) Many applications have a text message countdown function, which usually combines NSTimer with UIButton to implement this function. If UIButton is initialized like this:
When testing, you will find that when the timer changes the title every second, there will be flickering. Change UIButtonType.System to UIButtonType.Custom Here is a packaged countdown button that you can download and use directly: http://00red.com/download/Swift之贪的UIButton/ILCountDownButton.swift The following are some usage examples:
4. Greedy Button The frame of UIButton will directly affect the display effect of setImage and setBackgroundImage. Sometimes we just need to expand the click area of UIButton, but don't want to directly modify the frame of UIButton and affect the display. In this case, we can use the following method to deal with it The click event of the superview of UIButton is occupied, and all touch operations are transferred to the UIButton control. When iOS handles event distribution, it is divided into two steps: the first step is to find which UI component responds to this event, and the second step is event processing and responder chain. To realize event transfer, just handle it in the first step. The code is as follows:
When using ILGreedButton, the effect of clicking the parent view and UIButton responding to the event will appear. |
<<: How to avoid Null Pointer Exception in Java
>>: A collection of top-notch front-end special effects resources#001
I wishful thinking that, following a heartwarming...
What I am sharing today has been verified by hund...
As the saying goes, "the more autumn rain fa...
1. Layout brand words Before using video to attra...
In the past few years, the Internet industry has ...
Is event planning difficult? In the author's ...
THE END ▌The theme of the cartoon comes from the ...
Mouse Finance Four Mouse Gold Point System Course...
December 26 The 6th "Made in China Day"...
Since January 23 this year, when my country's...
1. What is Shenma Search ? Shenma Search is a mob...
A new UNC School of Medicine study finds that use...
Reviewer of this article: Xu Qibin, associate chi...
Science Times reporter Ji Chunhong The seasonal c...
The west wind blows, the crab legs itch, it’s the...