加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift,UIView触摸事件在控制器

发布时间:2020-12-14 06:03:05 所属栏目:百科 来源:网络整理
导读:如何以编程方式添加UIView touchbegin操作或touchend操作为Xcode不是从Main.storyboard提供? 你必须通过代码添加它。尝试这个: // 1.create UIView programmetically var myView = UIView(frame: CGRectMake(100,100,100)) // 2.add myView to UIView hier
如何以编程方式添加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
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读