ios笑脸app实现

import UIKit

@IBDesignable
class FaceView: UIView { @IBInspectable
var lineWidth:CGFloat=3{didSet{setNeedsLayout()}}
@IBInspectable
var color:UIColor = UIColor.blueColor(){didSet{setNeedsLayout()}}
@IBInspectable
var scale:CGFloat=0.9{didSet{setNeedsLayout()}} 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
//大圆半径(face半径)与小圆半径(eye半径)的比率,次数越小,小圆越大.因为 下面bezierPathForEye的方法中定义 eyeRadius = faceRadius / Scaling.FaceRadiusToEyeRadiusRatio
static let FaceRadiusToEyeOffsetRatio: CGFloat = 3
//大圆与小圆的偏移率,此数越大,小圆的圆心距大圆越近
static let FaceRadiusToEyeSeparationRatio: CGFloat = 1.5
//两个小圆之间在大圆内的分离比率
static let FaceRadiusToEyeMounthWidthRatio: CGFloat = 1
static let FaceRadiusToEyeMounthHeightRatio: CGFloat = 3
static let FaceRadiusToEyeMounthOffsetRatio: CGFloat = 3
} private enum Eye {
case Left , Right
} private func bezierPathForEye(whichEye: Eye) -> UIBezierPath {
//此处定义的方法为设置一只眼睛的位置,上面定义了左右眼的枚举,可通过调用.Left.Right来实现两个位置的设定
let eyeRadius = faceRadius / Scaling.FaceRadiusToEyeRadiusRatio
//定义小圆半径是大圆半径的几分之几,此处因为FaceRadiusToEyeRadiusRatio: CGFloat = 10 故为十分之一
let eyeVerticalOffset = faceRadius / Scaling.FaceRadiusToEyeOffsetRatio
//小圆的垂直偏距
let eyeHorizontalSeparation = faceRadius / Scaling.FaceRadiusToEyeSeparationRatio
//小圆的水平距离 var eyeCenter = faceCenter
eyeCenter.y -= eyeVerticalOffset
//此处相当于是用大圆圆心的y坐标减去小圆圆心的y坐标,故小圆圆心在大圆圆心之上.若为加,则在下
switch whichEye {
case .Left: eyeCenter.x -= eyeHorizontalSeparation / 2
//相当于大圆圆心的x坐标减去(小圆圆心的x坐标除以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.FaceRadiusToEyeMounthWidthRatio
//大圆半径与线宽的比率,此处线宽=大圆半径
let mouthHeight = faceRadius / Scaling.FaceRadiusToEyeMounthHeightRatio
//mouthHeight即线的中点到圆心的距离
let mouthVerticalOffset = faceRadius / Scaling.FaceRadiusToEyeMounthOffsetRatio let smileHeight = CGFloat(max(min(fractionOfMaxSmile, 1), -1)) * mouthHeight
//此处max(min(fractionOfMaxSmile, 1), -1)限定了笑脸指数只能在-1到1之间,fractionOfMaxSmile这个参数可以自行设定,如果设定的大于1,则只取1,设定小于-1,则只取-1 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) //设置曲线点1,此处mouthWidth / 3用于调节曲线的弧度
let cp2 = CGPoint(x: end.x - mouthWidth / 3, y: cp1.y) //设置曲线点2 let path = UIBezierPath()
path.moveToPoint(start)
path.addCurveToPoint(end, controlPoint1: cp1, controlPoint2: cp2)
path.lineWidth = lineWidth
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.8
let smilePath = bezierPathForSmile(smiliness)
smilePath.stroke()
}
}
  • IBDesignable可以在storyboard中看到自定义的uiview
  • IBInspectable使属性可以改变

利用协议与代理联结数据源

protocol FaceViewDataSource:class {
func smilnessForFaceView(sender:FaceView)->Double?
}

手势识别实现缩放与改变笑脸弧度

 @IBOutlet weak var faceView: FaceView!{
didSet{
faceView.dataSource=self
faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: "scale:"))
//faceView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: "changeHappiness:")) }
}
private struct Constants{
static let HappinessGestureScale:CGFloat=4
} @IBAction func changeHappiness(sender: UIPanGestureRecognizer) {
switch sender.state {
case .Ended:
fallthrough
case .Changed:
let translation=sender.translationInView(faceView) let happinessChange = -Int(translation.y/Constants.HappinessGestureScale) if happinessChange != 0{
happiness+=happinessChange
sender.setTranslation(CGPoint.zero, inView: faceView)
}
default:
break }
} func scale(gesture:UIPinchGestureRecognizer){
if gesture.state == .Changed{ scale*=gesture.scale gesture.scale=1
}
}

源代码:Happiness

