前言

使用Swift实现的转盘菜单,主要用到UIBezierPathCALayer遮罩绘制扇形UIViewCATransform3DMakeRotation实现旋转动画。代码设计使用默认configureCallback回调方便创建和设置基本属性,参考UITableView代理和数据源模式,支持AutoLayoutFrame

效果图

1.遮罩绘制扇形View

计算扇形曲线位置,通过CALayermask属性绘制出扇形UIView

核心代码

func setMaskLayer(_ startAngle: CGFloat, endAngle: CGFloat) {
let center = CGPoint(x: bounds.width * 0.5, y: bounds.height * 0.5)
let layer = CAShapeLayer()
path.addArc(withCenter: center, radius: bounds.width * 0.5, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.addLine(to: center)
layer.path = path.cgPath
layer.rasterizationScale = UIScreen.main.scale
layer.shouldRasterize = true
self.layer.mask = layer
}

2.中间镂空

func createHole(in view : UIView, radius: CGFloat)   {
let path = CGMutablePath()
path.addArc(center: view.center, radius: radius, startAngle: 0.0, endAngle: 2.0 * .pi, clockwise: true)
path.addRect(CGRect(origin: .zero, size: view.bounds.size))
let maskLayer = CAShapeLayer()
maskLayer.path = path
maskLayer.fillRule = .evenOdd
view.layer.mask = maskLayer
view.clipsToBounds = true
}

3.旋转动画

添加UIPanGestureRecognizerUITapGestureRecognizer手势,根据手势位置使用atan2函数计算旋转角度,然后用CATransform3DMakeRotation围绕Z轴旋转做动画

核心代码

func handlePanGesture(_ sender: UIPanGestureRecognizer) {
let location = sender.location(in: self)
switch sender.state {
case .began:
startPoint = location
case .changed:
let radian1 = -atan2(startPoint.x - menuLayerView.center.x, startPoint.y - menuLayerView.center.y)
let radian2 = -atan2(location.x - menuLayerView.center.x, location.y - menuLayerView.center.y)
menuLayerView.transform = menuLayerView.transform.rotated(by: radian2 - radian1)
startPoint = location
default:
let angle = 2 * CGFloat(Double.pi) / CGFloat(cells.count)
var menuViewAngle = atan2(menuLayerView.transform.b, menuLayerView.transform.a)
if menuViewAngle < 0 {
menuViewAngle += CGFloat(2 * Double.pi)
}
var index = cells.count - Int((menuViewAngle + CGFloat(Double.pi / 4)) / angle)
if index == cells.count {
index = 0
}
setSelectedIndex(index, animated: true)
}
}
func handleTapGesture(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: menuLayerView)
for (index, cell) in cells.enumerated() {
if cell.path.contains(location) {
setSelectedIndex(index, animated: true)
}
}
}

4.弹出收起动画

func openMenuView(withAnimate animate: Bool = true) {
openMenu = true
UIView.animate(withDuration: animate ? configure.animationDuration : 0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5.0, options: .curveEaseInOut) {
self.centerButton.transform = CGAffineTransform(rotationAngle: .pi * -0.5)
self.centerButton.setImage(self.configure.closeImage, for: .normal)
self.menuLayerView.transform = CGAffineTransform(scaleX: 1, y: 1).rotated(by: self.currentAngle)
}
}
func closeMenuView(withAnimate animate: Bool = true) {
openMenu = false
let scale = (configure.centerRadius * 2) / bounds.width
UIView.animate(withDuration: animate ? configure.animationDuration : 0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5.0, options: .curveEaseInOut) {
self.centerButton.transform = .identity
self.centerButton.setImage(self.configure.openImage, for: .normal)
self.menuLayerView.transform = CGAffineTransform(scaleX: scale, y: scale).rotated(by: self.currentAngle)
}
}

5.内部细节

考虑到方便布局和使用,内部使用UIView叠加旋转实现,这里也可以采用Layer直接绘制实现,相对UIView,层次结构会简单很多

总结

核心代码已经贴出,完整代码请查看----->>>CLDemo,如果对你有所帮助,欢迎Star。

最新文章

  1. 即时聊天IM之四 Android客户端IM帮助类编写
  2. jQuery 学习之路(4):事件
  3. Java学习-048-插件应用之 Find Bugs
  4. iscroll 使用及遇到的问题
  5. Linux6(5)安装Oracle Rac11g
  6. Java 操作Excel 之Poi(第一讲)
  7. NHibernate分页
  8. 杭电 2034 人见人爱A-B
  9. c#基础语言编程-正则表达式基础
  10. 使用jekyll主题
  11. SQL SERVER 2008 R2 自动备份并删除过期备份数据
  12. Tomcat启动报Error listenerStart错误
  13. virus.win32.parite.H查杀病毒的方法
  14. 上curl java 模拟http请求
  15. CCF系列之矩阵(201512-5)
  16. Scala 开发遇到的坑
  17. window.location.replace和window.location.href的区别
  18. python之N阶乘结果末尾有几个0
  19. m3u8转码
  20. memory prefix inter,intra,intro,iso out 5

热门文章

  1. SpringMVC(7)格式化显示
  2. python logger 动态设置日志名
  3. ctf杂项之easy_nbt
  4. IP地址详解
  5. win10 删除WPS提示存在wpsupdate.exe进程
  6. python twain模块
  7. window对象之计时器--v客学院技术分享
  8. 微信小程序云开发-云函数-数据库和云函数获取数据的区别
  9. 【LOJ 109 并查集】 并查集
  10. 单点登录详解(token简述)(七)