iT邦幫忙

2022 iThome 鐵人賽

DAY 20
0
自我挑戰組

iOS Junior的菜雞之路系列 第 20

說說看一句話會不會讓我對你再暈船

  • 分享至 

  • xImage
  •  

昨天空畫面為今天一次都沒有配對到的話,會出現一個讓你再配對一次的Label
今天會是做tap 這個Label然後跳出一個View來讓你打上你說對他說的話

邏輯處理

因為沒有特別再去碰AI,所以這次的判斷會是讓他再跑一次random去配對
如果再沒有,可以去拜拜求個平安

    let ran = Int.random(in: 0...9)
    if ran == 1 || ran == 5 || ran == 9 {
        let index = ran / 2
        emptyView.isHidden = true
        matchView.configure(imageURL: data[index])
        matchView.isHidden = false
    }

在EmptyView加入動作

    @IBOutlet weak var tapButton: UILabel! {
        didSet {
            tapButton.isUserInteractionEnabled = true
        }
    }
    
    @objc func tapOneMoreButton() {
        delegate?.tapOneMoreChance()
    }

    weak var delegate: EmptyViewDelegate?

創立Protocol以傳遞Delegate
記得要繼承AnyObject

protocol EmptyViewDelegate: AnyObject {
    func tapOneMoreChance()
}

RetryViewController

創立RetryViewController
建立一個TextView、Button,Button送出按鈕後,就通知到SlideViewController進行上面的邏輯運算

protocol RetryViewControllerDelegate: AnyObject {
    func tapRetryButton()
}

class RetryViewController: UIViewController {

    @IBOutlet weak var titleImageView: UIImageView!
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var sendLabel: UILabel! {
        didSet {
            sendLabel.isUserInteractionEnabled = true
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapSendLabel))
            sendLabel.addGestureRecognizer(tapGesture)
        }
    }

    weak var delegate: RetryViewControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

SlideViewController

extension SlideViewController: EmptyViewDelegate {
    func tapOneMoreChance() {
        let vc = RetryViewController()
        vc.delegate = self
        present(vc, animated: true, completion: nil)
    }
}


extension SlideViewController: RetryViewControllerDelegate {
    func tapRetryButton() {
        let ran = Int.random(in: 0...9)
        if ran == 1 || ran == 5 || ran == 9 {
            let index = ran / 2
            emptyView.isHidden = true
            matchView.configure(imageURL: data[index])
            matchView.isHidden = false
        }
    }
}

上一篇
空畫面的Handler
下一篇
ScrollView 圖片伸縮
系列文
iOS Junior的菜雞之路30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言