2015-05-28 13 views
41

Jak mogę dodać akcję UIView touchbegin lub touchend programowo, ponieważ Xcode nie dostarcza z Main.storyboard?Swift, UIView zdarzenie dotykowe w kontrolerze

+0

To jest dla przycisku, PO chce dodać zdarzenie do UIView – Miknash

+0

Użyj 'UILongPressGestureRecognizer' z' minimumPressDuration' ustawiony na zero. [Zobacz tę odpowiedź.] (Http://stackoverflow.com/a/38143429/3681880) Nie wymaga podklasy ani przesłonięcia czegokolwiek. – Suragch

Odpowiedz

84

Będziesz musiał dodać to za pomocą kodu. Spróbuj tego:

// 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

Zrobiłem, że daje mi błąd, gdy klikam na uiView. Mój kod: niech gest = UITapGestureRecognizer (cel: self.uiPendingView, działanie: "touchPending") self.uiPendingView.addGestureRecognizer (gest) i metoda: func touchPending (nadawca: AnyObject) { println ("metoda >> >>>>>>>>>>>) } –

+1

po prostu dodaj: w "touchPending:" w funkcji wygląda jak func touchPending (nadawca: UITapGestureRecognizer) –

+1

brakuje ":" w akcji . – Miknash

6

umieścić to w swoim UIView podklasy (najłatwiej jeśli popełnisz sublcass dla tej funkcji).

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, mam więcej niż 10 Uiview na mojej stronie i chcę dodać akcję w niektórych Sub UIView. Więc co powinienem zmienić? –

+0

Zależy od tego, do czego chcesz go użyć. Czy chcesz powiedzieć, którego 'UIView' chcesz nacisnąć lub czy chcesz obsłużyć niektóre naciśnięcia w każdym z' UIView'? – Chackle

+0

Chcę touchevent tylko w przypadku niektórych specyfikacji UIview. –

15

Aktualizacja @ odpowiedź Chackle za Swift 2.x:

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

Wystarczy aktualizację powyższych odpowiedzi:

Jeśli chcesz mieć zobaczyć zmiany w zdarzenia click, czyli Kolor zmiany UIVIew shud za każdym razem, gdy użytkownik kliknie UIView, a następnie wprowadź zmiany, jak poniżej ...

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 

także wywołać tę klasę z Storyboard & ViewController jak:

@IBOutlet weak var panVerificationUIView:ClickableUIView! 
14

Aktualizacja @ odpowiedź Crashalot za Swift 3.x:

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

Aktualizacja dla Swift 4:

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

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

Aktualizacja dla 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

Dla szybkiej 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") 
} 
Powiązane problemy