IOS之笑脸app的更多相关文章

  1. iOS高仿app源码:纯代码打造高仿优质《内涵段子》

    iOS高仿app源码:纯代码打造高仿优质<内涵段子>收藏下来 字数1950 阅读4999 评论173 喜欢133 Github 地址 https://github.com/Charlesy ...

  2. 分分钟解决iOS开发中App启动广告的功能

    前不久有朋友需要一个启动广告的功能,我说网上有挺多的,他说,看的不是很理想.想让我写一个,于是乎,抽空写了一个,代码通俗易懂,简单的封装了一下,各种事件用block回调的,有俩种样式的广告,一种是全屏 ...

  3. iOS UIKit:App

    1.App生命周期 IOS架构是由许多设计模式实现,如model-view-controller 和 delegation模式. 1.1 main函数 与其它框架类似,IOS框架的入口也是从main函 ...

  4. 【HELLO WAKA】WAKA iOS客户端 之一 APP分析篇

    由于后续篇幅比较大,所以调整了内容结构. 全系列 [HELLO WAKA]WAKA iOS客户端 之一 APP分析篇 [HELLO WAKA]WAKA iOS客户端 之二 架构设计与实现篇 [HELL ...

  5. <iOS开发>之App上架流程(2017)

    本文主要介绍了App上架流程,以及上架过程中会遇到的一些问题. 一.App上架前的准备. 上架前,需要开发人员有苹果开发者账号,具体请阅读苹果开发者账号注册申请流程.本文是在已经拥有开发者账号的前提下 ...

  6. ios 推送app badge 数字累加操作

    ios 推送app badge 数字累加操作: 一:此数字需要后台配合: 二:大致原理: 后台发推送时,第一次 传badge 为1,往后,依次累加操作即可: 当用户打开app时,app向后台发送请求, ...

  7. iOS 轻松使用 App 数据统计

    想获取用户各项行为数据吗? 想轻松查看用户行为图表吗? 想高效进行 App 运营管理吗? 想,来我带你玩转 App 数据统计.这里我使用专业.轻便的 JAnalytics. 本文内容分为两部分:代码示 ...

  8. IOS研究之App转让流程须知具体介绍

     网络上有非常多开发人员提问怎么转让App并想知道具体的流程.实际上Appstore的App转让流程还是比較简单的.以下特酷吧依据自己的实际操作总结下iOS Appstore中App的转让流程.供 ...

  9. 有谁知道什么工具测试IOS手机上APP的性能软件啊?

    有谁知道什么工具测试IOS手机上APP的性能软件啊?

随机推荐

  1. [Android分享] 【转帖】Android ListView的A-Z字母排序和过滤搜索功能

      感谢eoe社区的分享   最近看关于Android实现ListView的功能问题,一直都是小伙伴们关心探讨的Android开发问题之一,今天看到有关ListView实现A-Z字母排序和过滤搜索功能 ...

  2. 一种map容器遍历的方法

    遍历算法是一种很常见而且非常重要的算法,我们用map容器的时候可能用的比较多的是查找,我今天才第一次要用到遍历.下面举个例子就知道了. map<string,string> mp; str ...

  3. python 数据结构 初学时没太注意却发现很有用的点点滴滴

    1. list.extend(L) 将指定列表中的所有元素附加到另一个列表的末尾:相当于a[len(a):] = L. 2. list.pop([i]) 删除列表中指定位置的元素并返回它.如果未指定索 ...

  4. [KOJ95603]全球奥运

    [COJ95603]全球奥运 试题描述 一个环形的图中有N个城市,奥运会重要项目就是传递圣火,每个城市有A[i]个圣火,每个城市可以向它相邻的城市传递圣火(其中1号城市可以传递圣火到N号城市或2号城市 ...

  5. C语言 homework (3)

      #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { ; char c; do{ ...

  6. BZOJ 1090: [SCOI2003]字符串折叠

    Sol 区间DP. 转移很简单,枚举会形成的断长转移就行,话说上一题我就跟这个是差不多的思路,转移改了改,然后死活过不了... 同样都是SCOI的题...相差4年... Code /********* ...

  7. Windows 2008R2关闭网络发现

    在Windows Server 2008 R2安装完后,默认情况下,在高级共享设置中无法对网络发现的更改进行保存(每次选择"启用网络发现"后保存修改,但重新打开"高级共享 ...

  8. 【leetcode】Search in Rotated Sorted Array

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  9. 方法重写和方法重载;this关键字和super关键字

    1:方法重写和方法重载的区别?方法重载能改变返回值类型吗? 方法重写: 在子类中,出现和父类中一模一样的方法声明的现象. 方法重载: 同一个类中,出现的方法名相同,参数列表不同的现象. 方法重载能改变 ...

  10. Struts2常用标签

    Struts2常用标签总结 一 介绍 1.Struts2的作用 Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性.实现了更好的 ...