Ramotion / animated-tab-bar

:octocat: RAMAnimatedTabBarController is a Swift UI module library for adding animation to iOS tabbar items and icons. iOS library made by @Ramotion
https://www.ramotion.com/animated-tab-bar-ios-app-development-ui-library/
MIT License
11.12k stars 1.33k forks source link

Possible to work with different images of selected and unselected state? #75

Closed OptTrader closed 8 years ago

OptTrader commented 8 years ago

Is it possible to work on the animation with 2 different images for each tab (selected & unselected)?

I'm assuming it doesn't work. Just want to check.

0ber commented 8 years ago

yes you can, just add custom animation class. Look section "Creating Custom Animations" in README.md. change image in this methods:

override func playAnimation(icon : UIImageView, textLable : UILabel) {
    // add animation
   .....
   icon.image = selectedImage
   ....
  }
  // method call when Tab Bar Item is deselected
  override func deselectAnimation(icon : UIImageView, textLable : UILabel, defaultTextColor : UIColor) {
    // add animation
   .....
   icon.image = normalImage
   ....
  }
savasadar commented 7 years ago

I am going crazy, How can I access "selectedImage" from in custom animation.

lyc2345 commented 6 years ago

Hi, I have the same question with @savasadar. How can I access selectedImage in CustomAnimation class?

lyc2345 commented 6 years ago

@savasadar I solved selectedImage by create a img and a selectedImg in a custom RAMAnimatedTabBarItem. Because the original image and selectedImage somehow effect each other.

class HostAnimatedTabBarItem: RAMAnimatedTabBarItem {
  var img: UIImage?
  var selectedImg: UIImage?
}

Then also create a weak item: RAMAnimatedTabBarItem in a overrided RAMItemAnimation class.

class HostBounceAnimation: RAMItemAnimation {

  weak var tabBarItem: RAMAnimatedTabBarItem?
}
let item = HostAnimatedTabBarItem(title: ..., image: ..., selectedImage: ...)
let animation = HostBounceAnimation()
item.animation = animation
animation.tabBarItem = item

Final step is assigning your item with your new img or selectedImg property.

  override func playAnimation(_ icon: UIImageView, textLabel: UILabel) {
    playBounceAnimation(icon)
    textLabel.textColor = Configuration.Theme.darkBlue
    guard let item = tabBarItem as? HostAnimatedTabBarItem else {
      fatalError("item selected image is nil")
    }
    icon.image = item.selectedImg
  }

Do the same thing with your deselect method. I think it will work like a charm.