• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 绘制的线条与触摸位置缩小并淡入背景 Swift 3.0

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:46:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在 Swift 3.0 上遇到了一个奇怪的错误,我成功地绘制到位于 UIView 内的 UIImageView,但是当我绘制之前绘制的线开始淡入并缩小到 UIImageView 的顶部并变得非常模糊时如附图所示。 Beginning of Drawing , Image becoming blurry .

如果能提供任何帮助/指导,我将不胜感激。

class DrawView: UIView {
    var drawArea: UIImageView!
    var clearDrawArea: UIButton!
    var lastTouch = CGPoint.zero

    init(){
        super.init(frame: CGRect(x: UIScreen.main.bounds.width*0.135, y:UIScreen.main.bounds.height*0.05, width: UIScreen.main.bounds.width*0.8, height: UIScreen.main.bounds.height*0.9))
        drawArea = UIImageView(frame: CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.bounds.width*0.9, height: self.bounds.height*0.86))
        drawArea.backgroundColor = UIColor.white

        let buttonWidth = self.bounds.width*0.2
        let buttonHeight = self.bounds.height*0.1
        clearDrawArea = UIButton(frame: CGRect(x: (self.bounds.width*0.61)-(buttonWidth/2), y: self.bounds.height*0.94, width: buttonWidth, height: buttonHeight))
        clearDrawArea.setTitle("Clear Draw Area", for: .normal)
        clearDrawArea.backgroundColor = UIColor(red: 49/255, green: 200/255, blue: 134/255, alpha: 255)
        clearDrawArea.addTarget(self, action: #selector(resetImage), for: .touchUpInside)

        self.addSubview(clearDrawArea)
        self.addSubview(drawArea)

    }

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

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let firstTouch = touches.first{

            lastTouch = firstTouch.location(in: drawArea)
        }

    }
    func drawLines(_ from: CGPoint, to: CGPoint){
        UIGraphicsBeginImageContext(drawArea.frame.size)
        let context = UIGraphicsGetCurrentContext()

        context?.move(to: CGPoint(x: from.x, y: from.y))
        context?.addLine(to: CGPoint(x: to.x, y: to.y))

        context?.setLineCap(.round)
        context?.setLineWidth(3)
        context?.setStrokeColor(UIColor.black.cgColor)

        context?.strokePath()

        drawArea.image?.draw(in: drawArea.bounds)
        drawArea.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {


        if let firstTouch = touches.first{
            let touchLocation = firstTouch.location(in: drawArea)
            drawLines(lastTouch, to: touchLocation)

            lastTouch = touchLocation
        }
    }

    func resetImage(){

        drawArea.image = nil            
    }
}



Best Answer-推荐答案


稍微修改一下你的代码试试下面的..

class DrawView: UIView {
var drawArea: UIImageView!
var clearDrawArea: UIButton!
var lastTouch = CGPoint.zero
let pathAnimation: CABasicAnimation = CABasicAnimation(keyPath:"animation")
let tempPathLayer: CAShapeLayer = CAShapeLayer()
var lineLayer:CAShapeLayer=CAShapeLayer()
init(){
    super.init(frame: CGRect(x: UIScreen.main.bounds.width*0.135, y:UIScreen.main.bounds.height*0.05, width: UIScreen.main.bounds.width*0.8, height: UIScreen.main.bounds.height*0.9))
    drawArea = UIImageView(frame: CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.bounds.width*0.9, height: self.bounds.height*0.86))
    drawArea.backgroundColor = UIColor.white

    let buttonWidth = self.bounds.width*0.2
    let buttonHeight = self.bounds.height*0.1
    clearDrawArea = UIButton(frame: CGRect(x: (self.bounds.width*0.61)-(buttonWidth/2), y: self.bounds.height*0.94, width: buttonWidth, height: buttonHeight))
    clearDrawArea.setTitle("Clear Draw Area", for: .normal)
    clearDrawArea.backgroundColor = UIColor(red: 49/255, green: 200/255, blue: 134/255, alpha: 255)
    clearDrawArea.addTarget(self, action: #selector(resetImage), for: .touchUpInside)

    self.addSubview(clearDrawArea)
    self.addSubview(drawArea)

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    //fatalError("init(coder has not been implemented")
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    if let firstTouch = touches.first{

        lastTouch = firstTouch.location(in: drawArea)
    }

}
func drawLines(_ from: CGPoint, to: CGPoint){
    UIGraphicsBeginImageContext(drawArea.frame.size)

    //let context = UIGraphicsGetCurrentContext()
    let tempPath: UIBezierPath = UIBezierPath()
    var lineLayer:CAShapeLayer=CAShapeLayer()
    tempPath.move(to: from)
    tempPath.addLine(to: to)
    let strokeColor = UIColor.red
    UIColor.blue.setFill()
    strokeColor.setStroke()

    let tempPathLayer: CAShapeLayer = CAShapeLayer()
    tempPathLayer.frame = self.bounds
    tempPathLayer.position=self.layer.position
    tempPathLayer.strokeColor = UIColor.green.cgColor
    tempPathLayer.fillColor = UIColor.red.cgColor
    tempPathLayer.lineWidth = 1.0
    tempPathLayer.path = tempPath.cgPath
    lineLayer=tempPathLayer
    pathAnimation.beginTime=CACurrentMediaTime()
    pathAnimation.duration = 2
    pathAnimation.fromValue = NSNumber(value: 0.0)
    pathAnimation.toValue = NSNumber(value:1.0)

    lineLayer.add(pathAnimation, forKey: "animation")
    self.layer.addSublayer(lineLayer)

    drawArea.image?.draw(in: drawArea.bounds)
    drawArea.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {


    if let firstTouch = touches.first{
        let touchLocation = firstTouch.location(in: drawArea)
        drawLines(lastTouch, to: touchLocation)

        lastTouch = touchLocation
    }
}

func resetImage(){

    drawArea.image = nil            
}

}

关于ios - 绘制的线条与触摸位置缩小并淡入背景 Swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44253559/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap