iOS Animate 使用手势将 ImageView 从屏幕外滑动到屏幕上

标签 ios image animation sliding

我正在寻找带有文字的气泡动画,以在屏幕上滑动和滑出。此动画的理想实现是启用分页的 iOS 水平滚动。当我到达语音气泡的末尾时,我绝对想要“反弹”,并且我绝对希望气泡跟踪手指直到它们滑出屏幕之前的某个点。我相信这与滑动不同(这只是一个方向的轻弹)。

然而,水平滚动的问题在于它针对静态数量的图像进行了优化。我将拥有动态数量的图像,据我所知,您不能将图像动态附加到水平滚动条。这个想法是应用程序在您继续浏览滚动条时动态地将内容添加到滚动条中。

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

最佳答案

我不确定我是否完全遵循您的问题,但如果您想根据手势为某物的运动设置动画,您可以使用 UIPanGestureRecognizer并更改 center您想要的任何 subview 。例如,在 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];
                         }];
    }
}

关于iOS Animate 使用手势将 ImageView 从屏幕外滑动到屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12647017/

相关文章:

python - 从 request.get 在 Django 中保存图像

objective-c - iOS 10 UILabels -> 1 UIView -> 使用动画循环

ios - 使用 for-in 循环枚举

ios - C4Label 截断显示的文字

ios - 以下 iOS 代码安全吗? (__autoreleasing 语义)

javascript - 为我的 children 制作一个数学游戏;如何根据选择切换图像?

ios - 使用自定义文本输入 UIView 处理返回键点击

Android 相机图像大小

animation - Unity3D:何时评估转换条件

java - 设置动画反向移动并在单击按钮时显示类中的形状