Quartz 2D绘制曲线示例2

Quartz 2D 的曲线绘图挺好用,尤其是用CGContextAddCurveToPoint()CGContextAddQuadCurveToPoint()两个函数,加起来就能把一条曲线画得又顺又自然。前者是三阶贝塞尔,后者是二阶,控制点数量不一样,用的时候注意下就行。

像你要在drawRect里搞点花活,比如画个动态折线图、圆滑进度条,用这俩 API 就蛮顺手。路径画完别忘了CGContextStrokePath(),不然啥都看不见,别问我怎么知道的,踩过坑哦。

顺手推荐几个相关资源,想研究贝塞尔曲线的朋友可以看看:贝塞尔曲线绘图》讲得挺清楚,还有个demo可以直接跑跑看效果。如果你做的是 iOS 动画,也别错过贝塞尔曲线绘制进度条动画》这个例子,灵感多多。

写代码的时候建议你先理清楚每个点的位置,尤其是控制点。不然线条不按你想的走,会挺抓狂。二阶那种只要一个控制点,好理解,三阶的就得多试几次。用UIBezierPath封一下其实更方便。

如果你是跨平台或者搞游戏方向的,也有 Unity、QT、VB 等版本的资料,比如Unity3D 里的贝塞尔解析QT 画曲线,基本思路都差不多,API 名字不一样罢了。

,这套 Quartz 2D 的曲线函数,写起来不复杂,控制力也比较强。你要是想做点花里胡哨但用户体验好的图形展示,用它还挺合适。

zip
CurveTest.zip 预估大小:24个文件
folder
CurveTest 文件夹
file
.DS_Store 6KB
folder
CurveTest.xcodeproj 文件夹
folder
project.xcworkspace 文件夹
file
contents.xcworkspacedata 154B
folder
xcuserdata 文件夹
folder
shuju.xcuserdatad 文件夹
file
WorkspaceSettings.xcsettings 332B
file
UserInterfaceState.xcuserstate 15KB
folder
yeeku.xcuserdatad 文件夹
file
UserInterfaceState.xcuserstate 20KB
file
project.pbxproj 13KB
folder
xcuserdata 文件夹
folder
shuju.xcuserdatad 文件夹
folder
xcschemes 文件夹
file
CurveTest.xcscheme 3KB
file
xcschememanagement.plist 481B
folder
yeeku.xcuserdatad 文件夹
folder
xcschemes 文件夹
file
CurveTest.xcscheme 3KB
file
xcschememanagement.plist 481B
folder
CurveTest 文件夹
file
main.m 360B
file
FKAppDelegate.m 2KB
file
FKViewController.h 248B
file
CurveTest-Prefix.pch 315B
file
FKCurveView.h 228B
folder
Base.lproj 文件夹
file
Main.storyboard 2KB
file
FKCurveView.m 975B
file
CurveTest-Info.plist 1KB
folder
Images.xcassets 文件夹
folder
AppIcon.appiconset 文件夹
file
Contents.json 585B
folder
LaunchImage.launchimage 文件夹
file
Contents.json 689B
folder
en.lproj 文件夹
file
InfoPlist.strings 45B
file
FKAppDelegate.h 310B
file
FKViewController.m 378B
file
FKContext.h 3KB
zip 文件大小:42.08KB