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

Posted

技术标签:

【中文标题】带有平移、旋转和捏合手势的 UIKit Dynamics【英文标题】:UIKit Dynamics with pan, rotate and pinch gesture 【发布时间】:2017-07-06 12:32:06 【问题描述】:

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

这是我的代码:

func handleAttachmentGesture(_ sender: UIPanGestureRecognizer) 
    let location = sender.location(in: emojiSuperView!)
    let boxLocation = sender.location(in: self)
    switch sender.state 
    case .began:
        print("Your touch start position is \(location)")
        print("Start location in image is \(boxLocation)")

        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("Your touch end position is \(location)")
        print("End location in image is \(boxLocation)")

        animator.removeAllBehaviors()

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

        if magnitude > ThrowingThreshold 
            // 2
            let pushBehavior = UIPushBehavior(items: [self], 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: [self])
            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: [dynamicItem!])
    let animator = UIDynamicAnimator(referenceView: emojiSuperView!)
    // or however you'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

【问题讨论】:

好问题 +1 ???? ,我也面临同样的问题, 您的视图控制器看起来如何?你有公开的吗? 【参考方案1】:

当用户执行任何变换事件时,将当前变换保存到全局变量。

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

        attachmentBehavior.action = 
            self.attachmentBehavior.items[0].transform = self.aTransform
        

【讨论】:

以上是关于带有平移、旋转和捏合手势的 UIKit Dynamics的主要内容,如果未能解决你的问题,请参考以下文章

如果视图注册平移手势,则捏合手势不起作用

手势识别(拖动,旋转,捏合)

旋转后平移手势行为不正确

UIImageView 上的捏合、平移和双击手势

同时检测捏合和旋转手势

同时捏合、拖动和平移