iosswiftswift3uikit-dynamics

UIKit Dynamics with pan, rotate and pinch gesture


I have added UIDynamics to imageview and used pan gesture for that. It is working fine with pan gesture but when I apply pinch gesture with that it is not working. It is showing large imageview but when I start dragging then it is changed to original size.

Here is my code:

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
}

Solution

  • When a user does any transform event, save current transform to a global variable.

    after that when panning start assigns new transform in began state using UIAttachmentBehavior's action property.

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