一 、 旋轉(zhuǎn)菜單的需要的知識(shí)點(diǎn)
重寫 UICollectionView 的布局類UICollectionViewLayout 。
重寫UICollectionView 的觸摸方法。
余弦定理
二 、 詳細(xì)方法的介紹和展示
1、 重寫 UICollectionView 的布局類需要重寫下面幾個(gè)方法
override func prepare() :: 初始化 Items 的函數(shù)。初始化之前先調(diào)用父類的該方法(super.prepare())。
override func layoutAttributesForElements(in rect: CGRect) :: 返回 Items 的所有 UICollectionViewLayoutAttributes 對(duì)象。
override var collectionViewContentSize: CGSize :: 設(shè)置UICollectionView 的視圖范圍。
1> ** prepare()** 的函數(shù)重寫
override func prepare() {
super.prepare()
// TODO: 獲取圓盤上Item的個(gè)數(shù)
let itemCount = self.collectionView?.numberOfItems(inSection: 0)
// TODO: 獲取圓盤的半徑(獲取CollectionView的寬和高的最小的一個(gè))
let diskRadii = min((self.collectionView?.bounds.width)!, (self.collectionView?.bounds.height)!) * 0.5
// TODO: 計(jì)算圓盤的圓心
let diskCenterPoint = CGPoint.init(x: (self.collectionView?.bounds.width)! * 0.5, y: (self.collectionView?.bounds.height)! * 0.5)
// TODO: 檢測(cè)CollectionVie的總高度是否小于默認(rèn)的ItemRadii高度
ItemRadii = diskRadii > 40 ? ItemRadii:diskRadii
// TODO: 計(jì)算每一個(gè)Item的位置和大小
for i in 0..<itemCount! {
// TODO: 獲取每個(gè)Item系統(tǒng)的 LayoutAttribute
let layoutAttribute = UICollectionViewLayoutAttributes.init(forCellWith: IndexPath.init(row: i, section: 0))
// TODO: 設(shè)置大小
layoutAttribute.size = CGSize.init(width: ItemRadii * 2, height: ItemRadii * 2)
// TODO: 計(jì)算Item中心距離圓盤的中心距離
let disDiff = diskRadii - ItemRadii
// TODO: 計(jì)算每個(gè)Items的中心位置
let itemCenterX = diskCenterPoint.x + cos(CGFloat(2 * .pi/CGFloat(itemCount!) * CGFloat(i))+rotationAngle) * disDiff
let itemCenterY = diskCenterPoint.y + sin(2 * .pi / CGFloat(itemCount!) * CGFloat(i)+rotationAngle) * disDiff
// TODO: 設(shè)置Item的中心
layoutAttribute.center = CGPoint.init(x: itemCenterX, y: itemCenterY)
// TODO: 存儲(chǔ)Item的layoutAttributes
LayoutAttributes.add(layoutAttribute)
}
}
2> layoutAttributesForElements(in rect: CGRect) 函數(shù)的重寫
// MARK: Item的layoutAttribute屬性的返回
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return (LayoutAttributes as! [UICollectionViewLayoutAttributes])
}
3> collectionViewContentSize: CGSize 函數(shù)的重寫
// MARK: 設(shè)置圓盤的大小
override var collectionViewContentSize: CGSize {
get {
return (self.collectionView?.frame.size)!
}
set {
self.collectionViewContentSize = newValue
}
}
2、 重寫UICollectionView 的觸摸方法
在重寫UICollectionView 的觸摸方法時(shí),由于Swift 對(duì) UICollectionView 對(duì)類的擴(kuò)展需要使用到 OC ,還要橋接文件。本簡(jiǎn)書就使用繼承來解決這個(gè)問題。我們要重寫的UICollectionView觸摸的方法如下:
-
func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
通過該方法,你可以限制觸摸的范圍,最重要的是記錄開始觸摸的一點(diǎn)。
-
func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
觸摸后手勢(shì)的移動(dòng),處理圓盤的轉(zhuǎn)動(dòng)。這里需要注意:有一個(gè)角度的計(jì)算。使用到的知識(shí)就是 三角函數(shù)的 余弦定理。
1> touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) 的函數(shù)的重寫
// MARK: 設(shè)置觸摸起始點(diǎn)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// 獲取觸摸的點(diǎn)
let touchPoint = touches.first?.location(in: self)
lastPoint = touchPoint!
}
2> **func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) **的重寫函數(shù)
// MARK: 移動(dòng)旋轉(zhuǎn)處理
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let discCenter = CGPoint.init(x: self.bounds.width * 0.5, y: self.bounds.height * 0.5)
// 獲取觸摸的點(diǎn)
let touchPoint = touches.first?.location(in: self)
// 計(jì)算滑動(dòng)的角度
let rads = self.computingAngle(startPoint1: lastPoint, endPoint1: touchPoint!, discCenter: discCenter)
totalRads += rads
let FlowLayout = self.collectionViewLayout as! RotatingMenuViewLayout
FlowLayout.rotationAngle = totalRads * rotationRate
// CollectionView重新布局
FlowLayout.invalidateLayout()
// 更新最后的一點(diǎn)
lastPoint = touchPoint!
}
3> 角度計(jì)算函數(shù)
// MARK: 計(jì)算移動(dòng)的角度
func computingAngle(startPoint1:CGPoint,endPoint1:CGPoint,discCenter:CGPoint) -> CGFloat {
// 計(jì)算開始點(diǎn)和結(jié)束點(diǎn)距離中心點(diǎn)的長度
let a = startPoint1.x - discCenter.x
let b = startPoint1.y - discCenter.y
let c = endPoint1.x - discCenter.x
let d = endPoint1.y - discCenter.y
// 使用余弦定理
let shang = a*c + b*d
let yu = sqrt(a * a + b * b) * sqrt(c*c + d*d)
// 使用反余弦求得角度
let rads = acos( Double(shang) / Double(yu))
var plusminus = -1
if (endPoint1.x - startPoint1.x < 0 && d > 0) {
plusminus = 1
}else if endPoint1.y - startPoint1.y < 0 && a < 0 {
plusminus = 1
}else if endPoint1.x - startPoint1.x > 0 && d < 0 {
plusminus = 1
}else if endPoint1.y - startPoint1.y > 0 && a > 0 {
plusminus = 1
}else if endPoint1.x - startPoint1.x > 0 && d > 0 {
plusminus = -1
}else if a > 0 && endPoint1.y - startPoint1.y < 0 {
plusminus = -1
}else if endPoint1.x - startPoint1.x < 0 && d < 0 {
plusminus = -1
} else if endPoint1.y - startPoint1.y > 0 && a < 0{
plusminus = -1
}
// 轉(zhuǎn)化成角度
return CGFloat(rads) * CGFloat(plusminus)
}
這里有帶優(yōu)化,目前也是最接近完美的一種方法處理圓盤的旋轉(zhuǎn)方向避免不要的卡頓。
三 、 旋轉(zhuǎn)菜單的創(chuàng)建
1> 創(chuàng)建UICollectionView 對(duì)象。
// MARK: 創(chuàng)建UICollectionView
func createCollectionView() {
let FlowLayout = RotatingMenuViewLayout.init()
FlowLayout.ItemRadii = 40
FlowLayout.rotationAngle = .pi
let CollectionView = RotatingMenuCollectionView.init(frame: CGRect.init(x: 0, y: 100, width: view.bounds.width, height: 400), collectionViewLayout: FlowLayout)
CollectionView.delegate = self
CollectionView.dataSource = self
self.view.addSubview(CollectionView)
CollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "NetWork小賤")
// 添加中圖片
let centerImageV = UIImageView.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: CollectionView.bounds.height - 240, height: CollectionView.bounds.height - 240)))
centerImageV.center = CGPoint.init(x: CollectionView.bounds.width * 0.5, y: CollectionView.bounds.height * 0.5)
centerImageV.image = UIImage.init(named: "taiji.png")
centerImageV.contentMode = .scaleAspectFill
CollectionView.addSubview(centerImageV)
}
2> 代理方法的實(shí)現(xiàn)
// MARK: 代理
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (dataArray?.count)!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NetWork小賤", for: indexPath)
Cell.contentView.backgroundColor = UIColor.red
Cell.contentView.layer.cornerRadius = Cell.contentView.bounds.width * 0.5
// 添加標(biāo)記
Cell.tag = indexPath.row
// 添加一個(gè)手勢(shì)
let tap = UITapGestureRecognizer.init(target: self, action: #selector(itemDidSelecd(_ :)))
Cell.addGestureRecognizer(tap)
// 標(biāo)記
for item in Cell.contentView.subviews {
item.removeFromSuperview()
}
let markL = UILabel.init(frame: Cell.bounds)
markL.text = dataArray![indexPath.row]
markL.font = UIFont.boldSystemFont(ofSize: 30)
markL.textAlignment = .center
Cell.contentView.addSubview(markL)
return Cell
}
// 由于Cell的點(diǎn)擊觸發(fā)事件
func itemDidSelecd(_ tap:UITapGestureRecognizer) {
let AlertV = UIAlertController.init(title: nil, message: dataArray![(tap.view?.tag)!], preferredStyle: .alert)
let sureAction = UIAlertAction.init(title: "確定", style: .cancel, handler: nil)
AlertV.addAction(sureAction)
self.present(AlertV, animated: true, completion: nil)
}
五、最終的效果展示

4.gif