用Swift实现笑脸

Posted 文恒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Swift实现笑脸相关的知识,希望对你有一定的参考价值。

写在前面:这是根据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实现笑脸的主要内容,如果未能解决你的问题,请参考以下文章

纯CSS3实现的酷炫笑脸猫动画

仿支付宝笑脸刷新加载动画的实现

☺ 这个笑脸是如何打出来的?

在 windows phone 8.1 C# 中显示 &#8211 带有笑脸的文本(笑脸看起来像 &#8211。)

实现:笑脸_Crack

如何在我的警报视图上添加笑脸? [复制]