如何在swift中创建虚线?

gt0wga4j  于 2024-01-05  发布在  Swift
关注(0)|答案(8)|浏览(155)

我想知道如何在swift中创建一条虚线,就像这样:-而不是像这样的常规直线:--,我知道我可以创建多行,但如果我只能用一行来写,那将需要很多不必要的代码。

plupiseo

plupiseo1#

  • 斯威夫特4号酒店,纽约**
  1. @IBOutlet var dashedView: UIView!
  2. func drawDottedLine(start p0: CGPoint, end p1: CGPoint, view: UIView) {
  3. let shapeLayer = CAShapeLayer()
  4. shapeLayer.strokeColor = UIColor.lightGray.cgColor
  5. shapeLayer.lineWidth = 1
  6. shapeLayer.lineDashPattern = [7, 3] // 7 is the length of dash, 3 is length of the gap.
  7. let path = CGMutablePath()
  8. path.addLines(between: [p0, p1])
  9. shapeLayer.path = path
  10. view.layer.addSublayer(shapeLayer)
  11. }

字符串

调用函数

  1. drawDottedLine(start: CGPoint(x: dashedView.bounds.minX, y: dashedView.bounds.minY), end: CGPoint(x: dashedView.bounds.maxX, y: dashedView.bounds.minY), view: dashedView)


有了上面的你将有一条直线,你也可以改变点,因为你想,例如,如果你改变端点的y从dashedView.bounds.minYdashedView.bounds.maxY,你将有对角线。
如果你将在UIView的子类中使用它,你将没有出口,所以你将使用它与self代替。

展开查看全部
stszievb

stszievb2#

创建虚线的方法与使用Swift的方法相同,不同的是使用Swift。
以下是使用UIBezierPath的方法:

  1. let path = UIBezierPath()
  2. let p0 = CGPoint(x: self.bounds.minX, y: self.bounds.midY)
  3. path.move(to: p0)
  4. let p1 = CGPoint(x: self.bounds.maxX, y: self.bounds.midY)
  5. path.addLine(to: p1)
  6. let dashes: [ CGFloat ] = [ 16.0, 32.0 ]
  7. path.setLineDash(dashes, count: dashes.count, phase: 0.0)
  8. path.lineWidth = 8.0
  9. path.lineCapStyle = .butt
  10. UIColor.magenta.set()
  11. path.stroke()

字符串
以下是如何使用UIBezierPath绘制虚线:

  1. let path = UIBezierPath()
  2. let p0 = CGPointMake(CGRectGetMinX(self.bounds), CGRectGetMidY(self.bounds))
  3. path.moveToPoint(p0)
  4. let p1 = CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMidY(self.bounds))
  5. path.addLineToPoint(p1)
  6. let dashes: [ CGFloat ] = [ 0.0, 16.0 ]
  7. path.setLineDash(dashes, count: dashes.count, phase: 0.0)
  8. path.lineWidth = 8.0
  9. path.lineCapStyle = .Round
  10. UIColor.magentaColor().set()
  11. path.stroke()


以下是如何使用CGContext绘制虚线:

  1. let context: CGContext = UIGraphicsGetCurrentContext()!
  2. let p0 = CGPointMake(CGRectGetMinX(self.bounds), CGRectGetMidY(self.bounds))
  3. CGContextMoveToPoint(context, p0.x, p0.y)
  4. let p1 = CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMidY(self.bounds))
  5. CGContextAddLineToPoint(context, p1.x, p1.y)
  6. let dashes: [ CGFloat ] = [ 16.0, 32.0 ]
  7. CGContextSetLineDash(context, 0.0, dashes, dashes.count)
  8. CGContextSetLineWidth(context, 8.0)
  9. CGContextSetLineCap(context, .Butt)
  10. UIColor.blueColor().set()
  11. CGContextStrokePath(context)

展开查看全部
xwbd5t1u

xwbd5t1u3#

通过使用继承自UIView的自定义类,也支持故事板。

