Do you really know about UIControl ?, UIControl
I. First, let's take a look at the definition of UIControl.
NS_CLASS_AVAILABLE_IOS (2_0) @ interface UIControl: UIView // The control is enabled by default. YES. Whether to disable the control @ property (nonatomic, getter = isEnabled) BOOL enabled; @ property (nonatomic, getter = isSelected) BOOL selected; // default value: NO when you select the control, the UIControl class sets its selected Attribute to YES. The subclass sometimes uses this attribute to allow the control to select itself or display different behavior methods. @ Property (nonatomic, getter = isHighlighted) BOOL highlighted; // The default value is NO. This is how to set/clear automatically when the touch enters/exits in the tracking process, and how to clear the // control to deploy its content vertically. By default, the @ property (nonatomic) uicontrolcontentverticalignment contentverticalignment on the top of the content; // horizontal alignment @ property (nonatomic) UIControlContentHorizontalAlignment contenthorizontalalign; // The current UIControlState status read-only @ property (nonatomic, readonly) UIControlState state; // to determine whether the current object is tracking the touch operation, if this value is YES, it indicates that the tracing is in progress. Read-Only @ property (nonatomic, readonly, getter = isTracking) BOOL tracking; // to determine whether the current touch point is in the control area class, you can use the touchInside attribute, this is a read-only attribute @ property (nonatomic, readonly, getter = isTouchInside) BOOL touchInside; // trace touch events-(BOOL) beginTrackingWithTouch :( UITouch *) touch withEvent :( nullable UIEvent *) event;-(BOOL) continueTrackingWithTouch :( UITouch *) touch withEvent :( nullable UIEvent *) event;-(void) endTrackingWithTouch :( nullable UITou Ch *) touch withEvent :( nullable UIEvent *) event;-(void) cancelTrackingWithEvent :( nullable UIEvent *) event; // Add Target-(void) addTarget :( nullable id) target action :( SEL) action forControlEvents :( UIControlEvents) controlEvents; // remove Target-(void) identifier :( nullable id) target action :( nullable SEL) action forControlEvents :( UIControlEvents) controlEvents; // obtain all target objects related to the control object. You can call the allTargets method to return a set. The collection may contain NSNull objects, indicating at least one nil target object-(NSSet *) allTargets; // obtain all Events-(UIControlEvents) allControlEvents of the last action; // obtain a target object and all actions related to the event, you can call-(nullable NSArray <NSString *> *) actionsForTarget :( nullable id) target forControlEvent :( UIControlEvents) controlEvent; // forward the behavior message to the UIApplication object, and then the UIApplication object calls its sendAction: to: fromSender: forEvent: Method to distribute the message to the specified target, if no target is specified, the event will be distributed to the first user in the response chain who wants to handle the cancellation.. If the subclass wants to monitor or modify such behavior, you can override this method. -(Void) sendAction :( SEL) action to :( nullable id) target forEvent :( nullable UIEvent *) event; // The function of the method is to send all behavior messages related to the specified type-(void) sendActionsForControlEvents :( UIControlEvents) controlEvents; @ end
UIControl inherits from UIView and is also a subclass of UIResponder. UIControl is the parent class of controls such as UISwitch, UIButton, UISegmentedControl, UISlider, UITextField, and UIPageControl. It also contains some attributes and methods, but cannot directly use the UIControl class, it only defines the methods required by subclass. UIControl is the base class of the control class. It is an abstract base class. We cannot directly use the UIControl class to instantiate the control. It just defines some common interfaces for the control subclass, it also provides some basic implementations to pre-process these messages when an event occurs and send them to the specified target object. The UIControl object uses a new event processing mechanism to convert a touch event to a simple operation, so that you do not need to care about the specific method of the user access control.
Knowledge Point 1: addTarget: action: forControlEvents
This is a method of UIControl. It adds events to the specified control object, for example, [controlObj addTarget: policientobj action @ selector (method) froControlEvents: UIControlEvents]; controlObj is the control object that responds to the event. The receientObj parameter is the control object that instantiates the control object. The action is followed by a selector, indicates the method that the event needs to respond, and the event is actually written in the method. The last one is the event type, indicating the event to respond.
Knowledge Point 2: Code simulated user clicks
Simulate the event sendActionsForControlEvents of the UI, for example, simulating the user click event:
[MyBtn sendActionsForControlEvents: UIControlEventTouchUpInside];
Instance:-(void) viewDidLoad {//... [control addTarget: self action: @ selector (tapImageControl :) forControlEvents: UIControlEventTouchUpInside]; [control sendActionsForControlEvents: Internal];}
You can see that the UIControlEventTouchUpInside event is triggered without clicking the control.
Knowledge Point 3: To delete the corresponding action of one or more events, you can use the removeTarget method of the UIControl class. The nil value can be used to delete all actions of the specified event target:
[ myControl removeTarget:myDelegate action:nil forControlEvents:UIControlEventAllEvents];
Knowledge Point 4: rewrite sendAction Application
/ImageControl. m-(void) sendAction :( SEL) action to :( id) target forEvent :( UIEvent *) event {// pass the event to the object to process [super sendAction: @ selector (handleAction :) to: self forEvent: event];}-(void) handleAction :( id) sender {NSLog (@ "handle Action");} // ViewController. m-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; ImageControl * control = [[ImageControl alloc] initWithFrame :( CGRect) {50366f, 100366f, 200366f, 300366f} title: @ "This is a demo" image: [UIImage imageNamed: @ "demo"]; //... [control addTarget: self action: @ selector (tapImageControl :) forControlEvents: UIControlEventTouchUpInside];}-(void) tapImageControl :( id) sender {NSLog (@ "sender =% @", sender );}
Since sendAction: to: forEvent: method is overwritten, the Selector that finally processes the event is the handleAction: Method of ImageControl, rather than the tapImageControl: Method of ViewController.