Swift - 繪製背景線條

来源:http://www.cnblogs.com/YouXianMing/archive/2016/08/14/5769947.html
-Advertisement-
Play Games

Swift - 繪製背景線條 效果 源碼 使用 ...


Swift - 繪製背景線條

 

效果

 

源碼

//
//  BackgroundLineView.swift
//  LineBackgroundView
//
//  Created by YouXianMing on 16/8/14.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

import UIKit

// MARK: Public class : BackgroundLineView

class BackgroundLineView: UIView {

    // MARK: Properties.
    
    /// Line width, default is 5.
    var lineWidth : CGFloat {
    
        get {return backgroundView.lineWidth}
        
        set(newVal) {
        
            backgroundView.lineWidth = newVal
            backgroundView.setNeedsDisplay()
        }
    }
    
    /// Line gap, default is 3.
    var lineGap : CGFloat {
        
        get {return backgroundView.lineGap}
        
        set(newVal) {
            
            backgroundView.lineGap = newVal
            backgroundView.setNeedsDisplay()
        }
    }
    
    /// Line color, default is grayColor.
    var lineColor : UIColor {
        
        get {return backgroundView.lineColor}
        
        set(newVal) {
            
            backgroundView.lineColor = newVal
            backgroundView.setNeedsDisplay()
        }
    }
    
    /// Rotate value, default is 0.
    var rotate : CGFloat {
    
        get {return backgroundView.rotate}
        
        set(newVal) {
        
            backgroundView.rotate = newVal
            backgroundView.setNeedsDisplay()
        }
    }
    
    convenience init(frame: CGRect, lineWidth : CGFloat, lineGap : CGFloat, lineColor : UIColor, rotate : CGFloat) {
        
        self.init(frame : frame)
        self.lineWidth = lineWidth
        self.lineGap   = lineGap
        self.lineColor = lineColor
        self.rotate    = rotate
    }
    
    // MARK: Override system method.
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        setupBackgroundView()
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.layer.masksToBounds = true
        self.addSubview(backgroundView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: Private value & func.
    
    private let backgroundView = LineBackground(length: 0)
    
    private func setupBackgroundView() {
    
        let drawLength        = sqrt(self.bounds.size.width * self.bounds.size.width + self.bounds.size.height * self.bounds.size.height)
        backgroundView.frame  = CGRectMake(0, 0, drawLength, drawLength)
        backgroundView.center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0)
        backgroundView.setNeedsDisplay()
    }
}

// MARK: Private class : LineBackground

private class LineBackground : UIView {

    private var rotate    : CGFloat = 0
    private var lineWidth : CGFloat = 5
    private var lineGap   : CGFloat = 3
    private var lineColor : UIColor = UIColor.grayColor()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        self.backgroundColor = UIColor.clearColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(length : CGFloat) {
        
        self.init(frame : CGRectMake(0, 0, length, length))
    }
    
    override func drawRect(rect: CGRect) {
        
        super.drawRect(rect)
        
        if self.bounds.size.width <= 0 || self.bounds.size.height <= 0 {
            
            return
        }
        
        let context      = UIGraphicsGetCurrentContext()
        let width        = self.bounds.size.width
        let height       = self.bounds.size.width
        let drawLength   = sqrt(width * width + height * height)
        let outerX       = (drawLength - width)  / 2.0
        let outerY       = (drawLength - height) / 2.0
        let tmpLineWidth = lineWidth <= 0 ? 5 : lineWidth
        let tmpLineGap   = lineGap   <= 0 ? 3 : lineGap
        
        var red   : CGFloat = 0
        var green : CGFloat = 0
        var blue  : CGFloat = 0
        var alpha : CGFloat = 0
        
        CGContextTranslateCTM(context, 0.5 * drawLength, 0.5 * drawLength)
        CGContextRotateCTM(context, rotate)
        CGContextTranslateCTM(context, -0.5 * drawLength, -0.5 * drawLength)
        
        lineColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        CGContextSetRGBFillColor(context, red, green, blue, alpha)
        
        var currentX = -outerX
        
        while currentX < drawLength {
            
            CGContextAddRect(context, CGRectMake(currentX, -outerY, tmpLineWidth, drawLength))
            currentX += tmpLineWidth + tmpLineGap
        }
        
        CGContextFillPath(context)
    }
}

 

使用

//
//  ViewController.swift
//  LineBackgroundView
//
//  Created by YouXianMing on 16/8/14.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    let tmpView = BackgroundLineView(frame: CGRectMake(0, 0, 300, 300), lineWidth: 4, lineGap: 4,
                                     lineColor: UIColor.blackColor().colorWithAlphaComponent(0.045), rotate: CGFloat(M_PI_4))
    
    override func viewDidLoad() {
        
        super.viewDidLoad()

        tmpView.center = self.view.center
        self.view.addSubview(tmpView)
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ...
  • 今天在學習《jQuery基礎教程》在學習編寫插件的時候,書中說利用回調函數來當參數,會極大的提高程式的靈活性。對回調函數很陌生。研究了一下給的示常式序。感覺對回調函數有了基本的瞭解。記錄下來,以備後用。 ...
  • Range對象基本用法 效果圖如下(在谷歌瀏覽器下的展示) 代碼如下 在火狐瀏覽器下的展示 為什麼會出現這種情況呢,因為火狐瀏覽器下可以針對不連續的內容多選. Range對象之SelectNode和selectNodeContents方法 效果圖如下 代碼內容如下: selectNodeConten ...
  • 自定義屬性 繪製 源碼: https://github.com/ln0491/ProgressDemo ...
  • Swift - RotateView 效果 源碼 https://github.com/YouXianMing/Swift-Animations ...
  • 介面 ...
  • 一.點擊更多按鈕 1.項目需求 點擊更多按鈕,從底部彈出一個框 2.怎麼從底部彈出一個框? 兩種方法: 一種用 UIActionSheet 另一種用 UIAlertController 2.1 使用UIActionSheet 好處: 不需要控制器,直接就能使用 缺點: 方法過期了,監聽按鈕的點擊比較 ...
  • 安卓開發過程中難免會碰到需要選擇日期時間的情況,由於大部分android初級教程都沒教怎麼選擇時間,初學者碰到這種難免會有些不知所措,難道要讓用戶自己輸入日期時間?先不說用戶體驗不好,處理用戶輸入各式各樣的日期格式也要花好大一番功夫。 所以當然不可能讓用戶自己輸入日期時間,筆者收集整理了一些資料,總 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...