gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:09:26 25 4
gpt4 key购买 nike

我已将 UIDynamics 添加到 imageview 并为此使用了平移手势。它在平移手势上工作正常,但是当我应用捏手势时它不起作用。它显示大 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
}

最佳答案

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

之后,当平移开始时,使用 UIAttachmentBehavior 的操作属性在开始状态分配新的转换。

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

关于ios - 带有平移、旋转和捏合手势的 UIKit Dynamics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44948982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com