Jak mogę dodać akcję UIView touchbegin lub touchend programowo, ponieważ Xcode nie dostarcza z Main.storyboard?Swift, UIView zdarzenie dotykowe w kontrolerze
Odpowiedz
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
}
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 >> >>>>>>>>>>>) } –
po prostu dodaj: w "touchPending:" w funkcji wygląda jak func touchPending (nadawca: UITapGestureRecognizer) –
brakuje ":" w akcji . – Miknash
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
}
}
}
@Chacle, mam więcej niż 10 Uiview na mojej stronie i chcę dodać akcję w niektórych Sub UIView. Więc co powinienem zmienić? –
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
Chcę touchevent tylko w przypadku niektórych specyfikacji UIview. –
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
}
}
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!
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
}
}
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
}
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")
}
- 1. Jak uzyskać zdarzenie dotykowe na CALayerze?
- 2. Dotykowe zdarzenie, które nie zwraca danych dotyku
- 3. Zdarzenie dotykowe: po usunięciu palca z elementu
- 4. ScrollView łapie zdarzenie dotykowe dla mapy google
- 5. Wykryj zdarzenie dotykowe na UIScrollView ORAZ na komponentach UIView [który jest umieszczony wewnątrz UIScrollView]
- 6. Przeciągnij UIView Swift 3
- 7. Swift 3 UIView animacja
- 8. Jak obsługiwać zdarzenie dotykowe na Uibuttonie dodanym w komórce UITableView?
- 9. Przesuwne menu blokuje zdarzenie dotykowe w górnym widoku
- 10. UIButton wewnątrz UIView nie reaguje na zdarzenia dotykowe
- 11. Zdobądź centrum dowolnego UIView w Swift
- 12. Jak programowo przenieść UIView w Swift
- 13. iOS 9 Swift UIView nie ma członka
- 14. Animacja na skali i ruch UIView (swift)
- 15. Ładowanie pliku XIB do UIView Swift
- 16. Swift UIView animateWithDuration zamknięcie zakończenie nazywa natychmiast
- 17. Przestarzałe operacje dotykowe w aplikacjach mobilnych
- 18. Prawidłowo powracający UIView w viewForHeaderInSection iOS 8 swift
- 19. Jak wysłać element UIView do tyłu w Swift
- 20. Jak Call initWithFrame z Objective C UIView w Swift
- 21. Swift: usuń warstwę, bez usuwania UIView w UITableViewCell
- 22. Jak pisać metody init sterownika UIView w Swift
- 23. UIWebView w środku UIScrollView spożywających zdarzenia dotykowe
- 24. Obsługa Gesty dotykowe w UICollectionView
- 25. Wyróżnianie UIView podobnego do UIButton
- 26. Wyświetl rozmiar ramki kontrolera w kontrolerze dziecięcym
- 27. Zdarzenia dotykowe UIScrollView podczas animacji nie uruchamianej z animacjąWithDuration: ale działa dobrze z UIView beginAnimations:
- 28. Swift, super.init() Musi zadzwonić do wyznaczonego inicjalizatora błędu nadklasy "UIView"
- 29. iOS Swift - Dynamicznie zwiększaj wysokość UITextView i rodzica UIView
- 30. Dotykowe wydarzenie z Androidem
To jest dla przycisku, PO chce dodać zdarzenie do UIView – Miknash
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