你所需要做的就是在故事板中创建一个视图,为这个视图分配类,然后看看故事板中的魔力。

  1. @IBDesignable
  2. class DashedLineView : UIView {
  3. @IBInspectable var perDashLength: CGFloat = 2.0
  4. @IBInspectable var spaceBetweenDash: CGFloat = 2.0
  5. @IBInspectable var dashColor: UIColor = UIColor.lightGray
  6. override func draw(_ rect: CGRect) {
  7. super.draw(rect)
  8. let path = UIBezierPath()
  9. if height > width {
  10. let p0 = CGPoint(x: self.bounds.midX, y: self.bounds.minY)
  11. path.move(to: p0)
  12. let p1 = CGPoint(x: self.bounds.midX, y: self.bounds.maxY)
  13. path.addLine(to: p1)
  14. path.lineWidth = width
  15. } else {
  16. let p0 = CGPoint(x: self.bounds.minX, y: self.bounds.midY)
  17. path.move(to: p0)
  18. let p1 = CGPoint(x: self.bounds.maxX, y: self.bounds.midY)
  19. path.addLine(to: p1)
  20. path.lineWidth = height
  21. }
  22. let dashes: [ CGFloat ] = [ perDashLength, spaceBetweenDash ]
  23. path.setLineDash(dashes, count: dashes.count, phase: 0.0)
  24. path.lineCapStyle = .butt
  25. dashColor.set()
  26. path.stroke()
  27. }
  28. private var width : CGFloat {
  29. return self.bounds.width
  30. }
  31. private var height : CGFloat {
  32. return self.bounds.height
  33. }
  34. }

字符串

展开查看全部
fbcarpbf

fbcarpbf4#

这里有一个易于使用的UIView,它画了一条虚线。
我接受了@Fan Jin的回答,并创建了一个UIView子类,它应该可以很好地与Auto Layout一起工作。

Swift 5.3,Xcode 12

  1. import UIKit
  2. public class DashedView: UIView {
  3. public struct Configuration {
  4. public var color: UIColor
  5. public var dashLength: CGFloat
  6. public var dashGap: CGFloat
  7. public init(
  8. color: UIColor,
  9. dashLength: CGFloat,
  10. dashGap: CGFloat) {
  11. self.color = color
  12. self.dashLength = dashLength
  13. self.dashGap = dashGap
  14. }
  15. static let `default`: Self = .init(
  16. color: .lightGray,
  17. dashLength: 7,
  18. dashGap: 3)
  19. }
  20. // MARK: - Properties
  21. /// Override to customize height
  22. public class var lineHeight: CGFloat { 1.0 }
  23. override public var intrinsicContentSize: CGSize {
  24. CGSize(width: UIView.noIntrinsicMetric, height: Self.lineHeight)
  25. }
  26. public final var config: Configuration = .default {
  27. didSet {
  28. drawDottedLine()
  29. }
  30. }
  31. private var dashedLayer: CAShapeLayer?
  32. // MARK: - Life Cycle
  33. override public func layoutSubviews() {
  34. super.layoutSubviews()
  35. // We only redraw the dashes if the width has changed.
  36. guard bounds.width != dashedLayer?.frame.width else { return }
  37. drawDottedLine()
  38. }
  39. // MARK: - Drawing
  40. private func drawDottedLine() {
  41. if dashedLayer != nil {
  42. dashedLayer?.removeFromSuperlayer()
  43. }
  44. dashedLayer = drawDottedLine(
  45. start: bounds.origin,
  46. end: CGPoint(x: bounds.width, y: bounds.origin.y),
  47. config: config)
  48. }
  49. }
  50. // Thanks to: https://stackoverflow.com/a/49305154/4802021
  51. private extension DashedView {
  52. func drawDottedLine(
  53. start: CGPoint,
  54. end: CGPoint,
  55. config: Configuration) -> CAShapeLayer {
  56. let shapeLayer = CAShapeLayer()
  57. shapeLayer.strokeColor = config.color.cgColor
  58. shapeLayer.lineWidth = Self.lineHeight
  59. shapeLayer.lineDashPattern = [config.dashLength as NSNumber, config.dashGap as NSNumber]
  60. let path = CGMutablePath()
  61. path.addLines(between: [start, end])
  62. shapeLayer.path = path
  63. layer.addSublayer(shapeLayer)
  64. return shapeLayer
  65. }
  66. }

字符串

展开查看全部
hwazgwia

hwazgwia5#

我的扩展方法是从@FanJins的答案构建的

  1. extension UIView {
  2. func createDashedLine(from point1: CGPoint, to point2: CGPoint, color: UIColor, strokeLength: NSNumber, gapLength: NSNumber, width: CGFloat) {
  3. let shapeLayer = CAShapeLayer()
  4. shapeLayer.strokeColor = color.cgColor
  5. shapeLayer.lineWidth = width
  6. shapeLayer.lineDashPattern = [strokeLength, gapLength]
  7. let path = CGMutablePath()
  8. path.addLines(between: [point1, point2])
  9. shapeLayer.path = path
  10. layer.addSublayer(shapeLayer)
  11. }
  12. }

