我该如何在编程中添加UIView的touchbegin动作或touchend动作,因为Xcode没有从Main.storyboard提供此功能?
我该如何在编程中添加UIView的touchbegin动作或touchend动作,因为Xcode没有从Main.storyboard提供此功能?
你需要通过代码来添加它。首先,创建视图并将其添加到层次结构中:
var myView = UIView(frame: CGRectMake(100, 100, 100, 100))
self.view.addSubview(myView)
之后初始化手势识别器。直到Swift 2:
let gesture = UITapGestureRecognizer(target: self, action: "someAction:")
在 Swift 2 之后:
let gesture = UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:)))
然后将其绑定到视图中:self.myView.addGestureRecognizer(gesture)
Swift 3:
func someAction(_ sender:UITapGestureRecognizer){
// do other task
}
在Swift 4中,只需在func
之前添加@objc
:
@objc func someAction(_ sender:UITapGestureRecognizer){
// do other task
}
Swift UI:
Text("Tap me!").tapAction {
print("Tapped!")
}
Swift 4 / 5:
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction))
self.myView.addGestureRecognizer(gesture)
@objc func checkAction(sender : UITapGestureRecognizer) {
// Do what you want
}
Swift 3:
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction(sender:)))
self.myView.addGestureRecognizer(gesture)
func checkAction(sender : UITapGestureRecognizer) {
// Do what you want
}
为Swift 3.x更新@Crashalot的答案:
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
}
}
为了适应Swift 2.x,更新@Chackle的答案:
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
}
}
将以下代码放入你的UIView
子类中(如果你为此功能创建一个子类,会更加容易)。
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
}
}
}
UIView
被按下了,还是想在每个 UIView
中处理一些按下事件? - Chackle针对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")
}
从在StoryBoard中创建的视图中创建出口。
@IBOutlet weak var redView: UIView!
@IBOutlet weak var orangeView: UIView!
@IBOutlet weak var greenView: UIView!
重写touchesBegan方法。有两个选项,每个人都可以确定哪一个更适合他。
Detect touch in special view.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if touch.view == self.redView {
tapOnredViewTapped()
} else if touch.view == self.orangeView {
orangeViewTapped()
} else if touch.view == self.greenView {
greenViewTapped()
} else {
return
}
}
}
Detect touch point on special view.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: view)
if redView.frame.contains(location) {
redViewTapped()
} else if orangeView.frame.contains(location) {
orangeViewTapped()
} else if greenView.frame.contains(location) {
greenViewTapped()
}
}
}
func redViewTapped() {
print("redViewTapped")
}
func orangeViewTapped() {
print("orangeViewTapped")
}
func greenViewTapped() {
print("greenViewTapped")
}
extension UIView {
func addTapGesture(action : @escaping ()->Void ){
let tap = MyTapGestureRecognizer(target: self , action: #selector(self.handleTap(_:)))
tap.action = action
tap.numberOfTapsRequired = 1
self.addGestureRecognizer(tap)
self.isUserInteractionEnabled = true
}
@objc func handleTap(_ sender: MyTapGestureRecognizer) {
sender.action!()
}
}
class MyTapGestureRecognizer: UITapGestureRecognizer {
var action : (()->Void)? = nil
}
并采用以下方式:
@IBOutlet weak var testView: UIView!
testView.addTapGesture{
// ...
}
Swift 4.2:
@IBOutlet weak var viewLabel1: UIView!
@IBOutlet weak var viewLabel2: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let myView = UITapGestureRecognizer(target: self, action: #selector(someAction(_:)))
self.viewLabel1.addGestureRecognizer(myView)
}
@objc func someAction(_ sender:UITapGestureRecognizer){
viewLabel2.isHidden = true
}
关于上面回答的更新:
如果你希望在点击事件中看到变化,即每当用户点击UIView时,UIView的颜色应更改,则请按照以下方式进行更改...
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
@IBOutlet weak var panVerificationUIView:ClickableUIView!
UILongPressGestureRecognizer
,并将minimumPressDuration
设置为零。请参见此答案。 不需要子类化或覆盖任何内容。 - Suragch