Ios UIView控制器中的触摸事件

Ios UIView控制器中的触摸事件,ios,swift,uiview,uiviewcontroller,Ios,Swift,Uiview,Uiviewcontroller,如何以编程方式添加UIView touchbegin操作或touchend操作,因为Xcode不是从Main.storyboard提供的?您必须通过代码添加它。试试这个: // 1.create UIView programmetically var myView = UIView(frame: CGRectMake(100, 100, 100, 100)) // 2.add myView to UIView hierarchy self.view.addSubv

如何以编程方式添加UIView touchbegin操作或touchend操作,因为Xcode不是从Main.storyboard提供的?

您必须通过代码添加它。试试这个:

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

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

    // update for Swift UI

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

将其放在
UIView
子类中(如果为该功能创建一个子类,则最简单)

class-YourView:UIView{
//在这里定义初始值设定项
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
如果让触摸=触摸。首先作为?UITouch{
让currentPoint=touch.locationInView(自)
//用你现在的观点做点什么
}
}
覆盖功能触摸移动(触摸:设置,withEvent事件:UIEvent){
如果让触摸=触摸。首先作为?UITouch{
让currentPoint=touch.locationInView(自)
//用你现在的观点做点什么
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent){
如果让触摸=触摸。首先作为?UITouch{
让currentPoint=touch.locationInView(自)
//用你现在的观点做点什么
}
}
}

更新@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
    }
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
如果让触摸=先触摸{
让currentPoint=touch.locationInView(自)
//用你现在的观点做点什么
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
如果让触摸=先触摸{
让currentPoint=touch.locationInView(自)
//用你现在的观点做点什么
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
如果让触摸=先触摸{
让currentPoint=touch.locationInView(自)
//用你现在的观点做点什么
}
}

以上答案的更新:

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

@objc func checkAction(sender : UITapGestureRecognizer) {
    // Do what you want
}
let gesture = UITapGestureRecognizer(target: self, action:  #selector(self.checkAction(sender:)))
self.myView.addGestureRecognizer(gesture)

func checkAction(sender : UITapGestureRecognizer) {
    // Do what you want
}
如果您希望在单击事件中看到更改,即每当用户单击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

更新@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
    }
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让currentPoint=触摸位置(in:self)
//用你现在的观点做点什么
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让currentPoint=触摸位置(in:self)
//用你现在的观点做点什么
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让currentPoint=触摸位置(in:self)
//用你现在的观点做点什么
}
}

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
}
let gesture = UITapGestureRecognizer(target: self, action:  #selector(self.checkAction(sender:)))
self.myView.addGestureRecognizer(gesture)

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

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

@objc func checkAction(sender : UITapGestureRecognizer) {
    // Do what you want
}
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
    }
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让currentPoint=touch.location(在:self.view中)
//用你现在的观点做点什么
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让currentPoint=touch.location(在:self.view中)
//用你现在的观点做点什么
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让currentPoint=touch.location(在:self.view中)
//用你现在的观点做点什么
}
}
您可以这样使用: 创建扩展名

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
 }

从在情节提要中创建的视图创建插座

@IBOutlet weak var redView: UIView!
@IBOutlet weak var orangeView: UIView!
@IBOutlet weak var greenView: UIView!   
重写touchesbeent方法。有两个选择,每个人都可以决定哪一个更适合他

  • 在特殊视图中检测触摸

    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
            }
        }
    
    }
    
    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()
            }
        }
    
    }
    

    这一个用于按钮,OP希望为UIView添加事件使用
    UILongPressGestureRecognitor
    ,并将
    minimumPressDuration
    设置为零。它不需要子类化或重写任何内容。@Chacle,我的页面中有10多个Uiview,我想在一些子Uiview中添加操作。因此,我应该改变什么?这取决于你想用它做什么。您想告诉我您按下了哪个
    UIView
    ,还是想在每个
    UIView
    中处理一些按下操作?我只想要某些特定UIView的touchevent。我已经这样做了,当我点击UIView时,它给了我一个错误。我的代码:让手势=UITapGestureRecognitizer(目标:self.uiPendingView,操作:“touchPending”)self.uiPendingView.AddGestureRecognitizer(手势)和方法:func touchPending(发送方:AnyObject){println(“方法>>>>>>>>>>>>”)只需在“touchPending:”中添加:即可:ans在函数中它看起来像func touchPending(发送方:UITapgestureRecognitizer),您在操作中缺少“:”。您好,当它们都具有相同的someAction函数时,我如何区分单击了哪个UIView?最简单的方法是使用标记属性,然后在函数中确定哪个视图是SendServer。非常好的示例,感谢向我展示!!我们也可以使用touchEvent来实现这一点。。我只知道双向按钮和手势。。谢谢+1顺便说一句,这个触摸在模拟器中不起作用。因此,如果您正在使用模拟器,则无法测试代码。应从VIEWDIDLOAD调用前2行!