4

我已经在2个应用程序中看到了这种效果,我真的很想找到如何去做。仅在UIBarButtonItem中设置动画效果

该动画位于UIBarButtonItem中,仅适用于图像。图像是一个+符号,它旋转到X.

如果你想看到效果,你必须开始与某人的对话,并在文本旁边输入theres图标和表情符号的+按钮。或者在另一个应用程序中查看效果视频,点按酒吧按钮后,您会看到它旋转到X,http://www.youtube.com/watch?v=S8JW7euuNMo

我已经找到了如何做的效果,但只对UIImageView,我必须关闭所有的自动调整和视图模式必须居中,然后将旋转变换应用到它。我尝试了很多方法来尝试让它在条形图项中工作,到目前为止,最好的方法是添加图像视图实例,然后设置它并设置视图模式居中并自动关闭,然后将该图像视图用于自定义酒吧项目视图。但是当我这样做的时候,除了在做它的时候,这个效果会起作用,那么这个图像就会停留在一边,而不是停留在已经存在的地方。我曾尝试在动画之前获得中心,并在动画期间设置它,但是它没有做任何事情。

回答

6

所以这个答案是你必须做一个图像视图的实例,然后设置它没有调整大小和视图模式居中。然后将图像视图添加到具有自定义类型的UIButton中,然后使用该按钮作为条形项目的自定义视图。

- (IBAction)animate { 
    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ 
     imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(45)); 
    } completion:^(BOOL finished) { 
     imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(0)); 
     if ([imageView.image isEqual:[UIImage imageNamed:@"Add.png"]]) { 
      imageView.image = [UIImage imageNamed:@"Close.png"]; 
     } 
     else imageView.image = [UIImage imageNamed:@"Add.png"]; 
    }]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Add.png"]]; 
    imageView.autoresizingMask = UIViewAutoresizingNone; 
    imageView.contentMode = UIViewContentModeCenter; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = CGRectMake(0, 0, 40, 40); 
    [button addSubview:imageView]; 
    [button addTarget:self action:@selector(animate) forControlEvents:UIControlEventTouchUpInside]; 
    imageView.center = button.center; 
    barItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
    navItem.rightBarButtonItem = barItem; 
} 
4

最近不得不在swift中做同样的事情。我created a tutorial包括起动机和最终的项目,去一步一步在撒上一些小技巧的代码看起来是这样的:

@IBOutlet weak var rightBarButton: UIBarButtonItem! { 
    didSet { 
     let icon = UIImage(named: "star") 
     let iconSize = CGRect(origin: CGPointZero, size: icon!.size) 
     let iconButton = UIButton(frame: iconSize) 
     iconButton.setBackgroundImage(icon, forState: .Normal) 
     rightBarButton.customView = iconButton 
     rightBarButton.customView!.transform = CGAffineTransformMakeScale(0, 0) 

     UIView.animateWithDuration(1.0, 
      delay: 0.5, 
      usingSpringWithDamping: 0.5, 
      initialSpringVelocity: 10, 
      options: .CurveLinear, 
      animations: { 
       self.rightBarButton.customView!.transform = CGAffineTransformIdentity 
      }, 
      completion: nil 
     )  

     iconButton.addTarget(self, action: "tappedRightButton", forControlEvents: .TouchUpInside)   
    } 
} 

func tappedRightButton(){ 
    rightBarButton.customView!.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 6/5)) 
    UIView.animateWithDuration(1.0) { 
     self.rightBarButton.customView!.transform = CGAffineTransformIdentity 
    } 
} 
+2

如果您希望按钮在动画过程中可点击,添加'.AllowUserInteraction'选项 – Jon 2016-08-26 14:47:31

1

我想继续扩大攻大小的原生UIBarButtonItem视图提供(如-initWithBarButtonSystemItem:target:action:-initWithCustomView:)。

这是我的代码的基本实现。

- (void)setup { 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(navigationBarRightAction)]; 
} 

- (void)navigationBarRightAction { 
    UIView *itemView = [self.navigationItem.rightBarButtonItem performSelector:@selector(view)]; 
    UIImageView *imageView = [itemView.subviews firstObject]; 

    if (self.shouldRotate) { 
     imageView.contentMode = UIViewContentModeCenter; 
     imageView.autoresizingMask = UIViewAutoresizingNone; 
     imageView.clipsToBounds = NO; 
     imageView.transform = CGAffineTransformMakeRotation(M_PI_4); 

    } else { 
     imageView.transform = CGAffineTransformIdentity; 
    } 
} 
相关问题