gpt4 book ai didi

iPhone - 同时动画 2 个 subview

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

我有 2 个占据整个屏幕的 subview (状态栏除外)。我们称此尺寸为“屏幕尺寸”。

我想为两者制作动画:

  • 第一个从比屏幕大一点点缩放到屏幕大小,从alpha 0到alpha 1。

  • 第二个从屏幕尺寸到比屏幕尺寸小一点,从 alpha 1 到 alpha 0。

第二个 View 在开始时可见并显示在屏幕上。

我是这样写的:

- (void) switchViews
{
if (self.view2Controller == nil) {
self.view2Controller = [[View2Controller alloc] initWithNibName:@"View2XIB" bundle:nil];
self.view2Controller.view.hidden = YES;
[self.view addSubview:self.view2Controller.view];
}

CGRect bigFrame = CGRectInset(self.view.frame, -50, -50);
CGRect normalFrame = self.view.frame;
CGRect smallFrame = CGRectInset(self.view.frame, 50, 50);

self.view2Controller.view.frame = bigFrame;
self.view2Controller.view.alpha = 0.0;

[UIView beginAnimations:@"Anim1" context:nil];
[UIView setAnimationDuration:5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

self.view2Controller.view.hidden = NO;
self.view2Controller.view.frame = normalFrame;
self.view2Controller.view.alpha = 1.0;

[UIView commitAnimations];

// ------------------------------

[UIView beginAnimations:@"Anim2" context:nil];
[UIView setAnimationDuration:5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

self.view1Controller.view.frame = smallFrame;
self.view1Controller.view.alpha = 0.0;

[UIView commitAnimations];
}

当然,我首先尝试将两个动画放在一个独特的动画中。这不会改变任何东西,这就是为什么我试图将它们分开。

启动后,view1 立即变黑,然后 view2 开始按预期设置动画。但是我无法同时运行两个动画。

我该怎么做?

最佳答案

尝试看一下基于 block 的动画。这是 iOS 4.0+ 推荐的方法。看看这里的答案:What are block-based animation methods in iPhone OS 4.0?

编辑尝试这样的事情

//You can do the same thing with a frame
CGPoint newCenter = CGPointMake(100, 100);
[UIView animateWithDuration:2.0
animations:^{
firstView.center = newCenter;
secondView.center = newCenter;
firstView.alpha = 0.2;
}
completion:^(BOOL finished){
NSLog(@"All done animating");
}];

您放入动画中的任何内容:^{ } 将成为您 View 的目标设置。上面我向您展示了如何更改位置和 alpha。

关于iPhone - 同时动画 2 个 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7603948/

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