首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS -使用Swipe W/ Swipe速度的高级动画

iOS -使用Swipe W/ Swipe速度的高级动画
EN

Stack Overflow用户
提问于 2011-02-01 22:15:11
回答 3查看 5.6K关注 0票数 3

基本上我试着把你的手指放在地球上,然后旋转它的类型函数。

所以我真正需要做的就是用一个很短的计时器(?500ms?)抓取滑动的方向和速度。

所以就像这样

代码语言:javascript
复制
While(swiping) {
   Get(pointTouched);
   swipeDirection = Calc(direction);
   swipeSpeed = Calc(speed);

   FramesToPlay = swipeSpeed * ConstantAmount;

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

   Play(playAnimation, FramesToPlay);

   wait(500ms);
}

有人知道这样的事吗?或者任何我可以拼凑起来的碎片?

我已经弄明白了这个动画,只是这个滑动的细节让我有点困惑。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-02-02 02:17:55

您可能会使用具有velocityInView:方法的UIPanGestureRecognizer。我还没有测试过,但是看起来应该可以工作了:

代码语言:javascript
复制
- (void)handlePanGesture:(UIPanGestureRecognizer *)pan
{
    if (pan.state == UIGestureRecognizerStateEnded)
    {
        CGPoint vel = [pan velocityInView:self.view];
        [self doSpinAnimationWithVelocity:vel.x];
    }
}

此外,当pan.state == UIGestureRecognizerChanged时,您可以让您的地球随着手指一起旋转。

票数 9
EN

Stack Overflow用户

发布于 2011-02-02 01:35:44

在当前UIView中使用touchesBegan和touchesMoved委托。这些代理返回xy位置和时间戳。您可以通过将触摸之间的毕达哥拉斯距离除以增量时间来估计触摸或滑动的速度,并从atan2(dy,dx)获得角度。您还可以通过对多个触摸事件执行此操作来平均或过滤返回的速度。

票数 2
EN

Stack Overflow用户

发布于 2011-02-02 02:05:04

我是这样做的:创建一个UISwipeGestureRecognizer的子类。这个子类的目的仅仅是记住它在其touchesBegan:withEvent:方法中接收的第一个也是最后一个UITouch对象。其他的都会被转发到super上。

当识别器触发其操作时,识别器将作为sender参数传入。您可以向它询问初始和最终的触摸对象,然后使用locationInView:方法和timestamp属性计算出滑动的速度(速度=距离的变化/时间的变化)。

所以应该是这样的:

代码语言:javascript
复制
@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

然后在其他地方你会这样做:

代码语言:javascript
复制
DDSwipeGestureRecognizer *swipe = [[DDSwipeGestureRecognizer alloc] init];
[swipe setTarget:self];
[swipe setAction:@selector(swiped:)];
[myView addGestureRecognizer:swipe];
[swipe release];

你的动作应该是这样的:

代码语言:javascript
复制
- (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);
}

警告:在浏览器中键入但未编译的代码。警告实施者。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4863599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档