这个秘诀就是
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:<#(UIView *)#>];
然后在view上添加动画效果就好了。
这里view上要添加TapGesture
在viewController中的设置
self.navigationController.navigationBar.clipsToBounds = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(backViewTapped:)];
backView = [[BackView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
[backView addGestureRecognizer:tapGesture];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backView];
[self.navigationItem setLeftBarButtonItem:backBarButton];
backView中加入一个imageView,在imageView上加动画
-(UIImageView *)imageView{
if(!_imageView){
_imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"back"]];
}
return _imageView;
}
-(void)move{
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"transform";
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(-5, 0, 0)];
animation.duration = 0.5;
[self.imageView.layer addAnimation:animation forKey:nil];
}
再在viewController中调用move的方法
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear: animated];
[backView move];
}