画面端のゴミ箱までドラッグすると、ゴミ箱が開いてコンテンツを消せるやーつです。
// pan ジェスチャをつける
@IBAction func pan(_ sender: UIPanGestureRecognizer) {
panObject(recognizer: sender)
}
func panObject(recognizer:UIPanGestureRecognizer) {
switch recognizer.state {
case .changed: // 移動中
let location = recognizer.location(in: view) // 触ってる場所を取得
trashLabel.center = location
if trashView.frame.contains(location) { // もしゴミ箱と重なっていたら
// ゴミ箱を赤くする
trashView.image = UIImage(systemName: "trash.circle.fill")
trashView.tintColor = .red
} else {
// 重なっていなかったらゴミ箱を元に戻す
trashView.image = UIImage(systemName: "trash.circle")
trashView.tintColor = .systemBlue
}
default: // 指を離した時
let location = recognizer.location(in: view) // 触ってる場所を取得
if trashView.frame.contains(location) { // もしゴミ箱と重なっていたら
trashLabel.removeFromSuperview() // ラベルを消す
}
}
}
🐣
フリーランスエンジニアです。
お仕事のご相談こちらまで
rockyshikoku@gmail.com
Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。