简体   繁体   English

UIKit Dynamics具有平移,旋转和捏合手势

[英]UIKit Dynamics with pan, rotate and pinch gesture

I have added UIDynamics to imageview and used pan gesture for that. 我已经将UIDynamics添加到imageview并为此使用了平移手势。 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
}

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. 之后,在平移开始时,将使用UIAttachmentBehavior的action属性在开始状态下分配新的转换。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM