English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Soluzione al problema di non risposta del UIButton di iOS

Prefazione

Nel quotidiano sviluppo, spesso ci troviamo a interagire con i pulsanti, ma a volte ci si scontra con problemi piuttosto difficili da risolvere, ovvero i pulsanti che non rispondono agli eventi di clic, in questo caso dobbiamo verificare da più parti

La ragione per cui il pulsante non risponde

1. Il pulsante è stato aggiunto a un genitore View che non ha attivato l'interazione utente, ad esempio UIImageView, in questo caso, abilita l'interazione del genitore view view.userInteractionEnabled = YES

Imposta su YES per risolvere il problema

2. Il pulsante stesso è nascosto, quando si fa clic non si clicca sul pulsante, ma su una view superiore, naturalmente non risponderà

C'è un metodo per vedere il livello qui, nella figura sottostante, fare clic sul pulsante nel cerchio può vedere gli elementi UI dell'interfaccia utente in esecuzione, puoi vedere se ci sono view che nascondono il pulsante

3. Il frame del pulsante supera il frame del genitore, questo è il più facile da apparire, il frame del pulsante deve essere all'interno del frame del genitore per funzionare correttamente, come nella figura sottostante, l'area di clic del pulsante nel cerchio non risponde.

Cosa fare se si incontra questo problema? Continua a leggere di seguito

Risolvi il problema di superare l'area di clic

Questo tipo di situazione è molto probabile che si verifichi, per esempio, l'altezza dell'area di chat è inferiore all'altezza della tastiera, e il campo di input è una sottovista dell'area di chat, dopo che la tastiera viene aperta, il campo di input si sposta in alto e supera il frame del genitore, in questo momento, fare clic sul pulsante nel cerchio per passare all'azione della tastiera di表情 non risponde.

Soluzione

Questi ci sono due metodi, ogni volta che c'è un'azione di touch, questi due metodi vengono eseguiti

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;

Riscrivi il metodo - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

Questo è un metodo all'interno di View, il flusso di lavoro è il seguente

  • Prima di tutto, chiama il metodo pointInside:withEvent: della vista corrente per determinare se il punto di tocco si trova all'interno della vista corrente;
  • Se restituisce NO, hitTest:withEvent: restituisce nil;
  • Se restituisce YES, invia il messaggio hitTest:withEvent: a tutte le sottoviste della vista corrente, l'ordine di esplorazione delle sottoviste è da top a bottom, ossia dall'ultimo elemento dell'array subviews verso l'inizio, fino a quando una sottovista restituisce un oggetto non vuoto o tutte le sottoviste sono state esplorate;
  • Se la prima sottovista restituisce un oggetto non vuoto, il metodo hitTest:withEvent: restituisce questo oggetto e la gestione termina;
  • Se tutte le sottoviste restituiscono non, il metodo hitTest:withEvent: restituisce se stesso (self).
  • Finally, this touch event is handed over to the view object returned by the main window's hitTest:withEvent: method to handle.

So we can handle it when returning nil, because at this time the button is outside the parent View

//Return a view to respond to the event
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
 UIView *view = [super hitTest:point withEvent:event];
 if (view == nil){
  //Convert coordinates
  CGPoint tempPoint = [self.testBtn convertPoint:point fromView:self];
  //Determine whether the point clicked is within the button area
  if (CGRectContainsPoint(self.testBtn.bounds, tempPoint)){
   //Return button
   return _testBtn;
  }
 }
 return view;
}

At this time, clicking on the button outside the area also has an effect

Summary

The above-mentioned solutions to the problem of iOS UIButton click without response introduced by the editor are for your reference. If you have any questions, please leave a message, and the editor will reply to you in time. We are also very grateful for everyone's support for the Shouting Tutorial!

Declaration: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not bear relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @) for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

Ti potrebbe interessare