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.

Author: Sumit Patel, 2013-06-15

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")
}
 214
Author: Vivek Sehrawat,
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];
 17
Author: IronManGill,
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")
}
 4
Author: Sakiboy,
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.
la vista puede ser ImageView, UIView, UILabel, TextField u otro controlador


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.

Icono de gesto en la línea de salida del documento y xib
Arrastre el gesto a UILable
Arrastre el gesto a UILable


Crear manualmente el método (IBAction)en el archivo de encabezado


crear el método IBAction en el archivo de encabezado
Ahora arrástrelo del controlador al icono de gesto en xib
Enlázalo con el icono de gesto

 2
Author: Subhash,
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!)
            }
        }
    }
 2
Author: Jaleel Nazir,
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)

 0
Author: xxtesaxx,
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.

 0
Author: Raki,
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