added button to sceneKit view but it has a lag

后端 未结 2 1529
半阙折子戏
半阙折子戏 2021-02-15 12:48

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

相关标签:
2条回答
  • 2021-02-15 13:07

    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)
    
                })
            })
        }
    
    0 讨论(0)
  • 2021-02-15 13:16

    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.

    0 讨论(0)
提交回复
热议问题