用Swift实现笑脸
来源:互联网 发布:sql数据库报表开发 编辑:程序博客网 时间:2024/04/27 19:10
写在前面:这是根据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 UIKitclass 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:以后写代码要记住写注释;
0 0
- 用Swift实现笑脸
- 用键盘移动笑脸
- 笑脸
- 用opencv画一个笑脸的图像
- 编写程序实现demo.exe演示笑脸小球运动效果
- JavaScript中使用画布实现笑脸火柴人
- Android 简单游戏实现笑脸的移动与人物移动
- 仿支付宝笑脸刷新加载动画的实现
- 用Swift实现汉诺塔算法
- 用python和html5在画布上画个车子和笑脸
- 用canvas画一个实心描边的笑脸
- YY-笑脸
- c10 笑脸
- ^=^笑脸
- 笑脸图
- swift UI专项训练39 用Swift实现摇一摇功能
- 用Swift实现一个简单的浏览器
- 用Swift实现一个弹性动画
- Android Bluetooth(蓝牙)实例
- 数理统计与参数估计-七月算法(julyedu.com)4 月机器学习算法班学习笔记
- hdu 1428 漫步校园 记忆化搜索
- 构造器
- 【Python】将Python发布成exe
- 用Swift实现笑脸
- 基于Netbeans的安卓Android开发环境配置
- 函数指针和函数引用的使用
- win10装win7装win7
- 从源码理解LinkedList.java
- XCODE如何像VS(visual studio)一样设置监视变量
- 正则表达式大全(深度解析)
- thinkphp解析进行中
- 大数据学习笔记·社会计算(3)