Haga clic en Evento en UIImageView programáticamente en ios
Estoy mostrando una imagen de código aquí está el código
UIImageView *preArrowImage =[[UIImageView alloc]init ];
preArrowImage.image =[UIImage imageNamed:@"arrowprev.png"];
preArrowImage.frame = CGRectMake(20, 60, 10, 30);
[self.view addSubview:preArrowImage];
Quiero manejar el evento touch en preArrowImage mediante programación.
7 answers
Objective-c
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[preArrowImage setUserInteractionEnabled:YES];
[preArrowImage addGestureRecognizer:singleTap];
-(void)tapDetected{
NSLog(@"single Tap on imageview");
}
SWIFT 4
let preArrowImage : UIImageView // also give it frame
let singleTap = UITapGestureRecognizer(target: self, action: Selector("tapDetected"))
preArrowImage.isUserInteractionEnabled = true
preArrowImage.addGestureRecognizer(singleTap)
//Action
@objc func tapDetected() {
print("Imageview Clicked")
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-28 07:35:49
Simplemente agregue un UITapGesture
en la imagen, pero recuerde hacer su Interacción de usuario Habilitada.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(singleTapGestureCaptured:)];
[preArrowImage addGestureRecognizer:singleTap];
[preArrowImage setMultipleTouchEnabled:YES];
[preArrowImage setUserInteractionEnabled:YES];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-15 05:32:06
Ahora en Swift!
let singleTap = UITapGestureRecognizer(target: self, action: Selector("tapDetected"))
singleTap.numberOfTapsRequired = 1
preArrowImage.userInteractionEnabled = true
preArrowImage.addGestureRecognizer(singleTap)
//Action
func tapDetected() {
println("Single Tap on imageview")
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-21 22:55:22
Usando story board con view in view controller.
Después de seleccionar uno de los reconocedores de gestos, arrástrelo a su controlador.
Ahora se mostrará en "Esquema del documento" y encabezado de view controller xib.
Arrastre el gesto a UILable
crear el método IBAction en el archivo de encabezado
Ahora arrástrelo del controlador al icono de gesto en xib
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-20 05:24:25
Swift 3
On UIImageView enable UserInterAction
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
if let touch = touches.first {
if touch.view == self.imgVwPostPreview { //image View property
//Do your actions
//self.delegate?.didClickOnCellImageView(indexPath: self.cellIndexPath!)
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-04 11:54:12
Si su UIImageView es un IBOutlet que obtiene de un guion gráfico o Plumín, puede agregar un UIGestureRecognizer a la vista de imagen, habilitar la interacción del usuario y conectar un IBAction al reconocedor de gestos.
Para obtener más información, consulte mi respuesta a esta pregunta: Cómo hacer un UIImageView en el storyboard clickable (swift)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:03:04
Aquí hay otra opción: Hacer trampa.
Quiero decir, pensar lateralmente.
Configure su UIImage como desee, recortado, ajuste de aspecto, lo que sea.
Superponga esto con un UIView (dimensiones iguales, posición, etc.)
Establezca el fondo en clearcolour y la clase en UIControl.
Apunta el evento retoque interior a tu controlador y listo.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-24 14:48:41