2015-05-28 13 views
41

Come è possibile aggiungere l'azione touchbegin UIView o l'azione touchend programmaticamente come Xcode non fornisce da Main.storyboard?Swift, evento di tocco UIView nel controller

+0

che uno è per il tasto, l'OP vuole aggiungere evento per UIView – Miknash

+0

Usare un 'UILongPressGestureRecognizer' con il' minimumPressDuration' impostato a zero. [Vedi questa risposta.] (Http://stackoverflow.com/a/38143429/3681880) Non richiede la sottoclasse o l'override di nulla. – Suragch

risposta

84

Dovrai aggiungerlo tramite codice. Prova questo:

// 1.create UIView programmetically 
    var myView = UIView(frame: CGRectMake(100, 100, 100, 100)) 
    // 2.add myView to UIView hierarchy 
    self.view.addSubview(myView) 
    // 3. add action to myView 
    let gesture = UITapGestureRecognizer(target: self, action: "someAction:") 
    // or for swift 2 + 
    let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:))) 
    self.myView.addGestureRecognizer(gesture) 

    func someAction(sender:UITapGestureRecognizer){  
     // do other task 
    } 

    // or for Swift 3 
    func someAction(_ sender:UITapGestureRecognizer){  
     // do other task 
    } 
+1

Ho fatto ciò mi sta dando un errore quando clicco su uiView. Il mio codice: lasciare gesto = UITapGestureRecognizer (target: self.uiPendingView, l'azione: "touchPending") self.uiPendingView.addGestureRecognizer (gesto) e il metodo: func touchPending (mittente: ANYOBJECT) { println ("METODO >> >>>>>>>>>>>>>>> ") } –

+1

aggiungi: in" touchPending: "in funzione assomiglia a func touchPending (mittente: UITapGestureRecognizer) –

+1

ti manca ':' in azione . – Miknash

6

mettere questo nel vostro UIView sottoclasse (è più facile se si effettua una sublcass per questa funzionalità).

class YourView: UIView { 

    //Define your initialisers here 

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    if let touch = touches.first as? UITouch { 
     let currentPoint = touch.locationInView(self) 
     // do something with your currentPoint 
    } 
    } 

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { 
    if let touch = touches.first as? UITouch { 
     let currentPoint = touch.locationInView(self) 
     // do something with your currentPoint 
    } 
    } 

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
    if let touch = touches.first as? UITouch { 
     let currentPoint = touch.locationInView(self) 
     // do something with your currentPoint 
    } 
    } 
} 
+0

@Chacle, ho più di 10 Uiview nella mia pagina e voglio aggiungere l'azione in alcuni sotto UIView. Quindi per quello che dovrei cambiare? –

+0

Dipende da cosa vuoi usarlo. Vuoi dire quale 'UIView' premi o vuoi gestire alcune press in ciascuna delle' UIView'? – Chackle

+0

Voglio solo per alcuni specifici UIview. –

15

Aggiornamento @ risposta di Chackle per 2.x Swift:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if let touch = touches.first { 
     let currentPoint = touch.locationInView(self) 
     // do something with your currentPoint 
    } 
} 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if let touch = touches.first { 
     let currentPoint = touch.locationInView(self) 
     // do something with your currentPoint 
    } 
} 

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if let touch = touches.first { 
     let currentPoint = touch.locationInView(self) 
     // do something with your currentPoint 
    } 
} 
1

Solo un aggiornamento di risposte di cui sopra:

Se si desidera avere vedere i cambiamenti nel l'evento click, vale a dire Il colore del tuo cambio UIVIew cambia ogni volta che l'utente fa clic su UIView, quindi apporta le modifiche come segue ...

class ClickableUIView: UIView { 
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
      if let touch = touches.first { 
       let currentPoint = touch.locationInView(self) 
       // do something with your currentPoint 
      } 

      self.backgroundColor = UIColor.magentaColor()//Color when UIView is clicked. 
     } 

     override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
      if let touch = touches.first { 
       let currentPoint = touch.locationInView(self) 
       // do something with your currentPoint 
      } 

      self.backgroundColor = UIColor.magentaColor()//Color when UIView is clicked. 
     } 

     override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
      if let touch = touches.first { 
       let currentPoint = touch.locationInView(self) 
       // do something with your currentPoint 
      } 

      self.backgroundColor = UIColor.whiteColor()//Color when UIView is not clicked. 

}//class closes here 

Inoltre, chiamare questa classe da Storyboard & ViewController come:

@IBOutlet weak var panVerificationUIView:ClickableUIView! 
14

Aggiornamento @ risposta di Crashalot per 3.x Swift:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
     let currentPoint = touch.location(in: self) 
     // do something with your currentPoint 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
     let currentPoint = touch.location(in: self) 
     // do something with your currentPoint 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
     let currentPoint = touch.location(in: self) 
     // do something with your currentPoint 
    } 
} 
21

Aggiornamento per Swift 4:

let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction)) 
self.myView.addGestureRecognizer(gesture) 

@objc func checkAction(sender : UITapGestureRecognizer) { 
    // Do what you want 
} 

Aggiornamento per Swift 3:

let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction(sender:))) 
self.myView.addGestureRecognizer(gesture) 

func checkAction(sender : UITapGestureRecognizer) { 
    // Do what you want 
} 
1

per SWIFT 4

@IBOutlet weak var someView: UIView! 
let gesture = UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:))) 
self.someView.addGestureRecognizer(gesture) 

@objc func someAction(_ sender:UITapGestureRecognizer){ 
    print("view was clicked") 
}