• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:11:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我已将 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
}



Best Answer-推荐答案


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

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

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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap