温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS开发实现转盘菜单Swift

发布时间:2021-06-22 16:10:54 来源:亿速云 阅读:177 作者:chen 栏目:编程语言

本篇内容主要讲解“iOS开发实现转盘菜单Swift”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“iOS开发实现转盘菜单Swift”吧!

前言

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

效果图

iOS开发实现转盘菜单Swift

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,层次结构会简单很多

到此,相信大家对“iOS开发实现转盘菜单Swift”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI