簡體   English   中英

動畫期間改變位置?

[英]Changing position during animation?

我有一個大小為64x64的UIImageView。 我希望將其大小設置為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];
}

使用UIViewanimateWithDuration:delay:options:animations:completion:方法,並在選項中提供UIViewAnimationOptionBeginFromCurrentStateUIViewAnimationOptionAllowAnimatedContent

資源:

UIView類參考

你的問題是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){}
        ];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM