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
Trả lời
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
}
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 >> >>>>>>>>>>>>>>> ") } –
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) –
bạn đang thiếu ':' trong hành động . – Miknash
Đặ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
}
}
}
@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? –
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
Tôi chỉ muốn có một số giao diện người dùng đặc biệt. –
Đ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
}
}
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!
Đ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
}
}
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
}
Đố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")
}
- 1. MKPointAnnotations sự kiện cảm ứng trong swift
- 2. Sự kiện điều khiển từ xa trong iOS với Swift
- 3. sự kiện cảm ứng trên webbrowser awesomium
- 4. Sử dụng trình kiểm tra sự kiện rỗng trong bộ điều khiển sự kiện
- 5. (UIView *) hitTest trong bộ điều khiển chế độ xem?
- 6. Nhiều sự kiện MouseHover trong Điều khiển
- 7. jquery chuyển bộ điều khiển sự kiện "bật" và "tắt"
- 8. Swift, iboutlet và điều khiển tùy chỉnh
- 9. Truyền dữ liệu từ Bộ điều khiển Xem tới Bộ điều khiển Con trong Swift
- 10. Swift NSViewController đáp ứng với sự kiện mouseDown, nhưng không phải sự kiện KeyDown
- 11. Liên kết sự kiện cảm ứng Android WebView
- 12. Android: ủy sự kiện cảm ứng để xem underlaying
- 13. Làm thế nào để mô phỏng sự kiện cảm ứng?
- 14. Làm cách nào để kích hoạt sự kiện cảm ứng?
- 15. Force UIView/UIVIewChuyển hướng điều khiển
- 16. gửi sự kiện cảm ứng từ ADB tới thiết bị
- 17. Làm thế nào để mô phỏng các sự kiện cảm ứng trong Windows 7?
- 18. Mô phỏng sự kiện cảm ứng rộng trên hệ điều hành iOS
- 19. Bảng điều khiển bên trong Swift iPad
- 20. Cách phát hiện sự kiện kết thúc cảm ứng cho UIButton?
- 21. Hành vi xử lý cảm ứng UIView đã thay đổi với Xcode 4.2?
- 22. UIView isKindOfClass trong Swift 3
- 23. Màu nền UIView trong Swift
- 24. Làm thế nào để vượt qua sự kiện cảm ứng để giám sát khi userInteractionEnabled = YES?
- 25. Thêm sự kiện cho trước/sau một hành động của một bộ điều khiển trong Magento
- 26. C# Thực hành tốt nhất: Bộ điều khiển sự kiện tập trung hoặc không
- 27. Truyền các sự kiện cảm ứng cho anh chị em thích hợp UIViews
- 28. Sự kiện kiểm tra đơn vị được điều khiển javascript
- 29. Làm thế nào để có được sự kiện cảm ứng trên một CALayer?
- 30. Sự kiện/Quan sát Điều khiển Ruby on Rails
Đó là một là dành cho nút, OP muốn thêm sự kiện cho UIView – Miknash
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