目前,我有一個CustomTableViewCell
,用於四個或五個不同的地方。自定義單元格具有延遲加載的UILongPressGestureRecognizer
屬性,該屬性被添加爲父VC中的cellForRowAtIndexPath
中的手勢識別器。使用自定義UITableViewCell顯示吐司彈出框的模式
self.tableView.addGestureRecognizer(cell.longPress)
當用戶啓動長按,我想Toast通知彈出顯示一些上下文信息,然後在幾秒鐘後消失。我在我的代碼中包含了這個CustomTableViewCell
,但所有這些決定都開始「嗅覺」。是否有更智能,更合乎邏輯的方式來執行這些決定?
此表視圖電池具有以下代碼:
weak var parentTableView: UITableView?
lazy var longPress: UILongPressGestureRecognizer = {
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressSelector))
longPress.minimumPressDuration = 0.5
longPress.delegate = self
return longPress
}()
func longPressSelector(_ longPress: UILongPressGestureRecognizer!) {
if let tableView = self.parentTableView {
let point = longPress.location(in: tableView)
let indexPath = tableView.indexPathForRow(at: point)
if ((indexPath! as NSIndexPath).section == 0 && longPress.state == .began) {
// do some work
// Show informational popup
let toast = createToastNotification(withTitle: addedSong.name)
Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { (timer) -> Void in
UIView.animate(withDuration: 1.0) {() -> Void in
toast.alpha = 0.0
toast = nil
}
}
}
}
}
func createToastNotification(withTitle title: String) -> UIView {
if let tableView = self.parentTableView {
let windowFrame = tableView.superview?.bounds
let height:CGFloat = 145, width: CGFloat = 145
let x = (windowFrame?.width)!/2 - width/2
let y = (windowFrame?.height)!/2 - height/2
let toast = EnsembleToastView.create()
toast.frame = CGRect(x: x, y: y, width: width, height: height)
toast.songLabel.text = title
toast.layer.cornerRadius = 5
tableView.superview?.addSubview(toast)
return toast
}
return UIView()
}
這很完美。我已經開始在委託方向上工作,並且包含CustomTableViewCell上的新選擇器的編輯將其置於頂端。謝謝你的幫助! – dstepan
@dstepan很高興我能幫到你。 –