0

今天 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;
}

情况是由于某种奇怪的原因,上面的两行代码得到了动画,而不是指定的真实动画。我通过在这些代码行上添加注释来仔细检查它——在这种情况下,动画效果很好。

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

4

1 回答 1

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;
于 2012-07-16T00:31:46.337 回答