如何通过Swift在一个视图中为不同的对象设置不同的颜色?

时间:2015-12-09 06:39:01

标签: swift

大家。

我做了一个练习,它显示了七个圆圈,一个大,六个小。 六个小圈子在大牌圈内。

现在,我想为六个圆圈设置不同的颜色。 我该怎么办?

由于

import UIKit

类CircleInCircle:UIView {

@IBInspectable 
var externalScale: CGFloat = 0.9 {
    didSet {
        externalScale = min(max(externalScale, 0), 1)
    }
}

var externalCenter: CGPoint {
    return convertPoint(center, fromView: superview)
}

var externalRadius: CGFloat {
    return min(bounds.size.height, bounds.size.width) / 2 * externalScale
}

@IBInspectable
var color: UIColor = UIColor.redColor() {
    didSet { setNeedsDisplay() }
}

func scale(gestrue: UIPinchGestureRecognizer) {
    if gestrue.state == .Changed {
        externalScale *= gestrue.scale
        gestrue.scale = 1
    }
}

enum internalCircle {
    case one, two, three, four, five, six
}

func internalCircleCondition(whichCircle: internalCircle, rotate: Double) -> UIBezierPath {

    var internalCenter = externalCenter

    let internalRadius = externalRadius / 15

    switch whichCircle {
    case .one:
        internalCenter = CGPoint(x: externalCenter.x - externalRadius / 7 * 1 * cos(CGFloat(M_PI) / 6 * 1 * CGFloat(rotate)), y: (externalCenter.y - externalRadius / 7 * 1 * sin(CGFloat(M_PI) / 6 * 1 * CGFloat(rotate))))
    case .two:
        internalCenter = CGPoint(x: externalCenter.x - externalRadius / 7 * 2 * cos(CGFloat(M_PI) / 6 * 3 * CGFloat(rotate)), y: (externalCenter.y - externalRadius / 7 * 2 * sin(CGFloat(M_PI) / 6 * 3 * CGFloat(rotate))))
    case .three:
        internalCenter = CGPoint(x: externalCenter.x - externalRadius / 7 * 3 * cos(CGFloat(M_PI) / 6 * 5 * CGFloat(rotate)), y: (externalCenter.y - externalRadius / 7 * 3 * sin(CGFloat(M_PI) / 6 * 5 * CGFloat(rotate))))
    case .four:
        internalCenter = CGPoint(x: externalCenter.x - externalRadius / 7 * 4 * cos(CGFloat(M_PI) / 6 * 7 * CGFloat(rotate)), y: (externalCenter.y - externalRadius / 7 * 4 * sin(CGFloat(M_PI) / 6 * 7 * CGFloat(rotate))))
    case .five:
        internalCenter = CGPoint(x: externalCenter.x - externalRadius / 7 * 5 * cos(CGFloat(M_PI) / 6 * 9 * CGFloat(rotate)), y: (externalCenter.y - externalRadius / 7 * 5 * sin(CGFloat(M_PI) / 6 * 9 * CGFloat(rotate))))
    case .six:
        internalCenter = CGPoint(x: externalCenter.x - externalRadius / 7 * 6 * cos(CGFloat(M_PI) / 6 * 11 * CGFloat(rotate)), y: (externalCenter.y - externalRadius / 7 * 6 * sin(CGFloat(M_PI) / 6 * 11 * CGFloat(rotate))))
    }

    let internalCirclePath = UIBezierPath(arcCenter: internalCenter, radius: internalRadius, startAngle: 0, endAngle: CGFloat(2*M_PI), clockwise: true)

    internalCirclePath.lineWidth = 2

    return internalCirclePath
}

  override func drawRect(rect: CGRect) {

    let externalCirclePath = UIBezierPath(arcCenter: externalCenter, radius: externalRadius, startAngle: 0, endAngle: CGFloat(2*M_PI), clockwise: true)

    externalCirclePath.lineWidth = 3

    color.set()

    externalCirclePath.stroke()

    let rotateRatio = ratio?.rotateRatioInCircle(self) ?? 1.0

    internalCircleCondition(.one, rotate: rotateRatio).stroke()
    internalCircleCondition(.two, rotate: rotateRatio).stroke()
    internalCircleCondition(.three, rotate: rotateRatio).stroke()
    internalCircleCondition(.four, rotate: rotateRatio).stroke()
    internalCircleCondition(.five, rotate: rotateRatio).stroke()
    internalCircleCondition(.six, rotate: rotateRatio).stroke()

  }

}

Demo Result

0 个答案:

没有答案