控制器中的 UIView 触摸事件

我如何能够添加 UIView 触摸开始动作或触摸结束动作编程,因为 Xcode 没有从 Main.storboard 提供?

192390 次浏览

首先,创建视图并将其添加到层次结构中:

var myView = UIView(frame: CGRectMake(100, 100, 100, 100))
self.view.addSubview(myView)

After that initialize gesture recognizer. Until Swift 2:

let gesture = UITapGestureRecognizer(target: self, action: "someAction:")

雨燕2之后:

let gesture = UITapGestureRecognizer(target: self, action:  #selector (self.someAction (_:)))

然后将其与视图绑定:

self.myView.addGestureRecognizer(gesture)


    

斯威夫特3:

func someAction(_ sender:UITapGestureRecognizer){
// do other task
}
    

Swift 4在 func之前加上 @objc:

@objc func someAction(_ sender:UITapGestureRecognizer){
// do other task
}

快速用户界面:

Text("Tap me!").tapAction {
print("Tapped!")
}

把它放在 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
}
}
}

更新@Chackle 对 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
}
}

以下是对以上答案的更新:

如果你想在点击事件中看到变化,例如,当用户点击 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

此外,从 Storyboard & ViewController 调用这个类为:

@IBOutlet weak var panVerificationUIView:ClickableUIView!

更新@Crashalot 对 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
}
}

斯威夫特4/5:

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


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

斯威夫特3:

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


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

为了迅捷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")
}

更新@stevo. mit 对 Swift 4. x 的回答:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: self.view)
// 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.view)
// 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.view)
// do something with your currentPoint
}
}

你可以这样使用; 创建一个扩展。

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{
// ...
}
    

迅捷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
}

从 StoryBoard 中创建的视图中创建出口。

@IBOutlet weak var redView: UIView!
@IBOutlet weak var orangeView: UIView!
@IBOutlet weak var greenView: UIView!

Override the touchesBegan method. There are 2 options, everyone can determine which one is better for him.

  1. 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
    }
    }
    
    
    }
    
  2. 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()
    }
    }
    
    
    }
    

Lastly, you need to declare the functions that will be called, depending on which view the user clicked.

func redViewTapped() {
print("redViewTapped")
}


func orangeViewTapped() {
print("orangeViewTapped")
}


func greenViewTapped() {
print("greenViewTapped")
}