iOS11 development tutorial (23) iOS11 application view realizes button response (3)

iOS11 development tutorial (23) iOS11 application view realizes button response (3)

2. Use code to add the response of the button implementation

Using the button added by the code, you need to use the addTarget(_:action:for:) method to implement the response. Its syntax is as follows:

func addTarget(_ target: AnyObject?, action: Selector, for controlEvents: UIControlEvents)

Among them, the parameter descriptions are as follows:

  • target: Indicates the target object. It is the sender of the action message.
  • action: Represents a selector used to identify action messages. It cannot be empty.
  • controlEvents: Represents control events. There are 19 control events in iOS, as shown in Table 2-4.

Table 2-4 Control events

control event

explain

touchDown

Single-touch press event: when the user touches the screen, or when a new finger falls

touchDownRepeat

Multi-touch press event, touch count greater than 1: when the user presses the second, third, or fourth finger.

touchDragInside

When a touch drags inside the control window.

touchDragOutside

When a touch drags outside the control window.

touchDragEnter

When a touch drags from outside the control window to inside.

touchDragExit

When a touch is dragged from inside the control window to outside.

touchUpInside

All touch up events within the control.

touchUpOutside

All touch up events outside the control (the touch must be started and inside the control to send the notification).

touchCancel

All touch cancellation events, i.e. a touch was cancelled because too many fingers were placed, or it was interrupted by a lock or a phone call.

valueChanged

Send a notification when the value of the control changes. Controls for sliders, segment controls, and other values. Developers can configure when the slider control sends notifications

editingDidBegin

Send a notification when editing begins in the text control.

editingChanged

Send a notification when the text in the text control is changed.

editingDidEnd

Sends a notification when editing in the text control ends.

editingDidEndOnExit

Sends a notification when editing is ended by pressing the enter key (or equivalent) within the text control.

allTouchEvents

Notify all touch events.

allEditingEvents

Notify of all events related to text editing.

applicationReserved

Provides the value of a series of control events used by the application

aystemReserved

A frame reserved for internal use within the scope of the control event value

AllEvents

Notify all events.

[Example 2-5] The following will implement the function of tapping the button to change the background color of the main view. code show as below:

 

  • import UIKit
  • class ViewController: UIViewController {
  •     var isCyan:Bool=false
  •     override func viewDidLoad() {
  •         super.viewDidLoad()
  •         // Do any additional setup after loading the view, typically from a nib.
  •         let button=UIButton(frame: CGRect(x: 90, y: 545, width: 225, height: 30))
  •         button.setTitle("Tap me,Change View Color", for: UIControlState()) //Set the title of the button
  •         button.setTitleColor (UIColor.black, for: UIControlState()) //Set the color of the button title
  •         self.view.addSubview(button)
  •         button.addTarget(self, action: #selector(ViewController.tapbutton), for: UIControlEvents.touchUpInside)
  •     }
  •     @objc func tapbutton () {
  •         if(isCyan){
  •             self.view.backgroundColor=UIColor.white
  •             isCyan=false
  •         }else{
  •             self.view.backgroundColor=UIColor.cyan
  •             isCyan=true
  •         }
  •     }
  • ……
  • }

Run the program at this time, and you will first see the effect as shown in Figure 2.14. When you tap the Tap me, Change View Color button, the background of the main view becomes cyan, as shown in Figure 2.15. When you tap the Tap me, Change View Color button again, the background color of the main view will change back to the original white.

Figure 2.14 Running effect Figure 2.15 Running effect

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324997647&siteId=291194637