【问题标题】:UIView main thread. Animation of 2 UIView at the same timeUIView 主线程。 2个UIView同时动画
【发布时间】:2012-10-14 20:25:15
【问题描述】:

我想知道是否可以同时编写不同的 UIView 动画。 我试图同时使用精灵动画为两个 Apple 风格的翻转计数器设置动画。 每个翻转计数器都有自己的动画。 问题是当第一个计数器完成时,第二个翻转计数器开始运行。

两次编辑:这是代码:https://dl.dropbox.com/u/1348024/MultipleFlipCounters.zip

有两个简单的类“FlipCounterView.m”和“CounterViewController”。 点击屏幕“CounterViewController”将启动动画。 谢谢!

编辑:

添加了相关代码。

Apple 风格的翻转计数器精灵动画是在 FlipCounterView 类中完成的。

- (void)viewDidLoad{
[super viewDidLoad];
flipCounter1 = [[FlipCounterView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[flipCounter1 add:10];
[self.view addSubview:flipCounter1];

flipCounter2 = [[FlipCounterView alloc] initWithFrame:CGRectMake(0, 120, 200, 200)];
[flipCounter2 add:10];
[self.view addSubview:flipCounter2];
}

点击屏幕:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

[UIView animateWithDuration:0.5
                     animations:^{
                         //this method made an sprite animation on flipCounter1
                         [flipCounter1 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
                     }];

[UIView animateWithDuration:0.5
                     animations:^{
                         //this method made an sprite animation on flipCounter2
                         [flipCounter2 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
                     }];

}

【问题讨论】:

标签: iphone ios uiview


【解决方案1】:

您的代码崩溃了,但请在修复崩溃后尝试一下:

- (void)animateNow1
{
    [UIView animateWithDuration:0.5
                     animations:^{
                         [flipCounter1 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
                     }];

}
- (void)animateNow2
{
    [UIView animateWithDuration:0.5
                     animations:^{
                         [flipCounter2 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
                     }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self performSelector:@selector(animateNow1) withObject:nil afterDelay:0.00];
    [self performSelector:@selector(animateNow2) withObject:nil afterDelay:0.00];
}    

【讨论】:

  • 你遇到了什么异常?奇怪的是,我使用的是 Xcode 版本 4.3.2,它并没有崩溃。我用 performSelector:@selector(animateNow1) 进行了测试,但不起作用☹
【解决方案2】:

您的代码中的崩溃似乎与动画无关(Xcode 4.5.1),如果您希望两个动画同时执行,您可以在一个动画块中执行此操作(因为动画属性相同)。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [UIView animateWithDuration:0.5 animations:^{
        [flipCounter1 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
        [flipCounter2 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
    }];
}

【讨论】:

  • 谢谢!我在一个动画块中编辑了上面的链接代码。我希望这能解决异常问题。顺便说一句,动画继续不在同一时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多