2015-05-28 14 views
41

Làm cách nào để thêm hành động touchbegin của UIView hoặc hành động chạm vào chương trình khi Xcode không cung cấp từ Main.storyboard?Swift, UIView sự kiện cảm ứng trong bộ điều khiển

+0

Đó là một là dành cho nút, OP muốn thêm sự kiện cho UIView – Miknash

+0

Sử dụng một 'UILongPressGestureRecognizer' với' minimumPressDuration' thiết lập để không. [Xem câu trả lời này.] (Http://stackoverflow.com/a/38143429/3681880) Nó không yêu cầu phân lớp hoặc ghi đè bất cứ điều gì. – Suragch

Trả lời

84

Bạn sẽ phải thêm mã thông qua mã. Hãy thử điều này:

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

Tôi đã làm điều đó cho tôi một lỗi khi tôi nhấp vào uiView. Mã của tôi: hãy cử chỉ = UITapGestureRecognizer (mục tiêu: self.uiPendingView, hành động: "touchPending") self.uiPendingView.addGestureRecognizer (gesture) và phương pháp: func touchPending (sender: AnyObject) { println ("PHƯƠNG PHÁP >> >>>>>>>>>>>>>>> ") } –

+1

chỉ cần thêm: trong" touchPending: "ans trong chức năng nó trông giống như func touchPending (người gửi: UITapGestureRecognizer) –

+1

bạn đang thiếu ':' trong hành động . – Miknash

6

Đặt mã này vào lớp con UIView của bạn (dễ nhất là nếu bạn tạo một sublcass cho chức năng này).

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, tôi có hơn 10 Uiview trong trang của tôi, và tôi muốn thêm hành động trong một số Sub UIView. Vì vậy, cho rằng những gì tôi nên thay đổi? –

+0

Nó phụ thuộc vào những gì bạn muốn sử dụng nó cho. Bạn có muốn nói 'UIView' nào bạn nhấn hay bạn muốn xử lý một số lần nhấn trong mỗi' UIView'? – Chackle

+0

Tôi chỉ muốn có một số giao diện người dùng đặc biệt. –

15

Đang cập nhật @ câu trả lời Chackle cho 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

Chỉ cần một cập nhật cho câu trả lời ở trên:

Nếu bạn muốn có thấy những thay đổi trong sự kiện click, tức là Màu sắc của bạn UIVIew shud thay đổi bất cứ khi nào người dùng nhấp vào UIView sau đó thực hiện thay đổi như dưới đây ...

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 

Ngoài ra, gọi lớp này khỏi Cốt truyện & ViewController như:

@IBOutlet weak var panVerificationUIView:ClickableUIView! 
14

Đang cập nhật @ câu trả lời Crashalot cho 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

Cập nhật cho Swift 4:

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

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

Cập nhật cho 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

Đối nhanh chóng 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") 
} 
Các vấn đề liên quan