iOS简单模仿最新版(6.5.0)淘宝APP底部点击效果

2017/3/13更新:给各位大佬们献上鲜嫩的Demo
1 OC版本
2Swift版本

今天是3.8妇女节,首先祝广大女性节日快乐!另外,今天iOS开发界还有件事炸开了锅,就是苹果禁止热更新,使用的应用审核中的被拒,上架的被发邮件警告,让那些已经或正在在学习RN、weex、JSPatch的同学感到一阵蛋疼!

iOS简单模仿最新版(6.5.0)淘宝APP底部点击效果
M4.jpg

回归正题,今天更新了下淘宝app,更新完让人眼前一亮,手感(用户体验)也贼好。底部tabbar的效果让轻微强迫症的我点了很久0.0 下面就简易的模仿下这个效果吧。
由于点击时的音效比较短,用AudioToolbox足矣。

1.引入AudioToolbox.framework和音频文件,文件格式必须是caf,waf,aif中的一种。

iOS简单模仿最新版(6.5.0)淘宝APP底部点击效果
导入框架.png

2.导入AudioToolbox,在tabbar点击方法中实现,下面分别是OC版和Swift版

2.1 OC版
  #import <AudioToolbox/AudioToolbox.h>
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{//此方法系统自动调用,写在继承自UITabBarController的controller中
    NSInteger index = [self.tabBar.items indexOfObject:item];
    [self animationWithIndex:index];//点击时动画
    [self playSound];//点击时音效
    if([item.title isEqualToString:@"首页"]){

    }
}
-(void) playSound{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"like" ofType:@"caf"];
    SystemSoundID soundID;
    NSURL *soundURL = [NSURL fileURLWithPath:path];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL,&soundID);
    AudioServicesPlaySystemSound(soundID);

}

- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer] 
     addAnimation:pulse forKey:nil]; 
}
2.2 Swift版
import AudioToolbox
   override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        var index:NSInteger?
        index = self.tabBar.items?.index(of: item)
        playSound()
        animationWithIndex(index!)
    }
    func playSound(){
         let path = Bundle.main.path(forResource: "like", ofType: "caf")
         let soundURL = NSURL.fileURL(withPath: path!)
         var soundID = SystemSoundID()

        AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID)
        AudioServicesPlaySystemSound(soundID)


    }
    func animationWithIndex(_ index :NSInteger){

        var tabbarbuttonArray : Array<UIView?> = []
        for tabBarButton in (self.tabBar.subviews) {
            if tabBarButton.isKind(of: NSClassFromString("UITabBarButton")!){
                 tabbarbuttonArray.append(tabBarButton)
            }
        }
        let pulse = CABasicAnimation.init(keyPath: "transform.scale")
        pulse.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
        pulse.duration = 0.08;
        pulse.repeatCount = 1;
        pulse.autoreverses = true;
        pulse.fromValue = NSNumber.init(value: 0.7)
        pulse.toValue = NSNumber.init(value: 1.3)
        tabbarbuttonArray[index]?.layer.add(pulse,forKey:nil)
    }

3.开发中遇到的问题

当程序运行

 NSURL *soundURL = [NSURL fileURLWithPath:path];

这句时说找不到这个音频文件,没生成路径,导致代码崩溃,以前做https适配导入证书的时候也碰到过找不到该证书,此时可以直接将文件加入到编译队伍里 ,如下图:

iOS简单模仿最新版(6.5.0)淘宝APP底部点击效果
导入like.caf文件.png

以上。

-原文链接-