iOS Touch,手势,动画

Sri*_*aju 1 animation core-animation objective-c ios

我正在探索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];
}
Run Code Online (Sandbox Code Playgroud)

但是当用户抬起他的手指时,我希望盒子继续运动(好像有动量).这就像苹果实施的橡皮筋滚动效果一样; 即使你离开滚动,屏幕也会滚动并慢慢停止.我该如何实现?

Nav*_*bas 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];

}
Run Code Online (Sandbox Code Playgroud)

希望这会帮助别人:)