[英]Changing position during animation?
I have an UIImageView with the size of 64x64
. 我有一个大小为
64x64
的UIImageView。 I want to animate its size down to 8x8. 我希望将其大小设置为8x8。 It works fine.
它工作正常。 However when i change position of point1 (touchPositionInView) from outside the animation it won't update and it will instead animate from the original position where the touches began.
然而,当我从动画外部更改point1(touchPositionInView)的位置时,它将不会更新,而是将从触摸开始的原始位置进行动画处理。 Is there any way to fix this?
有没有什么办法解决这一问题?
- (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];
}
Use the UIView
's animateWithDuration:delay:options:animations:completion:
method and provide UIViewAnimationOptionBeginFromCurrentState
and UIViewAnimationOptionAllowAnimatedContent
in options. 使用
UIView
的animateWithDuration:delay:options:animations:completion:
方法,并在选项中提供UIViewAnimationOptionBeginFromCurrentState
和UIViewAnimationOptionAllowAnimatedContent
。
Source: 资源:
your problem is that touchesBegan:withEvent:
is only called once and touchesMoved:withEvent:
is called on every movement. 你的问题是
touchesBegan:withEvent:
只被调用一次而touchesMoved:withEvent:
在每次运动时被调用。
I suggest you add an animation block in your touchesMoved:withEvent:
method also. 我建议你在
touchesMoved:withEvent:
方法中添加一个动画块。 :-) :-)
Store the point1 and resize your image to 8, 8 and start position animation. 存储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];
}
Update the position on every move and start the position animation. 在每次移动时更新位置并开始位置动画。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
point1 = [touch locationInView:View];
[self updatePoint:YES];
}
Do the position animation. 做位置动画。
- (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.