GithubHelp home page GithubHelp logo

uiviewroundcorner's Introduction

UIViewRoundCorner

一个非常轻量级的扩展,给UIView类型添加圆角,性能良好

原理:

在UIView的最上层绘制一张相应的遮罩图片,图片的背景只要保证和view的父视图背景色一样,就能达到圆角的效果

  • 带有border的圆角
  • 可设置圆角位置[.topLeft,.topRight]
  • 可给矩形设置圆角
  • 可动态修改圆角颜色
  • 使用NSCache,对生成的图片进行了缓存,避免重复生成
  • API非常简洁
  • 基本不掉帧,6s真机测试非常流畅

API

///  设置一个带边框的圆角
///
/// - Parameters:
///   - radius: 圆角半径
///   - cornerColor: 圆角背景色
///   - corners: 圆角位置
///   - borderColor: 边框颜色
///   - borderWidth: 边框线宽
func dx_roundCorner(radii: CGSize,cornerColor: UIColor, corners: UIRectCorner, borderColor: UIColor, borderWidth: CGFloat)  {
    self.layer.dx_roundCorner(cornerRadii: radii, cornerColor: cornerColor, corners: corners, borderColor: borderColor, borderWidth: borderWidth)
}

使用示例

// 注意:非正方形,四角圆形需要用[.topLeft,.topRight, .bottomRight, .bottomLeft]
// 而不是用UIRectCorner.allCorners
label.dx_roundCorner(radius: 20/2, cornerColor: UIColor.white,corners: [.topLeft,.topRight, .bottomRight, .bottomLeft])

// 给UIView
headView.dx_roundCorner(radii: CGSize.init(width: 35, height: 35), cornerColor: UIColor.white, corners: .allCorners, borderColor: UIColor.red, borderWidth: 2)

注意:

  • 1.非正方形,四角圆形需要用[.topLeft,.topRight, .bottomRight, .bottomLeft]
  • 2.如果使用了nib文件,圆角无效,需要设置layer.masksToBounds = true
  • 3.如果需要显示tableView的选中状态,需要自己处理setSelectedsetHighlighted方法,可以参考demo建议使用cell.selectionStyle = .none

效果图

声明: 本文参考了wazrxiOS一个简单的设置圆角不引起性能问题的分类Objective-C版本,并在原文上进行了改进,原文使用了运行时给分类扩展属性进行的保存XWCornerRadius

uiviewroundcorner's People

Contributors

dongxiexidu avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.