Swift,UIView触摸事件在控制器

前端之家收集整理的这篇文章主要介绍了Swift,UIView触摸事件在控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何以编程方式添加UIView touchbegin操作或touchend操作为Xcode不是从Main.storyboard提供?
你必须通过代码添加它。尝试这个:
// 1.create UIView programmetically
    var myView = UIView(frame: CGRectMake(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
    }
原文链接:/swift/320977.html

猜你在找的Swift相关文章