在我看来,这两个类方法不可互换。我有一个 UIView 的 subview ,在 touchesBegan 方法中包含以下代码:
if (!highlightView) {
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Highlight"]];
self.highlightView = tempImageView;
[tempImageView release];
[self addSubview:highlightView];
}
highlightView.alpha = 0.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
highlightView.alpha = 1.0;
[UIView commitAnimations];
当我触摸 Button 时,突出显示会淡入,正如您所期望的那样。当我立即触摸时(在动画完成之前),我的 touchesEnded 被调用。这就是我想要的行为。
但现在,我已经成为积木的忠实粉丝,并尽可能地尝试使用它们。所以我用这个替换了 UIView 动画代码:
[UIView animateWithDuration:0.2 animations:^{
highlightView.alpha = 1.0;
}];
结果:突出显示仍按预期淡入,但如果我在动画完成之前进行修饰,我的 touchesEnded 不会被调用。如果我在动画完成后触摸,我的 touchesEnded 确实被调用。这是怎么回事?
最佳答案
iOS 4 中的新动画 block 默认禁用用户交互。您可以传入一个选项,以允许 View 在动画期间使用位标志与 UIView
的 animateWithDuration:delay:options:animations:completion
方法一起响应触摸:
UIViewAnimationOptions options = UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction;
[UIView animateWithDuration:0.2 delay:0.0 options:options animations:^
{
highlightView.alpha = 1.0;
} completion:nil];
关于iphone - [UIView beginAnimations :context:] and [UIView animateWithDuration:animations:] 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3897279/