2010-09-23 11 views
34

Quiero llamar a mi evento de clic de botón mientras toco la uiimageview nombrada como (imagen1) .Imageview colocada en uiview (view1).UIImageView Touch Evento

Este es mi código:

- (IBAction)buttonClicked:(id)sender 
{ 

myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(alphabutt:) userInfo:nil repeats:NO]; 
} 


-(IBAction)alphabutt:(id)sender 
{ 
NSLog(@"alphabutt!"); 


if(i==1) 
{ 
    NSLog(@"i==1"); 
    [image1 setImage:[UIImage imageNamed:@"appleimg.jpg"]]; 
    [view1 addSubview:image1]; 
    transition1 = [CATransition animation]; 

    transition1.duration = 0.75; 

    transition1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 

    transition1.type=kCATransitionReveal; 
    transition1.subtype=kCATransitionFromRight; 
    transitioning = YES; 
    transition1.delegate = self; 
    [image1.layer addAnimation:transition1 forKey:nil]; 
} 
if(i==2) 
{ 
    NSLog(@"i==2"); 
    [image1 setImage:[UIImage imageNamed:@"boatimg.jpg"]]; 
    [view1 addSubview:image1]; 
    transition2 = [CATransition animation]; 

    transition2.duration = 0.75; 

    transition2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 

    transition2.type=kCATransitionFade; 
    transition2.subtype=kCATransitionFromRight; 
    transitioning = YES; 
    transition2.delegate = self; 
    [image1.layer addAnimation:transition2 forKey:nil]; 

    i=0; 
} 
i++; 

} 

Lo anterior es mi código mientras hace clic en el botón del evento es el evento buttonClicked called.Inside ButtonClick el temporizador está trabajando para llamar al (evento de clic de los botones) alphabutt con el tiempo Se llama al intervalo de 2.0.alphabutt con cada 2.0. Quiero hacer esta animación cuando toco la uiimageview (imagen1) y luego solo llama al evento click del botón (alphabutt). cómo puedo escribir el evento táctil para imageview ...

Por favor, explique brevemente con un poco de código para realizar eventos táctiles UIImageView ...

+0

por favor, responda a la brevedad posible ... –

+0

Por favor, ALW AYS formatea tu código. Su pregunta es confusa sobre el toque de vista de imagen y el botón táctil (¿qué desea preguntar aquí?) – vodkhang

+1

Disculpe por no formatear el código ... En mi código dentro del evento clic botón NSTimer (myTimer) se utiliza para llamar al otro botón haga clic en evento (alphabutt) con el interlock de tiempo de 2.0. En lugar del temporizador, quiero llamar al alphabutt (evento de clic de botón) por uiimageview (image1) touchevent. ¿Cómo puedo hacer esto? Por favor, ayúdenme ... –

Respuesta

56

Haga su interacción con el usuario habilitado vista de la imagen en el xib (o a través del código si va a agregar mediante programación) y el uso de los métodos UIResponder touchesBegan, touchesMoved, etc touchesEnded para detectar el toque en la vista de la imagen:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 

    if ([touch view] == yourImageView) 
    { 
      //add your code for image touch here 
    } 

} 
+0

Gracias Iukya ... –

+0

hola ... Mi aplicación se basa en la aplicación tabbar. Le doy todo el código para eventos táctiles dentro de myAppAppDelegate.m ... No funciona, ¿qué debo hacer en mi aplicación? Explique brevemente ... Estoy esperando Responda pronto ... Gracias ... –

+0

No tiene que manejar los toques en la aplicación delegada ... implemente los métodos táctiles en su controlador de vista que contiene la imagen ... – lukya

59

puede utilizar UITapGestureRecognizer añade a la UIImageView través addGestureRecognizer

fragmentos:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)]; 
singleTap.numberOfTapsRequired = 1; 
singleTap.numberOfTouchesRequired = 1; 
[iv addGestureRecognizer:singleTap]; 
[iv setUserInteractionEnabled:YES]; 

y

- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer { 
    NSLog(@"%@", [gestureRecognizer view]); 
} 
6

También puede arrastrar y soltar un objeto sobre el UIButtonUIImageView. Luego, en el editor del guión gráfico, simplemente haga el UIButton del tipo Custom, lo que hace que el UIButton sea invisible. A continuación, gestione el evento click tal como lo haría con cualquier botón.

+0

Verificar ** Versión Swift ** [http://stackoverflow.com/a/41328885/3177007](http://stackoverflow.com/a/41328885/3177007) –

13

Aquí os muestro dos posibilidades para lograr que en Swift:

primera posibilidad

import UIKit 

class ViewController: UIViewController { 
    @IBOutlet var myUIImageView: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // You can also manually set it through IB 
     // selecting the UIImageView in the storyboard 
     // and checking "User Interaction Enabled" checkbox 
     // in the Attributes Inspector panel. 
     self.myUIImageView.userInteractionEnabled = true 
    } 

    // You can choose to use one of the UIResponder methods: 
    // touchesBegan, touchesMoved, touchesEnded etc, in order to detect the touch 
    // on the UIImageView. 
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     let touch: UITouch? = touches.first 
     if touch?.view == myUIImageView { 
      println("myUIImageView has been tapped by the user.") 
     } 
     super.touchesEnded(touches, withEvent: event) 
    } 
} 

segunda posibilidad

import UIKit 

class ViewController: UIViewController { 
    @IBOutlet var myUIImageView: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let singleTap = UITapGestureRecognizer(target: self, action: "myUIImageViewTapped:") 
     singleTap.numberOfTapsRequired = 1 // Initialized to 1 by default 
     singleTap.numberOfTouchesRequired = 1 // Initialized to 1 by default 
     self.myUIImageView.addGestureRecognizer(singleTap) 
     self.myUIImageView.userInteractionEnabled = true 
    } 

    func myUIImageViewTapped(recognizer: UITapGestureRecognizer) { 
     if(recognizer.state == UIGestureRecognizerState.Ended){ 
      println("myUIImageView has been tapped by the user.") 
     } 
    } 
} 
+0

primera posibilidad funcionó como un encanto !! –