iPhone - 同时动画 2 个 subview
<p><p>我有 2 个占据整个屏幕的 subview (状态栏除外)。我们称这个尺寸为“屏幕尺寸”。</p>
<p>我想同时动画:</p>
<ul>
<li><p>第一个从比屏幕尺寸大一点的屏幕尺寸缩放到屏幕尺寸,从 alpha 0 到 alpha 1。</p></li>
<li><p>第二个从屏幕尺寸到比屏幕尺寸小一点,从alpha 1到alpha 0。</p></li>
</ul>
<p>第二个 View 在开始时可见并在屏幕上。</p>
<p>这是我写的:</p>
<pre><code>- (void) switchViews
{
if (self.view2Controller == nil) {
self.view2Controller = [ initWithNibName:@"View2XIB" bundle:nil];
self.view2Controller.view.hidden = YES;
;
}
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;
;
;
;
;
;
;
self.view2Controller.view.hidden = NO;
self.view2Controller.view.frame = normalFrame;
self.view2Controller.view.alpha = 1.0;
;
// ------------------------------
;
;
;
;
;
;
self.view1Controller.view.frame = smallFrame;
self.view1Controller.view.alpha = 0.0;
;
}
</code></pre>
<p>当然,我首先尝试将两种动画都放入一个独特的动画中。这不会改变任何事情,这就是我试图将它们分开的原因。</p>
<p>当启动时,view1 立即变为黑色,然后 view2 开始按预期设置动画。但我无法同时运行两个动画。</p>
<p>我该怎么做?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>尝试查看基于 block 的动画。这是 iOS 4.0+ 推荐的方法。看看这里的答案:<a href="https://stackoverflow.com/questions/3126833/what-are-block-based-animation-methods-in-iphone-os-4-0/3316009#3316009" rel="noreferrer noopener nofollow">What are block-based animation methods in iPhone OS 4.0?</a> </p>
<p><strong>编辑</strong>
试试这样的</p>
<pre><code>//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");
}];
</code></pre>
<p>您放入动画中的任何内容:^{ } 将是您 View 的目标设置。上面我向您展示了如何更改位置以及 alpha。</p></p>
<p style="font-size: 20px;">关于iPhone - 同时动画 2 个 subview ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/7603948/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/7603948/
</a>
</p>
页:
[1]