gpt4 book ai didi

ios - 在动画过程中改变位置?

转载 作者:行者123 更新时间:2023-12-01 19:01:57 25 4
gpt4 key购买 nike

我有一个uit_code大小的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];
}

最佳答案

您的问题是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){}
];
}
}

关于ios - 在动画过程中改变位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22209542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com