gpt4 book ai didi

iOS animationWithDuration 链接第一个动画未显示

转载 作者:行者123 更新时间:2023-11-28 23:51:35 27 4
gpt4 key购买 nike

我有 2 个动画,其中一个动画调用另一个动画,但只有后者真正发生。我不确定问题是什么。我是 iOS 新手。我试图将其动画化为向上然后向下,但只发生向下。

此外,如果我将调用从 [self animateUp] 更改为 [self animateDown],则不会发生动画。

-(void)viewDidLoad
{
[super viewDidLoad];
[self.containerView addSubview:self.logoView = ({
UIImageView *imageView = [UIImageView new];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageNamed:@"thisImage"];
imageView;
})];
[self animateUp];
}

- (void)animateUp
{
[UIView animateWithDuration:1 animations:^{
CGRect newFrame = CGRectMake(self.logoView.frame.origin.x, self.logoView.frame.origin.y - 40, self.logoView.frame.size.width, self.logoView.frame.size.height);
self.logoView.frame = newFrame;
} completion:^(BOOL finished) {
[self animateDown];
}];
}

- (void)animateDown
{
[UIView animateWithDuration:1 animations:^{
CGRect newFrame = CGRectMake(self.logoView.frame.origin.x, self.logoView.frame.origin.y + 40, self.logoView.frame.size.width, self.logoView.frame.size.height);
self.logoView.frame = newFrame;
} completion:nil ];
}

最佳答案

animateUp 的调用发生在 ImageView 具有初始帧集之前。您可以将此延迟到 viewDidAppearviewDidLayoutSubviews,应该会看到动画正确出现。

-(void)viewDidLoad
{
[super viewDidLoad];
[self.containerView addSubview:self.logoView = ({
UIImageView *imageView = [UIImageView new];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageNamed:@"thisImage"];
imageView;
})];
}

- (void) viewDidLayoutSubviews
{
[self animateUp]; // at this point the ImageView has its frame set
}

请注意,viewDidAppear 和 viewDidLayoutSubviews 在 UIViewController 的生命周期中可能会被多次调用,因此如果您只希望动画发生一次,您需要适应这种情况。

关于iOS animationWithDuration 链接第一个动画未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51937423/

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