菜鸟教程小白 发表于 2022-12-11 18:11:47

ios - 带有平移、旋转和捏合手势的 UIKit Dynamics


                                            <p><p>我已将 UIDynamics 添加到 imageview 并为此使用平移手势。平移手势可以正常工作,但是当我应用捏手势时它不起作用。它显示大 ImageView ,但是当我开始拖动时,它会更改为原始大小。</p>

<p>这是我的代码:</p>

<pre><code>func handleAttachmentGesture(_ sender: UIPanGestureRecognizer) {
    let location = sender.location(in: emojiSuperView!)
    let boxLocation = sender.location(in: self)
    switch sender.state {
    case .began:
      print(&#34;Your touch start position is \(location)&#34;)
      print(&#34;Start location in image is \(boxLocation)&#34;)

      animator.removeAllBehaviors()

      let centerOffset = UIOffset(horizontal: boxLocation.x - self.bounds.midX, vertical: boxLocation.y - self.bounds.midY)
      attachmentBehavior = UIAttachmentBehavior(item: self, offsetFromCenter: centerOffset, attachedToAnchor: location)
      animator.addBehavior(attachmentBehavior)
    case .ended:
      print(&#34;Your touch end position is \(location)&#34;)
      print(&#34;End location in image is \(boxLocation)&#34;)

      animator.removeAllBehaviors()

      // 1
      let velocity = sender.velocity(in: emojiSuperView!)
      let magnitude = sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y))

      if magnitude &gt; ThrowingThreshold {
            // 2
            let pushBehavior = UIPushBehavior(items: , mode: .instantaneous)
            pushBehavior.pushDirection = CGVector(dx: velocity.x / 10, dy: velocity.y / 10)
            pushBehavior.magnitude = magnitude / ThrowingVelocityPadding

            self.pushBehavior = pushBehavior
            animator.addBehavior(pushBehavior)

            // 3
            let angle = Int(arc4random_uniform(20)) - 10

            itemBehavior = UIDynamicItemBehavior(items: )
            itemBehavior.friction = 0.2
            itemBehavior.allowsRotation = true
            itemBehavior.addAngularVelocity(CGFloat(angle), for: self)
            animator.addBehavior(itemBehavior)
      }
    default:
      attachmentBehavior.anchorPoint = sender.location(in: emojiSuperView!)
      break
    }
}


func recognizePinchGesture(sender: UIPinchGestureRecognizer)
{
    weak var dynamicItem: UIDynamicItem?
    // whatever your item is, probably a UIView
    dynamicItem = self
    let behavior = UIGravityBehavior(items: )
    let animator = UIDynamicAnimator(referenceView: emojiSuperView!)
    // or however you&#39;re getting your animator
    animator.addBehavior(behavior)
    sender.view!.transform = sender.view!.transform.scaledBy(x: sender.scale, y: sender.scale)
    animator.updateItem(usingCurrentState: self)
    self.animator.updateItem(usingCurrentState: self)
    sender.scale = 1
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当用户执行任何变换事件时,将当前变换保存到全局变量。 </p>

<p>之后,当平移开始时,使用 UIAttachmentBehavior 的 action 属性在开始状态下分配新的变换。</p>

<pre><code>      attachmentBehavior.action = {
            self.attachmentBehavior.items.transform = self.aTransform
      }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 带有平移、旋转和捏合手势的 UIKit Dynamics,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44948982/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44948982/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 带有平移、旋转和捏合手势的 UIKit Dynamics