本文介绍了iOS触控,手势,动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索iOS4.3 SDK和想要实现特定的动画效果。但是不知道该怎么做。就像这样-屏幕上有一个方形框,用户将手指放在盒子上拖动他的手指,盒子应该跟随他。直到这里都很容易。我能够像这样实现它-

I am exploring iOS4.3 SDK & wanted to implement a particular animation effect. But have no idea how to do it. It goes like this - I have a square box on the screen & upon user putting his finger on the box & dragging his finger the box should follow him. It's easy till here. I was able to implement it like so -

-(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];
}

但是当用户抬起手指时,我希望盒子可以随身携带运动(好像有动量)。就像苹果使用的橡皮筋滚动效果一样。即使您退出滚动,屏幕也会滚动&慢慢停止。我该如何实施?

But as the user lifts his finger, I want the box to carry on with the motion (as if with momentum). It's like the same rubber band scrolling effect apple implements; even when u leave scrolling, the screen scrolls & slowly comes to a stop. How do I implement this?

推荐答案

为什么不考虑使用。您可以使用 translationInView:在移动手指时移动框。并且当手势的状态 UIGestureRecognizerStateEnded 时,您可以使用 velocityInView:获得理想的跟进效果。

Why don't you consider using UIPanGestureRecognizer. You can use the translationInView: to move the box as moves the finger across. And when the gesture's state is UIGestureRecognizerStateEnded, you could use velocityInView: to get the desired follow up effect.

这篇关于iOS触控,手势,动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:17