【发布时间】: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