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

ios - UI 旋转手势识别器捕捉直角

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

我正在尝试使用这种旋转机制来捕捉直角,例如当用户接近(85 到 95 度)时,它会自动捕捉到 90 度,直到他离开 85 或 95 度。

var lastRotation = CGFloat()
func rotateAction(sender:UIRotationGestureRecognizer){


    let currentTransform = sender.view?.transform
    let rotation = 0.0 - (lastRotation - sender.rotation)
    let newTransform = currentTransform!.rotated(by: rotation)

    let radians = atan2f(Float(sender.view!.transform.b), Float(sender.view!.transform.a))
    let degrees = radians * (180 / .pi)

    sender.view?.transform = newTransform
    lastRotation = sender.rotation
    if sender.state == .ended {
        lastRotation = 0.0;
    }

    // The if statement works correctly when reaching the angles
    if degrees > -95 && degrees < -85 {

    }
    else if degrees > -185 && degrees < -175 {

    }
    else if degrees > -275 && degrees < -265 {

    }
    else if degrees > -5 && degrees < 5 {  
        // So I tried this but it does not seem right, it always pushed it away from angle 0
        lastRotation = CGFloat(0.0 - radians)
    }

}



Best Answer-推荐答案


数学与您正在进行的检查不符(度数变量为:-180...0...180 -> -180...0 等)。

另一个问题是 sender.rotation 是累积的,这意味着它会在您旋转时不断增加或减少弧度。

一旦其他数学被修复,我会建议类似于以下内容来检查“快照”。

    let rad = fabs(sender.rotation.truncatingRemainder(dividingBy: 2 * CGFloat.pi))

    print("rotation", sender.rotation, degrees, rad)

    switch rad {
    case 1.48353...1.65806 :
        print("do things")
    case 3.05433...3.22886 :
        print("do things")
    case 4.62512...4.79966 :
        print("do things")
    case 0...0.0872665 :
        print("this is check one of 2")
    case 6.19592...6.28319 :
        print("this is check two of 2")
    default:
        print("do other things")
    }

编辑:https://developer.apple.com/documentation/uikit/uirotationgesturerecognizer/1624337-rotation

关于ios - UI 旋转手势识别器捕捉直角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45620889/

回复

使用道具 举报

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

本版积分规则

关注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