【问题标题】:animateWithDuration doing animation based on completely unrelated line of codeanimateWithDuration 基于完全不相关的代码行做动画
【发布时间】:2012-08-06 11:09:28
【问题描述】:

今天 animateWithDuration 表现得非常愚蠢,我不知道为什么。我检查了所有的东西,所有的括号,一切都很完美,但它总是显示我没有指定为动画的代码行。

好的,这就是重点。我有一个 longPressGestureRecognizer。在 longPress 开始后,它位于一些正在拖动的 UIImageView 上。当它在某个特殊区域结束时,我想开始动画,将 UIImageView 带到其他地方并完成它。这是我的代码:

if ([(UILongPressGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
    CGPoint dropPoint = CGPointMake(self.center.x, self.center.y-5);
    for (UIView * placeForCharacter in delegate.subviews) {
        if ([placeForCharacter isKindOfClass:[PlaceForCharacterCircle class]]) {
            PlaceForCharacterCircle * newPlaceForCharacter = (PlaceForCharacterCircle *) placeForCharacter;
            if (CGRectContainsPoint(newPlaceForCharacter.dropZone, dropPoint)) {
                    UIViewAnimationOptions options = 0;
                    [UIView animateWithDuration:0.2 delay:0.0 options:0 animations:^{
                        self.frame = CGRectMake(newPlaceForCharacter.frame.origin.x-8, newPlaceForCharacter.frame.origin.y-5, 40, 55);
                        self.alpha = 0.5;
                    } 
                    completion:^(BOOL finished){
                        NSLog(@"animation completed");
                        self.alpha=1;
                        //code referring to the finalizing of moving, doesn't matter here
                        return;
                    }];
            } else {
                if (CGRectContainsPoint(newPlaceForCharacter.dropZone, dropPoint)) {
                    //code referring to the moving to the other place in case the UIImageView was dropped on another zone that is full already, doesn't matter here
                    return;
                }
            }
        }
    }
        //code referring to the comeback of a UIImageView in case it was dropped somewhere else, doesn't matter here
        //These two are the lines that get executed when the animation starts
        //THEY BEGIN
        translatedPoint = CGPointMake(initialX, initialY);
        [[sender view] setCenter:translatedPoint];
        //THEY END
        return;
}

情况是由于某种奇怪的原因,上面的两行代码得到了动画,而不是指定的真实动画。我通过将 cmets 放在这些代码行上进行了仔细检查 - 在这种情况下,动画效果很好。

谁能帮我解决这个问题,为什么会这样?这似乎是一种非常奇怪的行为。

【问题讨论】:

  • 你指的是哪两行???原则上,动画应该在以下两行执行: self.frame = CGRectMake(newPlaceForCharacter.frame.origin.x-8, newPlaceForCharacter.frame.origin.y-5, 40, 55); self.alpha = 0.5;
  • 我的意思是这两个:translatedPoint = CGPointMake(initialX, initialY); [[发件人视图] setCenter:translatedPoint];
  • 我知道应该发生什么,但它不知道,这很奇怪。

标签: ios animation uigesturerecognizer


【解决方案1】:

如果您将 return; 从完成块(它不做任何事情)移动到动画设置之后,它应该可以工作。

[UIView animateWithDuration:0.2 delay:0.0 options:0 animations:^{
    self.frame = CGRectMake(newPlaceForCharacter.frame.origin.x-8, newPlaceForCharacter.frame.origin.y-5, 40, 55);
    self.alpha = 0.5;
}
completion:^{
    NSLog(@"animation completed");
    self.alpha=1;
    //code referring to the finalizing of moving, doesn't matter here
}];
return;

【讨论】:

  • 好吧,虽然我还是不明白这一点,但为什么这个回报如此重要?你能给我解释一下吗?
  • 您的代码在掉到最后两行之前没有返回,并且由于属性中心是可动画的,您已经看到了。通过将 return 移到完成块之外,您的方法调用在 state =ended 的情况下您期望执行结束的地方结束。
猜你喜欢
  • 1970-01-01
  • 2013-04-03
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多