我是swift的新手,我目前正在关注CS193P的截屏视频。在一集中,它向我展示了使用drawRect创建笑脸。我试图复制它,但事实证明它不能很好地工作。在原始的截屏视频中,结果是吹出的笑脸,但在我的练习中,我的结果如下;
虽然看起来在横向模式上工作。
我的代码如下,你能指导我哪些部分是错的???
import UIKit
class FaceView: UIView {
var faceCenter: CGPoint {
return convertPoint(center, fromView:superview)
}
var scale:CGFloat {
return 0.9
}
private struct Scaling {
static let FaceRadiusToEyeRadiusRatio: CGFloat = 10
static let FaceRadiusToEyeOffsetRatio: CGFloat = 3
static let FaceRadiusToEyeSeperationRatio: CGFloat = 1.5
static let FaceRadiusToMouthWidthRatio: CGFloat = 1
static let FaceRadiusToMouthHeightRatio: CGFloat = 3
static let FaceRadiusToMouthOffsetRatio: CGFloat = 3
}
private enum eye {case Left, Right}
private func bezierPathForEye(whichEye: eye) -> UIBezierPath {
let eyeRadius = faceRadius / Scaling.FaceRadiusToEyeRadiusRatio
let eyeVerticalOffset = faceRadius / Scaling.FaceRadiusToEyeOffsetRatio
let eyeHorizontalSeparation = faceRadius / Scaling.FaceRadiusToEyeSeperationRatio
var eyeCenter = faceCenter
eyeCenter.y = eyeVerticalOffset
switch whichEye {
case .Left: eyeCenter.x -= eyeHorizontalSeparation / 2
case .Right: eyeCenter.x += eyeHorizontalSeparation / 2
}
let path = UIBezierPath(arcCenter: eyeCenter, radius: eyeRadius, startAngle: 0, endAngle: CGFloat(2*M_PI), clockwise: true)
path.lineWidth = lineWidth;
return path
}
private func bezierPathForSmile(fractionOfMaxSmile: Double) -> UIBezierPath {
let mouthWidth = faceRadius / Scaling.FaceRadiusToMouthWidthRatio
let mouthHeight = faceRadius / Scaling.FaceRadiusToMouthHeightRatio
let mouthVerticalOffset = faceRadius / Scaling.FaceRadiusToMouthOffsetRatio
let smileHeight = CGFloat(max(min(fractionOfMaxSmile,1),-1)) * mouthHeight
let start = CGPoint(x:faceCenter.x - mouthWidth / 2 , y:faceCenter.y + mouthVerticalOffset)
let end = CGPoint(x:start.x + mouthWidth, y: start.y)
let cp1 = CGPoint(x:start.x + mouthWidth / 3, y: start.y + smileHeight)
let cp2 = CGPoint(x:end.x - mouthWidth/3, y:cp1.y)
let path = UIBezierPath()
path.moveToPoint(start)
path.addCurveToPoint(end, controlPoint1: cp1, controlPoint2: cp2)
path.lineWidth = lineWidth
return path
}
var faceRadius: CGFloat {
return min(bounds.size.width,bounds.size.height) / 2 * scale
}
var lineWidth: CGFloat = 3 { didSet { setNeedsDisplay() } }
var color: UIColor = UIColor.blueColor() { didSet { setNeedsDisplay() } }
override func drawRect(rect: CGRect) {
let facePath = UIBezierPath(arcCenter: faceCenter, radius: faceRadius, startAngle: 0, endAngle: CGFloat(2*M_PI), clockwise: true)
facePath.lineWidth = 3
color.set()
facePath.stroke()
bezierPathForEye(.Left).stroke()
bezierPathForEye(.Right).stroke()
let smiliness = 0.75
let smilepath = bezierPathForSmile(smiliness)
smilepath.stroke()
}
}
仅供参考,这不是一项任务,我只是想复制一些行动。
答案 0 :(得分:0)
从
改变eyeCenter.y = eyeVerticalOffset
进入这个
eyeCenter.y -= eyeVerticalOffset