您的位置:首页 > 移动开发 > Swift

用Swift实现笑脸

2016-05-05 21:32 363 查看
写在前面:这是根据Stanford的Swift课程敲的一个小程序;

Swift 及 storyboard知识点:

1、UIViewController 及 UIView的布局

2、CGFloat CGPoint、UIColor的使用

3、UIBezierPath的使用

实现效果:

1、一个简单线条组成的笑脸(可变成不高兴脸)

2、旋转屏幕实现redraw效果



实现旋转的redraw



笑脸实现相关代码:

//
//  FaceView.swift
//  Happiness
//
//  Created by VincentYau on 5/5/16.
//  Copyright (c) 2016 VincentYau. All rights reserved.
//

import UIKit

class FaceView: UIView
{

var lineWidth: CGFloat = 3{
didSet {setNeedsDisplay()}
}

var color: UIColor = UIColor.redColor(){didSet {setNeedsDisplay()}}
var scale: CGFloat = 0.90 {didSet {setNeedsDisplay()}}

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

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

private struct Scaling {
static let FaceRadiusToEyeRadiusRatio: CGFloat = 10
static let FaceRadiusToEyeOffsetRatio: CGFloat = 3
static let FaceRadiusToEyeSeparationRatio: 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.FaceRadiusToEyeSeparationRatio

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)
return path
}

override func drawRect(rect: CGRect)
{
let facePath = UIBezierPath(arcCenter: faceCenter, radius: faceRadius, startAngle: 0, endAngle: CGFloat(2*M_PI), clockwise: true)
facePath.lineWidth = lineWidth
color.set()
facePath.stroke()

bezierPathForEye(.Left).stroke()
bezierPathForEye(.Right).stroke()

let smiliness = -0.5
let smilePath = bezierPathForSmile(smiliness)
smilePath.stroke()

}

}


PS:以后写代码要记住写注释;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swift