gpt4 book ai didi

objective-c - 试图让 2 个动画同时工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:03 26 4
gpt4 key购买 nike

在接下来的代码中,有 2 个方法被一个接一个地调用。第一个使中心按钮消失,第二个使标签栏消失。另外,他们工作正常。我的问题是,当我尝试一个接一个地调用它们时,hideCenterButton 没有动画。按钮没有滚动到屏幕左侧,而是直接消失了。

-(void)hideCenterButton:(BOOL)animated
{
if(animated){

[UIView animateWithDuration:0.3
delay:0.0f
options:UIViewAnimationCurveLinear
animations:^{
CGRect frame = self.centerButton.frame;
frame.origin.x = -100;
self.centerButton.frame = frame;
}
completion:^(BOOL finished){
}];
}}

...

- (void)hideTabBar:(UITabBarController *) tabbarcontroller
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//[UIView setAnimationDelay:1];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]] || [view isKindOfClass:[UIImageView class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}

[UIView commitAnimations];
}

最佳答案

您可能遇到了基于 UIView 的动画的某种限制。您可能会尝试的几件事是:

  1. animateWithDuration 也用于 hideTabBar::确实,beginAnimation 是为 UIView 设置动画的旧方法; animateWithDuration 较新(与 iOS4 一起引入);通过在机器人案例中使用相同的动画调用,您可能会获得更好的结果;这应该很简单;这应该有效:

      - (void)hideTabBar:(UITabBarController *) tabbarcontroller
    {
    [UIView animateWithDuration:0.3
    delay:0.0f
    options:UIViewAnimationCurveLinear
    animations:^{
    for(UIView *view in tabbarcontroller.view.subviews)
    {
    if([view isKindOfClass:[UITabBar class]] || [view isKindOfClass:[UIImageView class]])
    {
    [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
    }
    else
    {
    [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
    }
    }
    }
    completion:nil];
    }
  2. 使用 Core Animation 重写您的动画:这是一种稍微低级的机制,但它可以让您在性能方面获得最佳结果;这是,例如,如何重写第一个动画:

首先你需要提供一个回调:

- (void)animationDidStop:(CABasicAnimation*)anim finished:(BOOL)flag {
CALayer* layer = [anim valueForKey:@"animationLayer"];
layer.position = [anim.toValue CGPointValue];
}

然后你可以像这样为你的按钮设置动画:

    CABasicAnimation* move = [CABasicAnimation animationWithKeyPath:@"position"];
CGPoint pos = self.centerButton.center;
pos.x -= 100;
move.toValue = [NSValue valueWithCGPoint:pos];
move.duration = 0.3;
move.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
move.removedOnCompletion = NO;
move.fillMode = kCAFillModeForwards;
move.autoreverses = NO;
move.delegate = self;
[move setValue:self.centerButton.layer forKey:@"animationLayer"];
[self.centerButton.layer addAnimation:move forKey:@"buttonPosition"];

使用 Core Animation,您最终会编写更多代码,并且需要留出一些时间来学习 Core Animation 基础知识,但这是我解决两个相关动画时遇到的类似问题的唯一方法。

希望对您有所帮助。

关于objective-c - 试图让 2 个动画同时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com