Ios Swift xcode错误:无法推断';形状';在错误类型中找不到和枚举案例
我试图在xcode中创建一个绘图方法,以便在UIView中绘制不同的形状。此应用程序的目的是将图形附加到保存在表格中的行(如果需要,我将发布表格代码,但我认为没有必要出现此错误) 错误截图:Ios Swift xcode错误:无法推断';形状';在错误类型中找不到和枚举案例,ios,xcode,swift,Ios,Xcode,Swift,我试图在xcode中创建一个绘图方法,以便在UIView中绘制不同的形状。此应用程序的目的是将图形附加到保存在表格中的行(如果需要,我将发布表格代码,但我认为没有必要出现此错误) 错误截图: 导入UIKit /* 该程序适用于Xcode 6.3和Swift 1.2 */ 类画布视图:UIView{ 枚举形状类型:字符串 { case Line=“Line” 大小写椭圆=“椭圆” case Rectangle=“Rectangle” case filledelipse=“填充椭圆” 大小写Fil
导入UIKit
/*
该程序适用于Xcode 6.3和Swift 1.2
*/
类画布视图:UIView{
枚举形状类型:字符串
{
case Line=“Line”
大小写椭圆=“椭圆”
case Rectangle=“Rectangle”
case filledelipse=“填充椭圆”
大小写FilledRectangle=“填充矩形”
case Scribble=“Scribble”
}
变量shape:ShapeType=.Line
var color:UIColor=UIColor.blueColor()
变量优先:CGPoint=CGPointZero
最后一个变量:CGPoint=CGPointZero
变量点:[CGPoint]=[]
//仅覆盖drawRect:如果执行自定义绘图。
//空实现会对动画期间的性能产生不利影响。
重写func drawRect(rect:CGRect){
//绘图代码
let context=UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(上下文,color.CGColor)
CGContextSetFillColorWithColor(上下文,color.CGColor)
设rect=CGRect(x:first.x,y:first.y,宽度:last.x-first.x,高度:last.y-first.y)
开关形状{
案例行:
CGContextMoveToPoint(上下文,first.x,first.y)
CGContextAddLineToPoint(上下文,last.x,last.y)
CGContextStrokePath(上下文)
案例.椭圆:
CGContextStrokeEllipseInRect(上下文,rect)
长方形:
CGContextStrokeRect(上下文,rect)
案例。填充椭圆:
CGContextFilllellipseInRect(上下文,rect)
大小写。填充反射角:
CGContextFillRect(上下文,rect)
案例。涂鸦:
CGContextMoveToPoint(上下文,first.x,first.y)
对于p点{
CGContextAddLineToPoint(上下文,p.x,p.y)
}
CGContextStrokePath(上下文)
}
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
如果让触摸=先触摸{
第一个=触摸。位置查看(自)
最后一个=第一个
points.removeAll(keepCapacity:true)
如果形状==.Scribble{
points.append(第一个)
}
setNeedsDisplay()
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
如果让触摸=先触摸{
最后一次=触摸。位置查看(自)
如果形状==.Scribble{
points.append(最后一个)
}
setNeedsDisplay()
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
如果让触摸=先触摸{
最后一次=触摸。位置查看(自)
如果形状==.Scribble{
points.append(最后一个)
}
setNeedsDisplay()
}
}
覆盖功能触摸已取消(触摸:设置?,带事件:UIEvent?){
}
}
还有一些奇怪的事情正在发生。所有错误都与它们指向的代码不匹配。尝试重新启动Xcode或执行“清理并生成”(在菜单“产品>清理”中),或者,如果“清理”不起作用,请找到“派生数据”文件夹(Xcode“首选项”-“位置”),退出Xcode,清空“派生数据”文件夹,然后重新启动Xcode。此程序适用于Xcode 6.3和Swift 1.2
此外,请尝试更新Xcode。@nhgrif-我同意,但是我在Xcode6.3中测试了它,它工作得很好(除了触摸方法的签名不正确之外)。
import UIKit
/*
This program is for Xcode 6.3 and Swift 1.2
*/
class CanvasView: UIView {
enum ShapeType: String
{
case Line = "Line"
case Ellipse = "Ellipse"
case Rectangle = "Rectangle"
case FilledEllipse = "Filled Ellipse"
case FilledRectangle = "Filled Rectangle"
case Scribble = "Scribble"
}
var shape: ShapeType = .Line
var color: UIColor = UIColor.blueColor()
var first :CGPoint = CGPointZero
var last :CGPoint = CGPointZero
var points: [CGPoint] = []
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, color.CGColor)
CGContextSetFillColorWithColor(context, color.CGColor)
let rect = CGRect(x: first.x, y: first.y, width: last.x - first.x, height: last.y - first.y)
switch shape {
case .Line:
CGContextMoveToPoint(context, first.x, first.y)
CGContextAddLineToPoint(context, last.x, last.y)
CGContextStrokePath(context)
case .Ellipse:
CGContextStrokeEllipseInRect(context, rect)
case .Rectangle:
CGContextStrokeRect(context, rect)
case .FilledEllipse:
CGContextFillEllipseInRect(context, rect)
case .FilledRectangle:
CGContextFillRect(context, rect)
case .Scribble:
CGContextMoveToPoint(context, first.x, first.y)
for p in points {
CGContextAddLineToPoint(context, p.x, p.y)
}
CGContextStrokePath(context)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
first = touch.locationInView(self)
last = first
points.removeAll(keepCapacity: true)
if shape == .Scribble {
points.append(first)
}
setNeedsDisplay()
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
last = touch.locationInView(self)
if shape == .Scribble {
points.append(last)
}
setNeedsDisplay()
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
last = touch.locationInView(self)
if shape == .Scribble {
points.append(last)
}
setNeedsDisplay()
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
}
}