【问题标题】:How do you set different lines with different colours?你如何设置不同颜色的不同线条?
【发布时间】:2016-04-10 11:18:09
【问题描述】:

我一直试图为每条线赋予不同的颜色,但每次我移动负责为线提供颜色的滑块时,每条线都会改变其颜色。我尝试将我从滑块中获得的值放入 for 循环中,但由于某种原因,它并没有给每条线提供不同的颜色。存储变量是我用来存储视图控制器类中定义的滑块的值的变量。

为了澄清我想要做什么,这里有一个例子: 第 1 行可以是红色,第 2 行可以是紫色。

这是我目前更改线条颜色的尝试:

import UIKit
import CoreData

class DrawClass: UIView {


var lines:[Line] = []
var lastPoint: CGPoint!


required init(coder aDecoder: NSCoder)
{
    super.init(coder: aDecoder)
    self.backgroundColor = UIColor.whiteColor()
    self.layer.zPosition = 1

}




override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent){
    if let touch = touches.first as? UITouch {
        lastPoint =  touch.locationInView(self)
    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent){
    if let touch = touches.first as? UITouch {
        var newPoint = touch.locationInView(self)
        lines.append(Line(start: lastPoint, end: newPoint))
        lastPoint = newPoint
    }
    self.setNeedsDisplay()
}






override func drawRect(rect: CGRect) {
    var context = UIGraphicsGetCurrentContext()


    var storage: Float = ViewController.simple.storage1
    var storage2: Float = ViewController.simple.storage2
    var storage3: Float = ViewController.simple.storage3

    for line in lines
    {
        CGContextBeginPath(context)
        CGContextMoveToPoint(context, line.start.x, line.start.y)
        CGContextAddLineToPoint(context, line.end.x, line.end.y)
        CGContextSetRGBStrokeColor(context, CGFloat(storage), CGFloat(storage2), CGFloat(storage3), 1)
        CGContextSetLineWidth(context, 5)
        CGContextStrokePath(context)
    }



}


}

和这行代码有什么关系吗:

lines.append(Line(start: lastPoint, end: newPoint))

【问题讨论】:

    标签: ios swift colors drawing drawrect


    【解决方案1】:

    当 drawRect 被调用时,它会重绘所有的线条。您需要将颜色与线条相关联。你可以这样做,在你的 Line 对象上添加颜色属性,然后在 touchesMoved 中设置它们,然后在 drawRect 中使用这些属性来设置 StrokeColor。例如添加到行:

       var redValue:CGFloat
       var greenValue:CGFloat
       var blueValue:CGFloat
    

    在 touchesMoved 中,可能真的应该是 touchesEnded,这样说:

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent){
        if let touch = touches.first as? UITouch {
            var newPoint = touch.locationInView(self)
            lines.append(Line(start: lastPoint, end: newPoint,redValue:ViewController.simple.storage1,greenValue:ViewController.simple.storage2,blueValue:ViewController.simple.storage3))
            lastPoint = newPoint
        }
        self.setNeedsDisplay()
    }
    

    然后在drawRect中:

    override func drawRect(rect: CGRect) {
        var context = UIGraphicsGetCurrentContext()
    
        for line in lines
        {
            CGContextBeginPath(context)
            CGContextMoveToPoint(context, line.start.x, line.start.y)
            CGContextAddLineToPoint(context, line.end.x, line.end.y)
            CGContextSetRGBStrokeColor(context, line.redValue, line.greenValue, line.blueValue, 1)
            CGContextSetLineWidth(context, 5)
            CGContextStrokePath(context)
        }
    }
    

    【讨论】:

    • 谢谢!它现在按预期运行!但是,我不得不将线条颜色值转换为 CGFloat 并删除起点和终点变量,因为我没有使用这些参数。
    【解决方案2】:

    您正在为图形中的每种颜色进行颜色更改。 如果您只想更改一个段的颜色(例如第一个段),则必须添加一个测试,例如

    if line == 0 {
        CGContextSetRGBStrokeColor(context, CGFloat(storage), CGFloat(storage2), CGFloat(storage3), 1)
    } else {
        CGContextSetRGBStrokeColor(context, CGFloat(0.0), CGFloat(0.), CGFloat(0.0), 1)
    }
    

    如果所有线段都可以有不同的颜色,您可以将颜色存储到线对象中,并像点一样检索它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 2018-11-26
      • 2023-03-19
      相关资源
      最近更新 更多