用Swift实现笑脸
2016-05-05 21:32
363 查看
写在前面:这是根据Stanford的Swift课程敲的一个小程序;
Swift 及 storyboard知识点:
1、UIViewController 及 UIView的布局
2、CGFloat CGPoint、UIColor的使用
3、UIBezierPath的使用
2、旋转屏幕实现redraw效果
实现旋转的redraw
笑脸实现相关代码:
PS:以后写代码要记住写注释;
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:以后写代码要记住写注释;
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift自定义iOS中的TabBarController并为其添加动画
- Swift编程中的泛型解析
- Swift中定义二维数组的方法及遍历方法示例
- 简单分析Swift语言的一些基本特征
- 使用 Swift 语言编写 Android 应用入门
- Swift与C语言指针结合使用实例
- Swift心得笔记之控制流
- 用Swift构建一个简单的iOS邮件应用的方法
- 苹果公司推出的新编程语言Swift简介和入门教程
- Swift实现iOS应用中短信验证码倒计时功能的实例分享
- 探讨Swift数组和字典
- Swift中的指针操作和使用详细介绍
- Swift中使用正则表达式的一些方法
- Swift心得笔记之运算符
- Swift中使用可选类型完美解决占位问题
- Swift学习笔记之构造器重载