Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在swift 3中的图像上的两点之间画一条线?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 swift的新手,我想在我称为mapView的图像上画两条点之间的线,我试图使用CGContext但没有结果,任何想法都有帮助吗?谢谢.

@H_403_8@

@H_403_8@

UIGraphicsBeginImageContext(mapView.bounds.sizE)
    let context : CGContext = UIGraphicsGetCurrentContext()!
    context.addLines(between: [CGPoint(x:oldX,y:oldY),CGPoint(x:newX,y:newY)])
    context.setstrokeColorSpace(CGColorSpaceCreateDeviceRGB())
    context.setstrokeColor(UIColor.blue.cgColor.components!)
    context.setLineWidth(3)
    mapView?.image?.draw(at: CGPoint(x:0,y:0))
    context.strokePath()
    mapView.image = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

解决方法

一种选择是在图像视图中添加子视图,并将线条绘制代码添加到其绘图(_ rect:CGRect)方法中.

@H_403_8@

示例游乐场实施:@H_403_8@

@H_403_8@

class LineView : UIView {
    override init(frame: CGRect) {
        super.init(frame: framE)
        self.BACkgroundColor = UIColor.init(white: 0.0,alpha: 0.0)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext() {
            context.setstrokeColor(UIColor.blue.cgColor)
            context.setLineWidth(3)
            context.beginPath()
            context.move(to: CGPoint(x: 5.0,y: 5.0)) // This would be oldX,oldY
            context.addLine(to: CGPoint(x: 50.0,y: 50.0)) // This would be newX,newY
            context.strokePath()
        }
    }
}


let imageView = UIImageView(image: #imageLiteral(resourcename: "image.png")) // This would be your mapView,here I am just using a random image
let lineView = LineView(frame: imageView.framE)
imageView.addSubview(lineView)

大佬总结

以上是大佬教程为你收集整理的如何在swift 3中的图像上的两点之间画一条线?全部内容,希望文章能够帮你解决如何在swift 3中的图像上的两点之间画一条线?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。