向 sceneKit 视图添加了按钮,但它有延迟

added button to sceneKit view but it has a lag

我在 sceneKit 视图中添加了一个自定义按钮。当它被触摸时,它会播放一个动画,表示它被点击了。我面临的问题是用户触摸和动画开始之间的延迟。我的场景有 28.1K 个三角形和 84.4K 个顶点。这是太多还是我需要以不同的方式实现按钮。场景以 60fps 渲染。我通过 sceneView.addSubview 添加了按钮: 感谢您的回答

     viewDidLoad(){
     // relevant code
        starButton = UIButton(type: UIButtonType.Custom)
        starButton.frame = CGRectMake(100, 100, 50, 50)
        starButton.setImage(UIImage(named: "yellowstar.png"), forState: UIControlState.Normal)
        sceneView.addSubview(starButton)
        starButton.addTarget(self, action: "starButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        starButton.adjustsImageWhenHighlighted = false
        }



    func starButtonClicked(){
            animateScaleDown()

        }

    func animateScaleDown(){

        UIView.animateWithDuration(0.1, animations: {
            self.starButton.transform = CGAffineTransformMakeScale(0.8, 0.8)

            }, completion: { _ in
                self.wait()
        })

    }

    func wait(){
        UIView.animateWithDuration(0.2, animations: {}, completion: { _ in
            UIView.animateWithDuration(0.2, animations: {
                self.starButton.transform = CGAffineTransformMakeScale(1, 1)

            })
        })
    }

好的,我解决了。有问题的代码是

starButton.addTarget(self, action: "starButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)

UIControlEvent.TouchUpInside给人一种滞后的错觉。将其更改为 .TouchDown 会好得多。

对于Swift 5

var starButton = UIButton()

    func a ()  {

        starButton = UIButton(type: UIButton.ButtonType.custom)
        starButton.frame = CGRect(x: 100, y: 100, width: 50, height: 50)
        starButton.backgroundColor = .blue
        SpielFenster.addSubview(starButton)

        starButton.addTarget(self, action: #selector(starButtonClicked), for: UIControl.Event.touchDown)
        starButton.adjustsImageWhenHighlighted = false
    }
    @objc func starButtonClicked(){
        animateScaleDown()
    }

    func animateScaleDown(){

        UIView.animate(withDuration: 0.1, animations: {
            self.starButton.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)

        }, completion: { _ in
            self.wait()
        })

    }

    func wait(){
        UIView.animate(withDuration: 0.2, animations: {}, completion: { _ in
            UIView.animate(withDuration: 0.2, animations: {
                self.starButton.transform = CGAffineTransform(scaleX: 1, y: 1)

            })
        })
    }