【问题标题】:iOS: Curverd AppTabBar with animated UITabBarItemiOS:带有动画 UITabBarItem 的 Curverd AppTabBar
【发布时间】:2021-05-06 18:00:52
【问题描述】:

我正在开发一个 iOS 应用程序,该应用程序需要在 UITabBarItem 下显示一条曲线,如显示

我遵循了一些教程,并且能够访问使我的 UITabBar 弯曲的代码,

import Foundation
import  UIKit
class AppTabBar: UITabBar {
    var curvePos : CGFloat =  0
    private var shapeLayer: CALayer?
    override func draw(_ rect: CGRect) {
        curvePos = rect.width / 2
        self.addShape(rect: rect)
    }
    
    private func addShape(rect: CGRect) {
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = createPath(in: rect)
        shapeLayer.strokeColor = UIColor.lightGray.cgColor
        shapeLayer.fillColor = #colorLiteral(red: 0.9782002568, green: 0.9782230258, blue: 0.9782107472, alpha: 1)
        shapeLayer.lineWidth = 0.5
        shapeLayer.shadowOffset = CGSize(width:0, height:0)
        shapeLayer.shadowRadius = 10
        shapeLayer.shadowColor = UIColor.gray.cgColor
        shapeLayer.shadowOpacity = 0.3
        if let oldShapeLayer = self.shapeLayer {
            self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
        } else {
            self.layer.insertSublayer(shapeLayer, at: 0)
        }
        self.shapeLayer = shapeLayer
    }

    func createPath(in rect: CGRect) -> CGPath {
        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: 0, y: rect.height))
        path.addLine(to: CGPoint(x: rect.width, y: rect.height))
        path.addLine(to: CGPoint(x: rect.width, y: 0))
        // adding Curve...
        path.move(to: CGPoint(x: curvePos + 40, y: 0))
        path.addQuadCurve(to: CGPoint(x: curvePos - 40, y: 0), controlPoint: CGPoint(x: curvePos, y: 70))
        return path.cgPath
    }
}

我现在需要的: 1-如何在点击UITabBar 的每个项目以在所选项目下制作曲线时更改curvePos 值。 2- 我如何为UITabBarItem 或 UITabBarItem 图像设置动画以使其如上图所示。 任何帮助将不胜感激。

【问题讨论】:

标签: ios xcode storyboard swift5 tabbar


【解决方案1】:

enter image description here

import UIKit

class MainViewController: UIViewController, UITabBarDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        
        switch item.title! {
        case "Home":
            //let oldShapeLayer = CAShapeLayer()
            //let shapeLayer = CAShapeLayer()
            //tabBar.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
            break
            
        case "Gallery" :
            
            break
            
        case "Settings" :
            
            break
            
        default: break
            
        }
        
    }
    
}

【讨论】:

    【解决方案2】:

    经过多次尝试,我可以得到我想做的事情: 波纹管修改了我的 AppTabBar 的代码:

    class AppTabBar: UITabBar {
        var curvePos : CGFloat =  0
        private var shapeLayer: CALayer?
        override func draw(_ rect: CGRect) {
            let itemIndex = CGFloat(self.items!.firstIndex(of: selectedItem!)!)
            let itemWidth = self.frame.width / CGFloat(self.items!.count)
            curvePos = (itemWidth / 2) + (itemWidth * itemIndex)
            self.addShape(rect: rect)
        }
    
        private func addShape(rect: CGRect) {
            let shapeLayer = CAShapeLayer()
            shapeLayer.path = createPath(in: rect)
            shapeLayer.strokeColor = UIColor.lightGray.cgColor
            shapeLayer.fillColor = #colorLiteral(red: 0.9782002568, green: 0.9782230258, blue: 0.9782107472, alpha: 1)
            shapeLayer.lineWidth = 0.5
            shapeLayer.shadowOffset = CGSize(width:0, height:0)
            shapeLayer.shadowRadius = 10
            shapeLayer.shadowColor = UIColor.gray.cgColor
            shapeLayer.shadowOpacity = 0.3
    
            if let oldShapeLayer = self.shapeLayer {
                self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
            } else {
                self.layer.insertSublayer(shapeLayer, at: 0)
            }
            self.shapeLayer = shapeLayer
        }
    
        func createPath(in rect: CGRect) -> CGPath {
            let path = UIBezierPath()
            path.move(to: CGPoint(x: 0, y: 0))
            path.addLine(to: CGPoint(x: 0, y: rect.height))
            path.addLine(to: CGPoint(x: rect.width, y: rect.height))
            path.addLine(to: CGPoint(x: rect.width, y: 0))
            
            // adding Curve...
            path.move(to: CGPoint(x: curvePos + 40, y: 0))
            
            path.addQuadCurve(to: CGPoint(x: curvePos - 40, y: 0), controlPoint: CGPoint(x: curvePos, y: 70))
            return path.cgPath
        }
    }
    

    并为 UITabBarController 创建另一个类,如下所示:

    class CustomTabBarController:  UITabBarController, UITabBarControllerDelegate    {
        override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
           print("sssss")
            tabBar.setNeedsDisplay()
        }
    }
    

    CustomTabBarController 设置为TabBarController,将AppTabBar 设置为UITabBar

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-01
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      相关资源
      最近更新 更多