使用贝赛尔曲线画扇形、圆形、弧线、多边形,实现App下载时的动画效果demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | // // MyView.swift // TestUIBezierPath // // Created by iCodeWoods on 16/5/8. // Copyright © 2016年 iCodeWoods. All rights reserved. // import Foundation import UIKit class MyView : UIView { override init ( frame : CGRect ) { super . init ( frame : frame ) backgroundColor = UIColor . grayColor () } required init ?( coder aDecoder : NSCoder ) { fatalError ( "init(coder:) has not been implemented" ) } // // 五边形 // override func drawRect(rect: CGRect) { // let color = UIColor.redColor() // color.set() // 设置线条颜色 // // let aPath = UIBezierPath() // // aPath.lineWidth = 5.0 // 线条宽度 // aPath.lineCapStyle = .Round // 线条拐角 // aPath.lineJoinStyle = .Round // 终点处理 // // // Set the starting point of the shape. // aPath.moveToPoint(CGPointMake(100, 10)) // // // Draw the lines // aPath.addLineToPoint(CGPointMake(200, 50)) // aPath.addLineToPoint(CGPointMake(160, 150)) // aPath.addLineToPoint(CGPointMake(40, 140)) // aPath.addLineToPoint(CGPointMake(10, 60)) // aPath.closePath() // 最后一条线通过调用closePath方法得到 // //// aPath.stroke() // Draws line 根据坐标点连线,不填充 // aPath.fill() // Draws line 根据坐标点连线,填充 // } // // 矩形 // override func drawRect(rect: CGRect) { // let color = UIColor.redColor() // color.set() // 设置线条颜色 // // let aPath = UIBezierPath(rect: CGRectMake(40, 40, 100, 50)) // 长方形 //// let aPath = UIBezierPath(rect: CGRectMake(40, 40, 100, 100)) // 正方形 // // aPath.lineWidth = 5.0 // 线条宽度 // aPath.lineCapStyle = .Round // 线条拐角 // aPath.lineJoinStyle = .Round // 终点处理 // // aPath.stroke() // Draws line 根据坐标点连线,不填充 //// aPath.fill() // Draws line 根据坐标点连线,填充 // } // // 圆、椭圆 // override func drawRect(rect: CGRect) { // let color = UIColor.redColor() // color.set() // 设置线条颜色 // // // 根据传人的矩形画出内切圆/椭圆 //// let aPath = UIBezierPath(ovalInRect: CGRectMake(40, 40, 100, 100)) // 如果传入的是正方形,画出的就是内切圆 // let aPath = UIBezierPath(ovalInRect: CGRectMake(40, 40, 100, 160)) // 如果传入的是长方形,画出的就是内切椭圆 // // aPath.lineWidth = 5.0 // 线条宽度 // //// aPath.stroke() // Draws line 根据坐标点连线,不填充 // aPath.fill() // Draws line 根据坐标点连线,填充 // } //// 弧线 // override func drawRect(rect: CGRect) { // let color = UIColor.redColor() // color.set() // 设置线条颜色 // // let aPath = UIBezierPath(arcCenter: CGPointMake(150, 150), radius: 75, startAngle: 0, endAngle: (CGFloat)(90*M_PI/180), clockwise: true) // // aPath.lineWidth = 5.0 // 线条宽度 // //// aPath.stroke() // Draws line 根据坐标点连线,不填充 // aPath.fill() // Draws line 根据坐标点连线,填充 // } // // 扇形 // override func drawRect(rect: CGRect) { // let color = UIColor.redColor() // color.set() // 设置线条颜色 // // let aPath = UIBezierPath(arcCenter: CGPointMake(150, 150), radius: 75, startAngle: 0, endAngle: (CGFloat)(90*M_PI/180), clockwise: true) // aPath.addLineToPoint(CGPointMake(150, 150)) // aPath.closePath() // aPath.lineWidth = 5.0 // 线条宽度 // // // aPath.stroke() // Draws line 根据坐标点连线,不填充 // aPath.fill() // Draws line 根据坐标点连线,填充 // } // 实现 App 下载时的效果 var beginAngle = M_PI * 3 / 2 // 起点 var finishAngle = M_PI * 3 / 2 + M_PI * 2 / 20 // 终点 override func drawRect ( rect : CGRect ) { let color = UIColor . whiteColor () color . set () // 设置线条颜色 let aPath = UIBezierPath ( arcCenter : CGPointMake ( 150 , 150 ), radius : 75 , startAngle : ( CGFloat )( beginAngle ), endAngle : ( CGFloat )( finishAngle ), clockwise : true ) aPath . addLineToPoint ( CGPointMake ( 150 , 150 )) aPath . closePath () aPath . lineWidth = 5.0 // 线条宽度 // aPath.fill() // Draws line 根据坐标点连线,填充 aPath . fillWithBlendMode (. Normal , alpha : 0.5 ) finishAngle += M_PI / 20 // 更新终点 } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2015-10-14 最简单也最难——如何获取到Android控件的高度
2015-10-14 Android新增API之AudioEffect中文API与应用实例
2013-10-14 Android之动态改变控件大小
2013-10-14 android适配各种分辨率的问题