您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“iOS開發(fā)實(shí)現(xiàn)轉(zhuǎn)盤菜單Swift”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“iOS開發(fā)實(shí)現(xiàn)轉(zhuǎn)盤菜單Swift”吧!
使用Swift實(shí)現(xiàn)的轉(zhuǎn)盤菜單,主要用到UIBezierPath
、CALayer
遮罩繪制扇形UIView
,CATransform3DMakeRotation
實(shí)現(xiàn)旋轉(zhuǎn)動畫。代碼設(shè)計使用默認(rèn)configureCallback
回調(diào)方便創(chuàng)建和設(shè)置基本屬性,參考UITableView
代理和數(shù)據(jù)源模式,支持AutoLayout
和Frame
。
計算扇形曲線位置,通過CALayer
的mask
屬性繪制出扇形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 }
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 }
添加UIPanGestureRecognizer
、UITapGestureRecognizer
手勢,根據(jù)手勢位置使用atan2
函數(shù)計算旋轉(zhuǎn)角度,然后用CATransform3DMakeRotation
圍繞Z
軸旋轉(zhuǎn)做動畫 核心代碼
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) } } }
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) } }
考慮到方便布局和使用,內(nèi)部使用UIView
疊加旋轉(zhuǎn)實(shí)現(xiàn),這里也可以采用Layer
直接繪制實(shí)現(xiàn),相對UIView
,層次結(jié)構(gòu)會簡單很多
到此,相信大家對“iOS開發(fā)實(shí)現(xiàn)轉(zhuǎn)盤菜單Swift”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。