字符串
然后调用方法看起来像这样:

  1. let topPoint = CGPoint(x: view.frame.midX, y: view.bounds.minY)
  2. let bottomPoint = CGPoint(x: view.frame.midX, y: view.bounds.maxY)
  3. view.createDashedLine(from: topPoint, to: bottomPoint, color: .black, strokeLength: 4, gapLength: 6, width: 2)

展开查看全部
i34xakig

i34xakig6#

非常简单的SWIFT 4.2UIView扩展:

  1. extension UIView {
  2. private static let lineDashPattern: [NSNumber] = [2, 2]
  3. private static let lineDashWidth: CGFloat = 1.0
  4. func makeDashedBorderLine() {
  5. let path = CGMutablePath()
  6. let shapeLayer = CAShapeLayer()
  7. shapeLayer.lineWidth = UIView.lineDashWidth
  8. shapeLayer.strokeColor = UIColor.lightGray.cgColor
  9. shapeLayer.lineDashPattern = UIView.lineDashPattern
  10. path.addLines(between: [CGPoint(x: bounds.minX, y: bounds.height/2),
  11. CGPoint(x: bounds.maxX, y: bounds.height/2)])
  12. shapeLayer.path = path
  13. layer.addSublayer(shapeLayer)
  14. }
  15. }

字符串

展开查看全部
4c8rllxm

4c8rllxm7#

目标C**

  • @user3230875* 的回答帮助我理解了画虚线需要什么。

所以我希望这个答案可以帮助Obj-C搜索者

  1. //dashed line
  2. path = [UIBezierPath bezierPath];
  3. [path moveToPoint:CGPointMake(dashedLineStartX, dashedLineStartY)];
  4. [path addLineToPoint:CGPointMake(dashedLineEndX, dashedLineEndY)];
  5. path.lineWidth = 5;
  6. [color setStroke];
  7. CGFloat dashes[] = {4.0,8.0};
  8. [path setLineDash:dashes count:2 phase:0.0];
  9. path.lineCapStyle = kCGLineCapButt;
  10. [path stroke];

字符串

展开查看全部
ecfsfe2w

ecfsfe2w8#

@clément-cardonnel的回答的修改版本。
通过isVertical选项和IBInspectable属性增强

  1. class DottedLineView : UIView {
  2. @IBInspectable var perDashLength: CGFloat = 2.0
  3. @IBInspectable var spaceBetweenDash: CGFloat = 2.0
  4. @IBInspectable var dashColor: UIColor = UIColor.gray
  5. @IBInspectable var lineWidth: CGFloat = 2.0
  6. @IBInspectable var isVertical: Bool = false
  7. private var dashedLayer: CAShapeLayer?
  8. override public func layoutSubviews() {
  9. super.layoutSubviews()
  10. guard bounds.width != dashedLayer?.frame.width,
  11. bounds.height != dashedLayer?.frame.height
  12. else { return }
  13. drawDottedLine()
  14. }
  15. private func drawDottedLine() {
  16. if dashedLayer != nil {
  17. dashedLayer?.removeFromSuperlayer()
  18. }
  19. var startPoint = CGPoint(
  20. x: bounds.width / 2 + lineWidth / 2,
  21. y: 0
  22. )
  23. var endPoint = CGPoint(
  24. x: bounds.width / 2 + lineWidth / 2,
  25. y: bounds.height
  26. )
  27. if !isVertical {
  28. startPoint = CGPoint(
  29. x: 0,
  30. y: bounds.height / 2 + lineWidth / 2
  31. )
  32. endPoint = CGPoint(
  33. x: bounds.width,
  34. y: bounds.height / 2 + lineWidth / 2
  35. )
  36. }
  37. dashedLayer = drawDottedLine(
  38. startPoint: startPoint,
  39. endPoint: endPoint
  40. )
  41. }
  42. private func drawDottedLine(startPoint: CGPoint, endPoint: CGPoint) -> CAShapeLayer {
  43. let shapeLayer = CAShapeLayer()
  44. shapeLayer.strokeColor = dashColor.cgColor
  45. shapeLayer.lineWidth = self.lineWidth
  46. shapeLayer.lineDashPattern = [perDashLength as NSNumber, spaceBetweenDash as NSNumber]
  47. let path = CGMutablePath()
  48. path.addLines(between: [startPoint, endPoint])
  49. shapeLayer.path = path
  50. layer.addSublayer(shapeLayer)
  51. return shapeLayer
  52. }
  53. }

字符串

展开查看全部

相关问题