我正在探索iOS4.3 SDK&想要实现特定的动画效果。但不知道该怎么做。它是这样的 - 我在屏幕上有一个方框&当用户将手指放在盒子上时拖着他的手指,盒子应该跟着他。直到这里很容易。我能够像这样实现它 -
-(void)touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
CGAffineTransform scaleTrans = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
CGAffineTransform rotateTrans = CGAffineTransformMakeRotation(angle * M_PI / 180);
boxView.transform = CGAffineTransformConcat(scaleTrans, rotateTrans);
angle = (angle == 180 ? 360 : 180);
scaleFactor = (scaleFactor == 2 ? 1 : 2);
boxView.center = location;
[UIView commitAnimations];
}
但是当用户抬起手指时,我希望盒子继续运动(好像有动量)。这就像苹果实施的相同橡皮筋滚动效果;即使你离开滚动,屏幕滚动&慢慢地停下来。我该如何实现?
答案 0 :(得分:5)
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocityPoint = [recognizer velocityInView:yourView];
[UIView setAnimationDelegate:self];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[yourView setCenter:CGPointMake(yourView.center.x + (velocityPoint.x/4), yourView.center.y + (velocityPoint.y/4))];
[recognizer setTranslation:CGPointZero inView:yourView];
[UIView commitAnimations];
}
希望这会有所帮助:)
答案 1 :(得分:2)
为什么不考虑使用UIPanGestureRecognizer
。您可以使用translationInView:
移动框,同时移动手指。当手势state
为UIGestureRecognizerStateEnded
时,您可以使用velocityInView:
来获得所需的跟进效果。