我有一个大小为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];
}
答案 0 :(得分:2)
使用UIView
的 animateWithDuration:delay:options:animations:completion:
方法,并在选项中提供UIViewAnimationOptionBeginFromCurrentState
和UIViewAnimationOptionAllowAnimatedContent
。
来源:
答案 1 :(得分: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){}
];
}
}