oc和swift UIView類擴展畫虛線外邊框
- oc類擴展
- swift類擴展
- 在swift里面呼叫類擴展
- 運行結果
oc類擴展
//
// UIView+Extension.h
// 畫虛線
//
// Created by tdw on 2020/12/2.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIView (Extension)
/**
畫外框虛線
@param width 虛線寬度
@param lenth 一段虛線的長度
@param space 虛線間距
@param cornerRadius 邊框圓角
*/
-(void) drawBoardDottedLine:(double)width lenth:(double)lenth space:(double)space cornerRadius:(double)cornerRadius color:(UIColor*)color;
@end
NS_ASSUME_NONNULL_END
@end
//
// UIView+Extension.m
// 畫虛線
//
// Created by tdw on 2020/12/2.
//
#import "UIView+Extension.h"
@implementation UIView (Extension)
-(void) drawBoardDottedLine:(double)width lenth:(double)lenth space:(double)space cornerRadius:(double)cornerRadius color:(UIColor*)color
{
self.layer.cornerRadius = cornerRadius;
CAShapeLayer *borderLayer = [CAShapeLayer layer];
borderLayer.bounds = self.bounds;
borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:cornerRadius].CGPath;
borderLayer.lineWidth = width / [[UIScreen mainScreen] scale];
//虛線邊框---小邊框的長度
borderLayer.lineDashPattern = @[@(lenth), @(space)];//前邊是虛線的長度,后邊是虛線之間空隙的長度
borderLayer.lineDashPhase = 0.1;
//實線邊框
borderLayer.fillColor = [UIColor clearColor].CGColor;
borderLayer.strokeColor = color.CGColor;
[ self . layer addSublayer :borderLayer];
}
@end
swift使用使用的時候要在橋接檔案里添加:
//畫虛線-Bridging-Header.h
#import "UIView+Extension.h"
swift類擴展
//
// UIView+Extension.swift
// 畫虛線
//
// Created by tdw on 2020/12/2.
//
import UIKit
extension UIView{
func swiftDrawBoardDottedLine(width:CGFloat,lenth:CGFloat,space:CGFloat,cornerRadius:CGFloat,color:UIColor){
self.layer.cornerRadius = cornerRadius
let borderLayer = CAShapeLayer()
borderLayer.bounds = self.bounds
borderLayer.position = CGPoint(x: self.bounds.midX, y: self.bounds.midY);
borderLayer.path = UIBezierPath(roundedRect: borderLayer.bounds, cornerRadius: cornerRadius).cgPath
borderLayer.lineWidth = width / UIScreen.main.scale
//虛線邊框---小邊框的長度
borderLayer.lineDashPattern = [lenth,space] as? [NSNumber] //前邊是虛線的長度,后邊是虛線之間空隙的長度
borderLayer.lineDashPhase = 0.1;
//實線邊框
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.strokeColor = color.cgColor
self.layer.addSublayer(borderLayer)
}
}
在swift里面呼叫類擴展
//
// ViewController.swift
// 畫虛線
//
// Created by tdw on 2020/12/2.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let v1 = UIView()
v1.frame = CGRect(x: 50, y: 50, width: 100, height: 100)
v1.backgroundColor = UIColor.yellow
//呼叫oc類擴展
v1.drawBoardDottedLine(3, lenth: 5, space: 3, cornerRadius: 10, color: UIColor.red)
view.addSubview(v1)
let v2 = UIView()
v2.frame = CGRect(x: 50, y: 200, width: 100, height: 100)
v2.backgroundColor = UIColor.lightGray
//呼叫swift類擴展
v2.swiftDrawBoardDottedLine(width: 5, lenth: 4, space: 2, cornerRadius: 4 ,color:UIColor.orange)
view.addSubview(v2)
}
}
運行結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/229898.html
標籤:其他
上一篇:Android11編譯匯入PRODUCT_BOOT_JARS
下一篇:2020年10月份面試問題記錄
