2014-02-12 15 views
0

当实现UIView animateWithDuration:animations:completion:Class方法时,我遇到了一个场景,我无法确定如何处理。iOS >>动画块>>更改移动ImageView方向'块中间'块执行

我有一个UIImageView在屏幕上移动。在某个事件发生后,它应该改变方向,并转移到新的位置。但是(!)看起来,如果这个事件发生,而不是仅仅改变方向并移动到新的位置,它就会跳到原来的“结束位置”并开始从那里移动到新的位置。

我不知道如何指示'移动对象',在completion == NO上捕捉它的当前位置并从那里开始动画,而不是跳到预定的结束位置。

下面是代码:

- (IBAction)goHere:(UIButton *)sender 
{ 
    CGPoint movingEndPoint; 
    if (sender == self.btn1) 
    { 
     movingEndPoint = CGPointMake(sender.center.x + 40, sender.center.y + 40); 
    } 
    if (sender == self.btn2) 
    { 
     movingEndPoint = CGPointMake(sender.center.x - 40, sender.center.y - 40); 
    } 

    [UIView animateWithDuration:3 
        animations:^ { 
     self.movingObj.center = movingEndPoint; 
    } 
        completion:^(BOOL completion) { 
         if (completion == NO) 
         { 
          //How to express "New Position = Current Position"? 
         } 
        }]; 
} 

回答

1

在完成块做相反的, 提供了新的位置之前,在你的事件添加此代码。

比方说,你的事件的方法是eventMethod,

修改后的代码应该是

[UIView animateWithDuration:5 
        animations:^ { 
         self.movingObj.center = CGPointMake(400, 400); //old final position 
        } 
        completion:^(BOOL completion) { 

        }]; 

-(void)eventMethod{ 
    _movingObj.frame =[_movingObj.layer.presentationLayer frame]; 
    [UIView animateWithDuration:2 
        animations:^ { 
         self.movingObj.center = CGPointMake(0, 0); //new final position 
        } 
        completion:^(BOOL completion) { 

        }]; 
} 
+0

尝试Robs解决方案。这很容易 – santhu

+0

这很好 - 我没有使用你的代码,虽然...我只是将presentationLayer框架分配给obj框架,每次调用方法 - 它很好地工作... –

3

这种行为在iOS 8的(它拿起使用对象的当前位置和新的动画被排除方向),但在早期的iOS版本中,最简单的修复方法是使用带options参数的animateWithDuration的再现,并包含UIViewAnimationOptionBeginFromCurrentState选项。

或者,您可以使用视图的图层presentationLayer来获取当前位置的动画。例如,

CALayer *layer = self.movingObj.layer.presentationLayer; 
CGRect currentFrame = layer.frame; 

然后,可以设置再停止的动画(例如[self.movingObj.layer removeAllAnimations]),然后开始下一个动画之前,使用此currentFrame设置视图的frame

+0

是的。完全忘了它。 – santhu