IOS之笑脸app
ios笑臉app實現(xiàn)
import UIKit@IBDesignable class FaceView: UIView {@IBInspectablevar lineWidth:CGFloat=3{didSet{setNeedsLayout()}}@IBInspectablevar color:UIColor = UIColor.blueColor(){didSet{setNeedsLayout()}}@IBInspectablevar 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半徑)的比率,次數(shù)越小,小圓越大.因為 下面bezierPathForEye的方法中定義 eyeRadius = faceRadius / Scaling.FaceRadiusToEyeRadiusRatiostatic let FaceRadiusToEyeOffsetRatio: CGFloat = 3//大圓與小圓的偏移率,此數(shù)越大,小圓的圓心距大圓越近static let FaceRadiusToEyeSeparationRatio: CGFloat = 1.5//兩個小圓之間在大圓內(nèi)的分離比率static let FaceRadiusToEyeMounthWidthRatio: CGFloat = 1static let FaceRadiusToEyeMounthHeightRatio: CGFloat = 3static let FaceRadiusToEyeMounthOffsetRatio: CGFloat = 3}private enum Eye {case Left , Right}private func bezierPathForEye(whichEye: Eye) -> UIBezierPath {//此處定義的方法為設(shè)置一只眼睛的位置,上面定義了左右眼的枚舉,可通過調(diào)用.Left.Right來實現(xiàn)兩個位置的設(shè)定let eyeRadius = faceRadius / Scaling.FaceRadiusToEyeRadiusRatio//定義小圓半徑是大圓半徑的幾分之幾,此處因為FaceRadiusToEyeRadiusRatio: CGFloat = 10 故為十分之一let eyeVerticalOffset = faceRadius / Scaling.FaceRadiusToEyeOffsetRatio//小圓的垂直偏距let eyeHorizontalSeparation = faceRadius / Scaling.FaceRadiusToEyeSeparationRatio//小圓的水平距離var eyeCenter = faceCentereyeCenter.y -= eyeVerticalOffset//此處相當(dāng)于是用大圓圓心的y坐標(biāo)減去小圓圓心的y坐標(biāo),故小圓圓心在大圓圓心之上.若為加,則在下switch whichEye {case .Left: eyeCenter.x -= eyeHorizontalSeparation / 2//相當(dāng)于大圓圓心的x坐標(biāo)減去(小圓圓心的x坐標(biāo)除以2),即在大圓圓心的左側(cè)case .Right: eyeCenter.x += eyeHorizontalSeparation / 2//此處加,即在右側(cè)}let path = UIBezierPath(arcCenter: eyeCenter, radius: eyeRadius, startAngle: 0, endAngle: CGFloat(2*M_PI), clockwise: true) //畫圓path.lineWidth = lineWidth //設(shè)定線寬return path}private func bezierPathForSmile(fractionOfMaxSmile: Double) -> UIBezierPath {let mouthWidth = faceRadius / Scaling.FaceRadiusToEyeMounthWidthRatio//大圓半徑與線寬的比率,此處線寬=大圓半徑let mouthHeight = faceRadius / Scaling.FaceRadiusToEyeMounthHeightRatio//mouthHeight即線的中點到圓心的距離let mouthVerticalOffset = faceRadius / Scaling.FaceRadiusToEyeMounthOffsetRatiolet smileHeight = CGFloat(max(min(fractionOfMaxSmile, 1), -1)) * mouthHeight//此處max(min(fractionOfMaxSmile, 1), -1)限定了笑臉指數(shù)只能在-1到1之間,fractionOfMaxSmile這個參數(shù)可以自行設(shè)定,如果設(shè)定的大于1,則只取1,設(shè)定小于-1,則只取-1let start = CGPoint(x: faceCenter.x - mouthWidth / 2, y: faceCenter.y + mouthVerticalOffset) //設(shè)置起點let end = CGPoint(x: start.x + mouthWidth, y: start.y) //設(shè)置終點let cp1 = CGPoint(x: start.x + mouthWidth / 3 , y: start.y + smileHeight) //設(shè)置曲線點1,此處mouthWidth / 3用于調(diào)節(jié)曲線的弧度let cp2 = CGPoint(x: end.x - mouthWidth / 3, y: cp1.y) //設(shè)置曲線點2let path = UIBezierPath()path.moveToPoint(start)path.addCurveToPoint(end, controlPoint1: cp1, controlPoint2: cp2)path.lineWidth = lineWidthreturn path}override func drawRect(rect: CGRect) {let facePath=UIBezierPath(arcCenter: faceCenter, radius: faceRadius, startAngle: 0, endAngle: CGFloat(2*M_PI), clockwise: true)facePath.lineWidth=lineWidthcolor.set()facePath.stroke()bezierPathForEye(.Left).stroke()bezierPathForEye(.Right).stroke()let smiliness = 0.8let smilePath = bezierPathForSmile(smiliness)smilePath.stroke()} }- IBDesignable可以在storyboard中看到自定義的uiview
- IBInspectable使屬性可以改變
利用協(xié)議與代理聯(lián)結(jié)數(shù)據(jù)源
protocol FaceViewDataSource:class {func smilnessForFaceView(sender:FaceView)->Double? }手勢識別實現(xiàn)縮放與改變笑臉弧度
@IBOutlet weak var faceView: FaceView!{didSet{faceView.dataSource=selffaceView.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:fallthroughcase .Changed:let translation=sender.translationInView(faceView)let happinessChange = -Int(translation.y/Constants.HappinessGestureScale)if happinessChange != 0{happiness+=happinessChangesender.setTranslation(CGPoint.zero, inView: faceView)}default:break}}func scale(gesture:UIPinchGestureRecognizer){if gesture.state == .Changed{scale*=gesture.scalegesture.scale=1}}源代碼:Happiness
總結(jié)
- 上一篇: Redis配置文件常用配置消息解说--版
- 下一篇: Struts2基础知识