iOS动画通过手势将图像视图从屏幕外滑动到屏幕上

时间:2012-09-28 20:31:52

标签: ios image animation sliding

我正在寻找带有文字的气泡,以便在屏幕上和屏幕上滑动。这个动画的理想实现是iOS的地平线滚动,启用了分页。当我到达讲话气泡的末尾时,我肯定想要“反弹”,我明确地希望气泡能够跟踪手指,直到它们滑出屏幕之前的某一点。我认为这与滑动不同(这只是一个方向的轻弹)。

然而,水平滚动的问题在于它针对静态数量的图像进行了优化。我将拥有动态数量的图像,据我所知,您无法动态地将图像附加到水平滚动条。我们的想法是,当您继续通过它时,应用程序会动态地向滚动条添加内容。

卷轴很容易上手但我现在不得不把它拆掉。我怎样才能开始使用手势(我不确定此时标准手势识别器是否适用于我)以及动画?我之前从未使用过那部分iOS代码。

1 个答案:

答案 0 :(得分:3)

我不确定我是否完全按照您的问题进行操作,但如果您想基于手势动画某些内容的移动,则可以使用UIPanGestureRecognizer并更改任何子视图的center你要。例如,在viewDidLoad中你会:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(movePiece:)];
[whateverViewYouWantToAnimate addGestureRecognizer:panGesture];

然后,您可以让手势识别器将其移动到您想要的位置:

- (void)movePiece:(UIPanGestureRecognizer *)gestureRecognizer
{
    static CGPoint originalCenter;

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        originalCenter = [gestureRecognizer view].center;
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [gestureRecognizer translationInView:self.view];

        gestureRecognizer.view.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y);

        // if you wanted to animate both left/right and up/down, it would be:
        // gestureRecognizer.view.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        // replace this offscreen CGPoint with something that makes sense for your app

        CGPoint offscreen = CGPointMake(480, gestureRecognizer.view.center.y);

        [UIView animateWithDuration:0.5
                         animations:^{
                             gestureRecognizer.view.center = offscreen;
                         }
                         completion:^(BOOL finished){
                             // when you're done, you might want to do whatever cleanup
                             // is appropriate for your app (e.g. do you want to remove it?)
                             [gestureRecognizer.view removeFromSuperview];
                         }];
    }
}