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)


        }

動的に追加したViewをあとから削除する

今回は動的に追加したのがボタンなので、
あとからボタンを削除するようにした時のことを書いてみる

まず、動的に追加したViewをあとから操作するには、生成時にタグをつける必要がある
ユニークなIDです

btn.tag = 10

そして、View(この場合、あとから追加したボタンはサブビューとなる)を削除するには以下のサイトを参考に

teratail.com

んでもって、どのボタンが押されたかを判定しなくてはいけないので、
押されたボタンのタグを取得して、削除メソッドに渡す

ふつうのタップ時のタグ取得は他のサイトにいっぱい載っているので、
長押ししたときのタグ取得方法をメモっておく

@IBAction func longpushed(_ sender: UILongPressGestureRecognizer){
        delView(tag: (sender.view?.tag)!)
    }

delViewってのは自前のView削除関数です

長押しイベントを動的に生成したボタンに追加する

hacknote.jp

ここの記載内容を参考にイベントを追加する

結局できたコードが以下

// 長押し時のイベントを追加
btn.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpushed(_:))))
        

過去に同じようにドラッグイベントを追加したときの記事
ramka.hateblo.jp

swiftで録音する

qiita.com

このままコピペしても動きませんでした

recorde関数のsetCategoryがそのままできなかったので、
以下のように変更

//            try! session.setCategory(AVAudioSessionCategoryPlayAndRecord)
            try! session.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)

record関数で変なエラーが出て、
「info.plistにちゃんとマイクにアクセスするってことを書いて」って怒られるので、
info.plistに以下を追加してみる

f:id:ramka:20190105232011p:plain
info.plistに追加



[-以下が追記-]
録音を行う関数の中に、再生を行うためのイニシャライズ処理も入っているようでした
録音直後は再生できるけど、一度アプリを閉じた直後に再生できなくなるのはそのためっだったようです

以下が再生も行うためのソース

        let session = AVAudioSession.sharedInstance()
        try! session.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
        try! session.setActive(true)

これをしないと再生できない

UIColor設定できないよ!CGColorじゃね?って怒られるとき

こんな感じでボタンの線の色をグレイにしようとすると怒られる

btn.layer.borderColor = UIColor.lightGray

Cannot assign value of type 'UIColor' to type 'CGColor?'


こうするといい

btn.layer.borderColor = UIColor.lightGray.cgColor