iOS开发 - 画线 方便自定义细线

某些时候我们需要一些细节调整,可以自己自定义线,下面是画直线的例子。同时提供 View 转 Image 方法,方便某些时候直接当图像使用。
/// 自定义线视图
class LineView: UIView {
    
    /// 快速创建 默认全屏  height 高度
    static func creatLineView(height height:CGFloat) -> LineView {
        let lineView = LineView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: height))
        lineView.backgroundColor = UIColor.clearColor()
        return lineView
    }
    
    /// 画直线
    override func drawRect(rect: CGRect) {
        
        let context = UIGraphicsGetCurrentContext()
        CGContextSetAllowsAntialiasing(context, true)
        CGContextSetLineWidth(context, self.frame.height)
        
        CGContextSetRGBStrokeColor(context, 136/255, 136/255, 136/255, 1)// 颜色
        CGContextBeginPath(context)
        
        CGContextMoveToPoint(context, 0, 0);  //起点坐标
        CGContextAddLineToPoint(context, self.frame.size.width, 0) //终点坐标
        
        CGContextStrokePath(context)
    }
}

extension LineView {
    /// View -> UIImage
    func convertViewToImage() -> UIImage {
        let size = self.bounds.size
        //UIGraphicsBeginImageContext(size)
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale)
        self.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,268评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,668评论 4 61
  • 二姐在朋友圈里写下一段话,很是吸引我。于是,用了两天时间我看完了这部《荼蘼》。 《荼蘼》,好有诚意的剧,在这6集里...
    周小葵OnRoad阅读 3,853评论 2 2
  • 现在人们对学历的要求越来越高,读了研究生读博士,读完博士还有博士后……事实上学历真的能代表一切么?我们读书究竟是为...
    孤单的温柔阅读 3,290评论 0 0
  • 还记得自己三岁的时候在干什么吗,玩泥巴,耍玩具,搞破坏,还是黏在妈妈怀里?昨天成就了法网十冠王的纳达尔,三岁就在玩...
    百万心生活阅读 3,199评论 0 0

友情链接更多精彩内容