Utilizzare un UIButton.
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:someUIImage forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(aMethod) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
Alternativa a UIButton (metodo più)
Un UIControl
implementa le interazioni degli utenti e il supporto per i già a grana fine interazioni con l'utente. È possibile combinare le funzionalità di une un UIControl
per ottenere questo poiché entrambe sono sottoclassi di UIView
.
Per ottenere questo comportamento, aggiungere un oggetto di UIImageView come sottoview a UIControl
in modo che l'immagine sia completamente coperta. Quindi aggiungi un gestore di eventi a questo controllo usando addTarget:action:forControlEvents:
. Ecco un esempio:
// assuming the image view object exists
UIImageView *anImageView = ..;
// create a mask that the covers the image exactly
UIControl *mask = [[UIControl alloc] initWithFrame:anImageView.frame];
// add the image as a subview of this mask
CGSize imageSize = anImageView.frame.size;
anImageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
[mask addSubview:anImageView];
// add a target-action for the desired control events to this mask
[mask addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
// add the mask as a subview instead of the image
[self.view addSubview:mask];
Per TouchDown, si intende 'UIControlEventTouchDown'? – Anurag
sì intendo UIControlEventTouchDown –