【问题标题】:Tinder like Animation using UIPanGestureRecognizer and Rotation使用 UIPanGestureRecognizer 和旋转的类似动画的火种
【发布时间】:2013-12-20 01:12:23
【问题描述】:

我正在尝试实现Tinder 之类的动画来左右滑动图像并在此过程中旋转它。我有一组图像,需要一个接一个地滑动它们。

到目前为止,我已经能够使用 UIPanGestureRecognizer 来移动图像,但是当我尝试旋转图像时,一切都会中断。

我已上传代码here。有人可以指出我正确的方向来开发算法并对其进行微调以实现像 Tinder 一样的完美。

【问题讨论】:

    标签: ios iphone objective-c uiimageview uigesturerecognizer


    【解决方案1】:

    您可以通过监听 touchesBegan、touchesMoved 和 touchesEnded 方法来做到这一点。

    基本上这是你需要做的:

    1. touchesBegan 中获取相对于当前视图的触摸点(稍后您将需要它通过在此点锚定来旋转图像)。
    2. touchesMoved 中获取相对于 superview 的触摸点,并在触摸移动时移动视图。
    3. 使用 CGAffineTransformMakeRotation 根据运动应用小旋转
    4. touchesEnded 中,将视图移出屏幕或将其设置回初始位置。

    希望对您有所帮助。查看此repo 以供参考。

    【讨论】:

      【解决方案2】:

      正如 Shri 提到的,您应该处理平移手势识别器状态。这是代码的快速示例

      func panGestureRecognized(gestureRecognizer: UIPanGestureRecognizer) {
          xDistanceFromCenter = gestureRecognizer.translationInView(self).x
              yDistanceFromCenter = gestureRecognizer.translationInView(self).y
      
              let touchLocation = gestureRecognizer.locationInView(self)
      
              switch gestureRecognizer.state {
              case .Began:
                  originalLocation = center
                  animationDirection = touchLocation.y >= frame.size.height / 2 ? -1.0 : 1.0
                  break
              case .Changed:
                  let rotationStrength = min(xDistanceFromCenter / self.frame.size.width, rotationMax)
                  let rotationAngle = animationDirection * defaultRotationAngle * rotationStrength
                  let scaleStrength = 1 - ((1 - scaleMin) * fabs(rotationStrength))
                  let scale = max(scaleStrength, scaleMin)
                  let transform = CGAffineTransformMakeRotation(rotationAngle)
                  let scaleTransform = CGAffineTransformScale(transform, scale, scale) 
                  self.transform = scaleTransform
                  break
              case .Ended:
                  swipeMadeAction()
              default :
                  break
              }
      }
      

      您可以在这个 repo 上查看我们的实现: https://github.com/Yalantis/Koloda

      【讨论】:

        【解决方案3】:

        我最近刚刚为我工作的公司实现了这个,并决定开源它。 https://github.com/skensell/MXCardsSwipingView

        我将UIPanGestureRecognizerUIAttachmentBehavior 结合使用。在 iOS 7 中引入,它在概念上更容易且代码更少。这就像将您的手指连接到UIView 的那部分。

        更多详情:https://github.com/skensell/MXCardsSwipingView/blob/master/MXCardsSwipingView/Classes/MXCardsSwipingView.m

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多