向sceneKit视图添加了按钮,但它有一个滞后

时间:2015-07-20 11:08:10

标签: ios uibutton scenekit

我在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)

            })
        })
    }

2 个答案:

答案 0 :(得分:6)

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

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

UIControlEvent.TouchUpInside 给出了滞后的错觉。将其更改为 .TouchDown 要好得多。

答案 1 :(得分:0)

对于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)

            })
        })
    }