IOS - Advanced W / Velocity Swipe Animation

Basically, I'm trying to move my finger to the globe and turn on the type function.

So, all I really need to do is capture the swipe direction and speed with a short timer (? 500 ms?)

So something like

While(swiping) {
   Get(pointTouched);
   swipeDirection = Calc(direction);
   swipeSpeed = Calc(speed);

   FramesToPlay = swipeSpeed * ConstantAmount;

   If(Direction == Backwards){
      FramesToPlay = FramesToPlay * -1;
   }

   Play(playAnimation, FramesToPlay);

   wait(500ms);
}

Does anyone know something like this? Or are there any things I can put together?

I have an animation, and I found out that these are just the details of this conversation that scare me.

+3
source share
3 answers

You can probably use UIPanGestureRecognizerone that has a method velocityInView:. I have not tested this, but it looks like it should work:

- (void)handlePanGesture:(UIPanGestureRecognizer *)pan
{
    if (pan.state == UIGestureRecognizerStateEnded)
    {
        CGPoint vel = [pan velocityInView:self.view];
        [self doSpinAnimationWithVelocity:vel.x];
    }
}

Also, when pan.state == UIGestureRecognizerChanged, you can make your ball rotate with your finger.

+9

touchsBegan UIView. xy . , atan2 (dy, dx). , .

+2

: UISwipeGestureRecognizer. - UITouch, touchesBegan:withEvent:. super.

When your recognizer starts its action, the recognizer will be passed as a parameter sender. You can set it for start and end touch objects, and then use the method locationInView:and property timestampto determine the scroll speed (speed = change in distance / change in time).

So, it will be something like this:

@interface DDSwipeGestureRecognizer : UISwipeGestureRecognizer 

@property (nonatomic, retain) UITouch * firstTouch;
@property (nonatomic, retain) UITouch * lastTouch;

@end

@implementation DDSwipeGestureRecognizer
@synthesize firstTouch, lastTouch;

- (void) dealloc {
  [firstTouch release];
  [lastTouch release];
  [super dealloc];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  [self setFirstTouch:[touches anyObject]];
  [super touchesBegan:touches withEvent:event];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  [self setLastTouch:[touches anyObject]];
  [super touchesEnded:touches withEvent:event];
}

@end

Then in another place you would do:

DDSwipeGestureRecognizer *swipe = [[DDSwipeGestureRecognizer alloc] init];
[swipe setTarget:self];
[swipe setAction:@selector(swiped:)];
[myView addGestureRecognizer:swipe];
[swipe release];

And your action would be something like this:

- (void) swiped:(DDSwipeGestureRecognizer *)recognizer {
  CGPoint firstPoint = [[recognizer firstTouch] locationInView:myView];
  CGPoint lastPoint = [[recognizer lastTouch] locationInView:myView];
  CGFloat distance = ...; // the distance between firstPoint and lastPoint
  NSTimeInterval elapsedTime = [[recognizer lastTouch] timestamp] - [[recognizer firstTouch] timestamp];
  CGFloat velocity = distance / elapsedTime;

  NSLog(@"the velocity of the swipe was %f points per second", velocity);
}

Warning: code entered in the browser and not compiled. Warning.

+1
source

Source: https://habr.com/ru/post/1789241/


All Articles