【发布时间】:2014-07-18 19:52:26
【问题描述】:
我需要在 iOS 的 Objective-C 中创建带有移动标签的动画。当用户点击某个按钮时,动画应该会出现。我还需要在动画进行时禁用任何 UI 交互。这就是我为实现这种行为所做的:
1.在视图控制器的viewDidLoad方法中创建UILabel对象,将所有需要的字段设置为合适的值并隐藏:
plusScoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
plusScoreLabel.text = @"+10";
plusScoreLabel.textColor = [UIColor colorWithRed:12.0/255.0 green:144.0/255.0 blue:51.0/255.0 alpha:1.0];
plusScoreLabel.hidden = YES;
plusScoreLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:plusScoreLabel];
uiInteractionsDisabled = NO;
2.然后我在按钮按下的动作上调用 animateWithDuration:animation:completion 方法,如下所示:
uiInteractionsDisabled = YES;
[plusScoreLabel setCenter:CGPointMake(buttonToChoose.center.x, buttonToChoose.center.y)];
plusScoreLabel.hidden = NO;
[UIView animateWithDuration:0.5f animations:^{
plusScoreLabel.center = CGPointMake(buttonToChoose.center.x, buttonToChoose.center.y - 50.0f);
} completion:^(BOOL finished) {
plusScoreLabel.hidden = YES;
}];
3.在每个 IBAction 方法中添加以下检查:
if (uiInteractionsDisabled == YES) {
return;
}
这个方法有问题吗?也许有更好的方法来实现这种行为?
提前致谢。
【问题讨论】:
标签: ios objective-c cocoa-touch