栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > iOS

Swift 2.1 为 UIView 添加点击事件和点击效果

iOS 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Swift 2.1 为 UIView 添加点击事件和点击效果

前言

  UIView 不像 UIButton 加了点击事件就会有点击效果,体验要差不少,这里分别通过自定义和扩展来实现类似 UIButton 的效果。

正文

  一、为 UIView 添加点击事件

extension UIView {

  func addonClickListener(target: AnyObject, action: Selector) {
    let gr = UITapGestureRecognizer(target: target, action: action)
    gr.numberOfTapsRequired = 1
    userInteractionEnabled = true
    addGestureRecognizer(gr)
  }

}

        二、为 UIView 添加点击效果

class UIViewEffect : UIView {

  override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
    backgroundColor = UIColor.groupTableViewBackgroundColor()
  }

  override func touchesCancelled(touches: Set?, withEvent event: UIEvent?) {
    UIView.animateWithDuration(0.15, animations: { () -> Void in
      self.backgroundColor = UIColor.clearColor()
    })
  }

  override func touchesEnded(touches: Set, withEvent event: UIEvent?) {
    UIView.animateWithDuration(0.15, animations: { () -> Void in
      self.backgroundColor = UIColor.clearColor()
    })
  }
}

 这里大家可以换成自己的点击效果,如果是 UIImageView 可以换成点击变更透明度。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/167270.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号