繁体   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