ramkaの朝ごはんはピーナッツパン

超個人的備忘録です。基本自分のためなので、内容の不備、読みづらさ、つまらなさはご了承ください。

ボタンを押されている間の処理を作る

qiita.com

押されている間、離した時にアニメーションを行う方法です

もちろんそのままでは動かないので一部修正
コメントアウトしているところです

また、パラメータ(色とか拡大率とか)も微妙に変えているので注意

        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            super.touchesBegan(touches, with: event)
            self.touchStartAnimation()
        }
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
            super.touchesMoved(touches, with: event)
            self.touchStartAnimation()
        }
        
        override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
            super.touchesCancelled(touches, with: event)
            self.touchEndAnimation()
        }
        
        
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            super.touchesEnded(touches, with: event)
            self.touchEndAnimation()
        }
        
        private func touchStartAnimation(){
//            UIView.animateWithDuration(0.1,
//                                       delay: 0.0,
//                                       options: UIView.AnimationOptions.CurveEaseIn,
//                                       animations: {() -> Void in
//                                        self.transform = CGAffineTransformMakeScale(0.95, 0.95);
//                                        self.alpha = 0.7
//            },
//                                       completion: nil
//            )
            
            UIView.animate(withDuration: 0.1, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {() -> Void in self.transform = CGAffineTransform(scaleX: 0.6, y: 0.6);self.alpha=0.7}, completion: nil)
        }
        
        private func touchEndAnimation(){
//            UIView.animateWithDuration(0.1,
//                                       delay: 0.0,
//                                       options: UIView.AnimationOptions.CurveEaseIn,
//                                       animations: {() -> Void in
//                                        self.transform = CGAffineTransformMakeScale(1.0, 1.0);
//                                        self.alpha = 1
//            },
//                                       completion: nil
//            )

            UIView.animate(withDuration: 0.1, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {() -> Void in self.transform = CGAffineTransform(scaleX: 1.0, y: 1.0);self.alpha=1.0}, completion: nil)


        }