iOS - Advancing an Animation in Realtime While Panning with UIPanGestureRecognizer

泄露秘密 提交于 2019-12-23 01:28:37

问题


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.


回答1:


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.
    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/5238149/ios-advancing-an-animation-in-realtime-while-panning-with-uipangesturerecogniz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!