在纵向模式下协调笑脸效果不佳
Coordinating smiley face on portrait mode doesn't work well
我是 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()
}
}
仅供参考,这不是作业,我只是想复制一些东西来锻炼。
变化自
eyeCenter.y = eyeVerticalOffset
进入这个
eyeCenter.y -= eyeVerticalOffset
我是 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()
}
}
仅供参考,这不是作业,我只是想复制一些东西来锻炼。
变化自
eyeCenter.y = eyeVerticalOffset
进入这个
eyeCenter.y -= eyeVerticalOffset