iOS - 使用 UIPanGestureRecognizer 平移时实时推进动画

发布于 2024-10-20 16:30:11 字数 351 浏览 5 评论 0原文

基本上我想复制地球的旋转。

在现实世界中,您将手指放在地球仪上,然后将其向右移动,当您移动手指时,地球仪向右旋转。

在 iPhone 上,事情没那么简单……

可能很简单,比如一根手指放在屏幕上,X 点被抓住,然后当手指向右移动一个像素时,地球仪向右旋转一帧,然后原点更改为新地点。然后,如果手指移回原来的位置,地球仪就会向左旋转一帧。一切都不需要拿起你的手指...

那么我该怎么办呢?我假设有一个“whileTouching”事件会持续运行/每 500 毫秒/等等...

有人知道这样的示例代码吗?

编辑:前进框架本身我只能管理捕获我无法弄清楚的触摸事件。

Basically I want to replicate the spinning of a globe.

In the real world you'd put your finger on the globe and and move it to the right and while you are moving your finger the globe spins to the right.

On an iPhone it's not that simple...

It could be something as simple as a finger comes down on the screen and point X is grabbed and then when the finger moves one pixel to the right the globe spines one frame to the right and the origin changes to the new spot. Then if the finger moves back to the original spot the globe spin one frame to the left. All with out picking up your finger...

So how might I go about this? I assume there is a "whileTouching" event that would run constantly / every 500ms / etc...

Anyone know of some sample code like this?

Edit: The advancing the frame itself I can manage just the capturing of the touch event I can't figure out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

執念 2024-10-27 16:30:12

每当手指移动时,UIPanGestureRecognizer 将继续调用其操作方法。您使用状态来确定如何更改当前视图。

此代码示例假设视图的视图控制器处理手势。

- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture //Your action method
{
    switch(panGesture.state) {
        case UIGestureRecognizerStateChanged:
            CGPoint translation = [panGesture translationInView:self.view];
            // Rotate the globe by the amount in translation
            // Fall through to began so that the next call is relative to this one
        case UIGestureRecognizerStateBegan:
            [panGesture setTranslation:CGPointZero inView:self.view];
            break;
        case UIGestureRecognizerStateEnded:
            CGPoint velocity = [panGesture velocityInView:self.view];
            // The user lifted their fingers. Optionally use the velocity to continue rotating the globe automatically
            break;
        default:
            // Something else happened. Do any cleanup you need to.
    }
}

The UIPanGestureRecognizer will continue to call its action methods whenever the finger moves. You use the state to determine how to change the current view.

This code sample assumes that the view's view controller handles the gesture.

- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture //Your action method
{
    switch(panGesture.state) {
        case UIGestureRecognizerStateChanged:
            CGPoint translation = [panGesture translationInView:self.view];
            // Rotate the globe by the amount in translation
            // Fall through to began so that the next call is relative to this one
        case UIGestureRecognizerStateBegan:
            [panGesture setTranslation:CGPointZero inView:self.view];
            break;
        case UIGestureRecognizerStateEnded:
            CGPoint velocity = [panGesture velocityInView:self.view];
            // The user lifted their fingers. Optionally use the velocity to continue rotating the globe automatically
            break;
        default:
            // Something else happened. Do any cleanup you need to.
    }
}
如日中天 2024-10-27 16:30:12

听起来您应该使用 UIPanGestureRecognizer 来执行此操作。基本上,只要您的手指被按下,这就会跟踪您的手指按压,并在特定视图内进行翻译。

编码的简要想法如下:

UIPanGestureRecognizer *touch = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(someFunction:);
[self.view addGestureRecognizer:touch];
[touch release]; 

这会将手势识别器添加到您的视图中(假设此代码位于视图控制器中)。然后,您需要在函数“someFunction”中添加“globe spin”代码。

像这样的事情:

-(void) someFunction:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view]; 

    // Your globe rotation code goes here
}

[recognizer TranslationInView:self.view] 将为您提供手势识别器的翻译。您可以使用它来设置地球的图像或变换,但您正在处理实际的旋转。

希望这有帮助。

干杯。

It sounds like you should use a UIPanGestureRecognizer to do this. Basically this will track your finger press and it's translation within a specific view as long as your finger is pressed.

A brief idea for the coding would be something like:

UIPanGestureRecognizer *touch = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(someFunction:);
[self.view addGestureRecognizer:touch];
[touch release]; 

This will add the gesture recognizer to your view (assuming this code is in a view controller). Then you would need to add the "globe spinning" code inside the function "someFunction".

Something like this:

-(void) someFunction:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view]; 

    // Your globe rotation code goes here
}

The [recognizer translationInView:self.view] will give you the translation of your gesture recognizer. You can use this to set the image or transform of your globe, however you are dealing with the actual spinning.

Hope this helps.

Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文