added button to sceneKit view but it has a lag

梦想与她 提交于 2019-12-04 20:47:30

问题


I added a custom button to the sceneKit view. When it is touched, it plays an animation, indicating that it was clicked. The problem I'm facing is the delay between user touch and start of animation. My scene has 28.1K triangles and 84.4K vertices. Is that to much or do I need to implement buttons differently. The scene renders with 60fps. I added the button via sceneView.addSubview: Thanks for answers

     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)

            })
        })
    }

回答1:


Okay I solved it. The problematic piece of code is

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

UIControlEvent.TouchUpInside gives the illusion of lag. Changing it to .TouchDown is much better.




回答2:


For 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)

            })
        })
    }


来源:https://stackoverflow.com/questions/31514935/added-button-to-scenekit-view-but-it-has-a-lag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!