jjzjj

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

coder 2024-01-29 原文

我在 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            
    }
}

最佳答案

稍微更改了您的代码尝试以下操作..

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/

有关ios - 带有触摸位置的绘制线条缩小并淡入背景 Swift 3.0的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. ruby - 正则表达式在哪个位置失败? - 2

    我需要一个非常简单的字符串验证器来显示第一个符号与所需格式不对应的位置。我想使用正则表达式,但在这种情况下,我必须找到与表达式相对应的字符串停止的位置,但我找不到可以做到这一点的方法。(这一定是一种相当简单的方法……也许没有?)例如,如果我有正则表达式:/^Q+E+R+$/带字符串:"QQQQEEE2ER"期望的结果应该是7 最佳答案 一个想法:你可以做的是标记你的模式并用可选的嵌套捕获组编写它:^(Q+(E+(R+($)?)?)?)?然后你只需要计算你获得的捕获组的数量就可以知道正则表达式引擎在模式中停止的位置,你可以确定匹配结束

  5. ruby-on-rails - 带有 Zeus 的 RSpec 3.1,我应该在 spec_helper 中要求 'rspec/rails' 吗? - 2

    使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做

  6. Ruby:如何使用带有散列的 'send' 方法调用方法? - 2

    假设我有一个类A,里面有一些方法。假设stringmethodName是这些方法之一,我已经知道我想给它什么参数。它们在散列中{'param1'=>value1,'param2'=>value2}所以我有:params={'param1'=>value1,'param2'=>value2}a=A.new()a.send(methodName,value1,value2)#callmethodnamewithbothparams我希望能够通过传递我的哈希以某种方式调用该方法。这可能吗? 最佳答案 确保methodName是一个符号,而

  7. ruby-on-rails - 带有 Pry 的 Rails 控制台 - 2

    当我进入Rails控制台时,我已将pry设置为加载代替irb。我找不到该页面或不记得如何将其恢复为默认行为,因为它似乎干扰了我的Rubymine调试器。有什么建议吗? 最佳答案 我刚发现问题,pry-railsgem。忘记了它的目的是让“railsconsole”打开pry。 关于ruby-on-rails-带有Pry的Rails控制台,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question

  8. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  9. 带有 attr_accessor 的类上的 Ruby instance_eval - 2

    我了解instance_eval和class_eval之间的基本区别。我在玩弄时发现的是一些涉及attr_accessor的奇怪东西。这是一个例子:A=Class.newA.class_eval{attr_accessor:x}a=A.newa.x="x"a.x=>"x"#...expectedA.instance_eval{attr_accessor:y}A.y="y"=>NoMethodError:undefinedmethod`y='forA:Classa.y="y"=>"y"#WHATTT?这是怎么回事:instance_eval没有访问我们的A类(对象)然后它实际上将它添加到

  10. ruby - 下载位置 Selenium-webdriver Cucumber Chrome - 2

    我将Cucumber与Ruby结合使用。通过Selenium-Webdriver在Chrome中运行测试时,我想将下载位置更改为测试文件夹而不是用户下载文件夹。我当前的chrome驱动程序是这样设置的:Capybara.default_driver=:seleniumCapybara.register_driver:seleniumdo|app|Capybara::Selenium::Driver.new(app,:browser=>:chrome,desired_capabilities:{'chromeOptions'=>{'args'=>%w{window-size=1920,1

随机推荐