我尝试初始化并专注于我的屏幕下面的代码使用绘图代码,但我没有设法找到正确的方法总是给我一个错误
import UIKit
public class IndicadorCirculo : NSObject {
//// Drawing Methods
public class func drawCanvas2(#indicadro: CGFloat) {
//// General Declarations
let context = UIGraphicsGetCurrentContext()
//// Color Declarations
let optimo = UIColor(red: 0.350, green: 0.800, blue: 0.320, alpha: 1.000)
//// Oval 2 Drawing
var oval2Path = UIBezierPath(ovalInRect: CGRectMake(48, 107, 237, 244))
optimo.setFill()
oval2Path.fill()
UIColor.blueColor().setStroke()
oval2Path.lineWidth = 8
oval2Path.stroke()
//// Oval Drawing
var ovalPath = UIBezierPath(ovalInRect: CGRectMake(91, 152, 151, 154))
UIColor.whiteColor().setFill()
ovalPath.fill()
UIColor.blueColor().setStroke()
ovalPath.lineWidth = 8
ovalPath.stroke()
//// Bezier Drawing
var bezierPath = UIBezierPath()
UIColor.blueColor().setStroke()
bezierPath.lineWidth = 8
bezierPath.stroke()
//// Bezier 2 Drawing
CGContextSaveGState(context)
CGContextTranslateCTM(context, 166.57, 231.46)
CGContextRotateCTM(context, -indicadro * CGFloat(M_PI) / 180)
var bezier2Path = UIBezierPath()
bezier2Path.moveToPoint(CGPointMake(0.43, -124.22))
bezier2Path.addCurveToPoint(CGPointMake(0.43, -79.28), controlPoint1: CGPointMake(0.43, -77.68), controlPoint2: CGPointMake(0.43, -79.28))
UIColor.darkGrayColor().setStroke()
bezier2Path.lineWidth = 8
bezier2Path.stroke()
CGContextRestoreGState(context)
}
}
@objc protocol StyleKitSettableImage {
func setImage(image: UIImage!)
}
@objc protocol StyleKitSettableSelectedImage {
func setSelectedImage(image: UIImage!)
}
我使用了init函数和框架不起作用,如果你能帮忙我很感激
感谢。
答案 0 :(得分:2)
正如马特所说,你没有图形上下文。要在屏幕上显示您的圈子,您需要有一个UIView。 UIView中有一个方法drawRect(),只要需要绘制视图,就会自动调用该方法。 (切勿直接在代码中调用drawRect()。)
DrawRect()使您可以访问视图的当前绘图上下文。
试试这个:
创建一个新的swift文件CirculoView作为UIView的子类。
在故事板中,将新的UIView添加到主视图中。将此UIView的类更改为CirculoView。
将CirculoView.swift更改为使用drawRect()方法中的PaintCode代码。
这是代码
import UIKit
class CirculoView: UIView {
var indicadro:CGFloat = 0 {
didSet {
self.setNeedsDisplay()
}
}
override func drawRect(rect: CGRect) {
//// General Declarations
let context = UIGraphicsGetCurrentContext()
//// Color Declarations
let optimo = UIColor(red: 0.350, green: 0.800, blue: 0.320, alpha: 1.000)
//// Oval 2 Drawing
var oval2Path = UIBezierPath(ovalInRect: CGRectMake(48, 107, 237, 244))
optimo.setFill()
oval2Path.fill()
UIColor.blueColor().setStroke()
oval2Path.lineWidth = 8
oval2Path.stroke()
//// Oval Drawing
var ovalPath = UIBezierPath(ovalInRect: CGRectMake(91, 152, 151, 154))
UIColor.whiteColor().setFill()
ovalPath.fill()
UIColor.blueColor().setStroke()
ovalPath.lineWidth = 8
ovalPath.stroke()
//// Bezier Drawing
var bezierPath = UIBezierPath()
UIColor.blueColor().setStroke()
bezierPath.lineWidth = 8
bezierPath.stroke()
//// Bezier 2 Drawing
CGContextSaveGState(context)
CGContextTranslateCTM(context, 166.57, 231.46)
CGContextRotateCTM(context, -indicadro * CGFloat(M_PI) / 180)
var bezier2Path = UIBezierPath()
bezier2Path.moveToPoint(CGPointMake(0.43, -124.22))
bezier2Path.addCurveToPoint(CGPointMake(0.43, -79.28), controlPoint1: CGPointMake(0.43, -77.68), controlPoint2: CGPointMake(0.43, -79.28))
UIColor.darkGrayColor().setStroke()
bezier2Path.lineWidth = 8
bezier2Path.stroke()
CGContextRestoreGState(context)
}
}
我只是跑了一下,在视图中看到了一个带蓝色笔划的绿色圆圈。