音乐程序的 TabBarController

Posted

技术标签:

【中文标题】音乐程序的 TabBarController【英文标题】:TabBarController for music program 【发布时间】:2017-12-28 12:04:57 【问题描述】:

我使用tabBarController 创建了一个音乐程序,但我有一些问题,如 gif 所示

问题:

    如何做到这一点,当您点击 tabBarItem 时,“presentViewController”工作

    怎么做才能让照片不变色并变圆,只在第三个tabBarItem

    最好没有库

应该是

我的 TabBarController

override func viewDidLoad() 
     super.viewDidLoad()

    self.delegate = self
    
    // меняет цвет фона tabBar
    self.tabBar.barTintColor = .white
    
    // меняет цвет UITabBarItem and Title
    UITabBar.appearance().tintColor = UIColor(hex: 0x0077fe, alpha: 1)
    
    //меняет цвет background UITabBar
    UITabBar.appearance().barTintColor = UIColor.white
    
    
    // делает фон серым
    for item in self.tabBar.items! 
        if let image = item.image 
            item.image = image.withRenderingMode(.alwaysOriginal)

        
    
    
    //показывает и переходит в контроллеры
    let storyBoard = UIStoryboard(name: "Main", bundle:nil)
    let controller1 = storyBoard.instantiateViewController(withIdentifier: "main") as! VCMain
    let controller2 = storyBoard.instantiateViewController(withIdentifier: "search")
    let controller3 = storyBoard.instantiateViewController(withIdentifier: "player")
    let controller4 = storyBoard.instantiateViewController(withIdentifier: "bookmark")
    let controller5 = storyBoard.instantiateViewController(withIdentifier: "menu")
    
    self.setViewControllers([controller1,controller2,controller3,controller4,controller5], animated: true)
    
    // создает навигационный контроллер для контроллеров
    let vc1 = UINavigationController(rootViewController: controller1)
    let vc2 = UINavigationController(rootViewController: controller2)
    let vc3 = UINavigationController(rootViewController: controller3)
    let vc4 = UINavigationController(rootViewController: controller4)
    let vc5 = UINavigationController(rootViewController: controller5)
    
    viewControllers = [vc1, vc2, vc3, vc4, vc5]
    


override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) 
    
    print(item.tag)
    if item.tag == 0
        if GlobalModals.count != 0 
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "player") as? VCPlayer
            self.present(vc!, animated: true, completion: nil)
        
    

播放器

override func viewDidLoad() 
    super.viewDidLoad()
  
        let im = Extension.resizeImage(image:GlobalModals[thisSong].ImageView! , targetSize: CGSize.init(width:20, height: 20))
        self.tabBarController?.tabBar.items![2].image = im
    
 

【问题讨论】:

【参考方案1】:

TabBarController 没有这些选项,需要通过子类来实现。

您可以使用这个库Animated Tab Bar 来实现与动画相同的结果。

【讨论】:

我不想要动画图标【参考方案2】:

我在 tabBarController 中创建了一个视图和一个按钮,并创建了一个设置参数并在设置完所有视图控制器后调用它的函数,还创建了一个可以从另一个控制器更改按钮图像的 notificationCenter

class TabBarController: UITabBarController,UITabBarControllerDelegate 


open var playerBtn = UIButton()

//func for NotificationCenter
@objc func imageChange() 
    if GlobalModals.count != 0 
        playerBtn.setImage(GlobalModals[thisSong].ImageView, for: .normal)
    


override func viewDidLoad() 
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(imageChange), name: NSNotification.Name(rawValue: "imageChange"), object: nil)

    self.delegate = self

    //shows and goes to controllers
    let storyBoard = UIStoryboard(name: "Main", bundle:nil)
    let controller1 = storyBoard.instantiateViewController(withIdentifier: "main") as! VCMain
    let controller2 = storyBoard.instantiateViewController(withIdentifier: "search")
    let controller3 = storyBoard.instantiateViewController(withIdentifier: "player")
    let controller4 = storyBoard.instantiateViewController(withIdentifier: "bookmark")
    let controller5 = storyBoard.instantiateViewController(withIdentifier: "menu")

    self.setViewControllers([controller1,controller2,controller4,controller5], animated: true)

    // creates navigation controller
    let vc1 = UINavigationController(rootViewController: controller1)
    let vc2 = UINavigationController(rootViewController: controller2)
    let vc3 = UINavigationController(rootViewController: controller3)
    let vc4 = UINavigationController(rootViewController: controller4)
    let vc5 = UINavigationController(rootViewController: controller5)

    viewControllers = [vc1, vc2, vc3, vc4, vc5]

    self.setupMiddleButton()





// TabBarButton – Setup Middle Button and View
func setupMiddleButton() 
    playerBtn.frame = CGRect(x: 0, y: 0, width: 45, height: 45)
    var playerBtnFrame = playerBtn.frame
    playerBtnFrame.origin.y = self.view.bounds.height - playerBtnFrame.height - 2
    playerBtnFrame.origin.x = self.view.bounds.width / 2 - playerBtnFrame.size.width / 2
    playerBtn.frame = playerBtnFrame
    playerBtn.layer.cornerRadius = playerBtnFrame.height/2
    playerBtn.layer.masksToBounds = true
    playerBtn.contentMode = .scaleAspectFill


    let view = UIView()
    let width = self.view.frame.width/5
    let xWidth = width*2
    view.frame = CGRect(x:  xWidth , y: playerBtnFrame.origin.y - 2, width: self.view.frame.width/5, height: tabBar.frame.height)
    view.backgroundColor = .clear
    self.view.addSubview(view)
    self.view.addSubview(playerBtn)

    playerBtn.setImage(UIImage(named: "home"), for: UIControlState.normal)
    playerBtn.addTarget(self, action: #selector(playerButtonAction), for: UIControlEvents.touchUpInside)

    self.view.layoutIfNeeded()


// Button Touch Action
@objc func playerButtonAction(sender: UIButton) 
    if GlobalModals.count != 0  
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "player") as? VCPlayer
        self.present(vc!, animated: true, completion: nil)
    



所以我改变了按钮的图像

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageChange"), object: nil)

【讨论】:

以上是关于音乐程序的 TabBarController的主要内容,如果未能解决你的问题,请参考以下文章

2.11 Go音乐播放器

在c语言程序中怎样载入背景音乐啊,是背景音乐哦,求函数具体用法和实例。谢谢

以编程方式将内容添加到音乐库

FL studio12怎么制作3d环绕音(求教程)

HTML5 <audio> 中断移动音乐(Spotify,...)

阻止用户使用桌面、文档、下载、电影、音乐、图片、公共文件夹