2012-10-03 32 views
0

我有一個向右移動在屏幕上留下了一個對象:動畫一個UIImageView在動畫中的中間

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:7.8]; 
[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
myImageview.layer.position = CGPointMake(20, myImageView.layer.position.y); 
[UIView commitAnimations]; 

我發現,即使在動畫仍然發生時,Xcode已經標誌着圖像的所在地爲最終目標,並以檢測運動圖像上的觸摸,我需要使用表示層:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    [super touchesBegan:touches withEvent:event]; 

    UITouch *touch = [touches anyObject]; 
    CGPoint touchPoint = [touch locationInView:self.view]; 

    if ([myImageview.layer.presentationLayer hitTest:touchPoint]) { 
     NSLog(@"it's a hit!"); 
    } 
} 

這部分工作。 現在,我希望圖像在按下時向上移動。 我希望圖像向上移動,同時繼續向左移動。 相反,該代碼移動圖像不僅達到,而且一路左其最終目的地:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    [super touchesBegan:touches withEvent:event]; 

    UITouch *touch = [touches anyObject]; 
    CGPoint touchPoint = [touch locationInView:self.view]; 

    if ([mouse.layer.presentationLayer hitTest:touchPoint]) { 
     NSLog(@"it's a hit!"); 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
     myImageView.layer.position = CGPointMake(mouse.layer.position.x, myImageView.layer.position.y - 40); 
     [UIView commitAnimations]; 
    } 
} 

我想要的圖像向上移動,同時繼續它的側向運動。 有誰知道一種方法來實現這一點?

非常感謝!

回答

2

您是否嘗試過設置動畫選項UIViewAnimationOptionBeginFromCurrentState

(我說的選項,因爲它如果不能從過時的UIView類方法中切換出來是與iOS 4.引入了基於塊的激勵方法的選擇,也可作爲[UIView setAnimationBeginsFromCurrentState:YES]只是還沒有。)

的touchesBegan變(使用塊動畫):

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    [super touchesBegan:touches withEvent:event]; 

    UITouch *touch = [touches anyObject]; 
    CGPoint touchPoint = [touch locationInView:self.view]; 

    if ([mouse.layer.presentationLayer hitTest:touchPoint]) { 
     NSLog(@"it's a hit!"); 
     [UIView animateWithDuration:0.5 delay:0.0 options:(UIViewAnimationOptionCurveLinear & UIViewAnimationOptionBeginFromCurrentState) animations:^{ 
      myImageView.layer.position = CGPointMake(myImageView.layer.position.x, mouse.layer.position.y - 40); 
     }completion:^(BOOL complete){ 
      // 
     }]; 
    } 
} 

如果你使用,你應該能夠指定最後xy座標你想和有動畫從該對象被感動到該點出發位置。

+0

太棒了!我完全不知道的是多麼棒的選擇! – RanLearns

+0

非常有趣,它取代了以前的動畫。我試圖讓原始動畫繼續下去,因爲它隻影響x軸,並沿途將動畫添加到y軸。這絕對是一個有趣的選項,但仍然不能正常工作,因爲它不僅將我的imageView向上移動,而且還將其發送到左側的最終目的地。非常感謝!將玩這個新的選項。 =) – RanLearns

+0

您確實需要重新計算和更改動畫參數才能獲得所需的最終結果。 –