2014-03-05 54 views
1

我有一個UIImageView,大小爲64x64。我想將它的大小設置爲8x8。它工作正常。但是,當我從動畫外部更改point1(touchPositionInView)的位置時,它不會更新,而是會從觸摸開始的原始位置開始動畫。有沒有什麼辦法解決這一問題?在動畫中改變位置?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [UIView beginAnimations:@"widen" context:nil]; 
    [UIView setAnimationDuration:1.0]; 
    CGRect newRect = yellow.frame; 
    newRect.size.height = 8; 
    newRect.size.width = 8; 
    yellow.center = point1; 
    yellow.frame = newRect; 
    [UIView commitAnimations]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    point1 = [touch locationInView:View]; 
} 

回答

1

您的問題是,touchesBegan:withEvent:只調用一次,touchesMoved:withEvent:被稱爲上的每一個動作。

我建議你在你的touchesMoved:withEvent:方法中添加一個動畫塊。 :-)

存儲point1並將圖像大小調整爲8,8和開始位置動畫。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    point1 = [touch locationInView:View]; 

    [UIView beginAnimations:@"widen" context:nil]; 
    [UIView setAnimationDuration:1.0]; 
    CGRect newRect = yellow.frame; 
    newRect.size = CGSizeMake(8, 8); 
    yellow.frame = newRect; 
    [self updatePoint:NO]; 
    [UIView commitAnimations]; 
} 

更新上的一舉一動位置和開始位置的動畫。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    point1 = [touch locationInView:View]; 
    [self updatePoint:YES]; 
} 

執行位置動畫。

- (void)updatePoint:(BOOL)animated 
{ 

    // I think it would be an approvement, if you calculate the duration by the distance from the 
    // current point to the target point 
    if (animated) 
    { 
     yellow.center = point1; 
    } 
    else 
    { 
     [UIView animateWithDuration:1.0 
           delay:0 
          options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowAnimatedContent 
         animations:^{ 
          yellow.center = point1; 
         } 
         completion:^(BOOL finished){} 
     ]; 
    } 
} 
+0

我試圖用你的代碼沒有奏效。 – Freddy

+0

什麼都不起作用? –

+0

當我移動觸摸它將首先改變大小到8,8然後它將移動到位置。我希望它在同一時間更改大小和位置:) – Freddy

2

使用UIViewanimateWithDuration:delay:options:animations:completion:方法和方案提供UIViewAnimationOptionBeginFromCurrentStateUIViewAnimationOptionAllowAnimatedContent

來源:

UIView Class Reference