如何在Swift 4中制作虚线或虚线

时间:2017-10-13 14:17:39

标签: swift core-graphics

我一直试图在这样的游乐场里这样做:

import UIKit
import XCPlayground

let path = UIBezierPath()
path.move(to: CGPoint(x:10,y:10))
path.addLine(to: CGPoint(x:290,y:10))
path.lineWidth = 2

let dashPattern : [CGFloat] = [0, 16] 
path.setLineDash(dashPattern, count: 2, phase: 0)
path.lineCapStyle = CGLineCap.round
path.lineCapStyle = .butt
UIGraphicsBeginImageContextWithOptions(CGSize(width:300, height:20), false, 2)
path.stroke()

XCPlaygroundPage.currentPage.captureValue(value: UIGraphicsGetImageFromCurrentImageContext(), withIdentifier: "image")
UIGraphicsEndImageContext()

但结果是:

enter image description here

1 个答案:

答案 0 :(得分:2)

1)在我看来,public void init() { ImageButton driver=(ImageButton)findViewById(R.id.driver); driver.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent login=new Intent(MapsActivity.this,Driverlogin.class); startActivity(login); } }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { checkLocationPermission(); } // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); init(); } 的破折号模式并不合理。两个值都应大于0。

2)除此之外,你的代码是正确的,但Xcode中[0, 16]的游乐场预览忽略了破折号模式(就像它忽略了颜色和许多其他设置 - 它只是在抽象中显示路径方式)。

如果将路径渲染到图像中,则会显示虚线图案。这是我使用的代码(我将短划线模式数组更改为UIBezierPath):

[16,16]