Skip to content

Instantly share code, notes, and snippets.

@mosluce
Forked from g001613001/handleTapGesture.swift
Last active December 22, 2016 11:10
Show Gist options
  • Save mosluce/74aa42e9ffa8b8b632e701407b4fbea5 to your computer and use it in GitHub Desktop.
Save mosluce/74aa42e9ffa8b8b632e701407b4fbea5 to your computer and use it in GitHub Desktop.
func handleTapGesture(_ recognizer: UITapGestureRecognizer) {
if recognizer.state == .ended {
let point = recognizer.location(in: recognizer.view)
if let indexPath = tableView.indexPathForRow(at: point) {//得到indexPath
if indexPath.section == 0 {
if inApp.Visible{
if ( inApp.ticketings.count > 0 || inApp.Month.count > 0 ) {
if indexPath.row == (inApp.ticketings.count + inApp.Month.count) {
if selectedRow >= 0 {
let selIndexPath = NSIndexPath(row: selectedRow, section: indexPath.section)
buyTicketType(indexPath: selIndexPath)
}
} else { // 勾選
if let cell = tableView.cellForRow(at: indexPath) as? MyTicketTVCell {
let pointInCell = recognizer.location(in: cell)
if (cell.lbTicketingName.frame).contains(pointInCell) {
selectedRow = indexPath.row
tableView.reloadData() //// 這裡勾選
setPID(indexPath: indexPath as NSIndexPath)
} else if (cell.imgTicketing.frame).contains(pointInCell) {
selectedRow = indexPath.row
tableView.reloadData() //// 這裡勾選
setPID(indexPath: indexPath as NSIndexPath)
}
}
}
}
view.endEditing(true)
}else{
view.endEditing(true)
if let cell = tableView.cellForRow(at: indexPath) as? MyTicketTVCell {
let pointInCell = recognizer.location(in: cell)
if (cell.btnConfirm.frame).contains(pointInCell) {
api2012_IPassVerify(iPass: cell.tfIPassNum.text!)
}
}
}
} else {
view.endEditing(true)
if let cell = tableView.cellForRow(at: indexPath) as? MyTicketTVCell {
let pointInCell = recognizer.location(in: cell)
if (cell.btnConfirm.frame).contains(pointInCell) {
api2012_IPassVerify(iPass: cell.tfIPassNum.text!)
}
}
}
}
}
}
func buyTicketType(indexPath : NSIndexPath) {
// 執行VC
//performSegue(withIdentifier: "IAP", sender: indexPath)
//return
// original use View
let maxInt = inApp.ticketings.count + inApp.Month.count + 1
let ticketDetailView = Bundle.main.loadNibNamed("TicketDetailView", owner: self, options: nil)?[0] as! TicketDetailView
ticketDetailView.tvDescription.text = "注意事項:\n凡付費後即無法更改,亦不符合七天鑑賞期退費規定,恕無法退換票。所有影片版權歸高雄市電影館所有,請勿未經授權轉錄,拷貝,重置影片。影片串流限定台,澎,金,馬地區,購買前請斟酌。"
ticketDetailView.didTapBuy = {
self.requestProductInfo()
self.blockUI(true)
}
UIApplication.shared.keyWindow!.addSubview(ticketDetailView)
ticketDetailView.constraintToFit(toView: UIApplication.shared.keyWindow!)
if inApp.ticketings.count > 0 {
if indexPath.row < inApp.ticketings.count {
//特別影展票
let ticket = inApp.ticketings[indexPath.row]
//ticket.log()
ticketDetailView.lbDate.text = ticket.Date
ticketDetailView.lbTitle.text = "\(ticket.Name) \(ticket.FormattedPrice)"
} else if indexPath.row >= inApp.ticketings.count && indexPath.row < maxInt {
//雲端戲院月票
let month = inApp.Month[indexPath.row-inApp.ticketings.count] as FilmFestival
//month.log()
ticketDetailView.lbTitle.text = "\(month.Name) \(month.FormattedPrice)"
}
} else {
//雲端戲院月票
let month = inApp.Month[indexPath.row-inApp.ticketings.count] as FilmFestival
//month.log()
ticketDetailView.lbTitle.text = "\(month.Name) \(month.FormattedPrice)"
}
}
// 就上面的案例重組一下
import UIKit
struct FilmFestival {
var Name: String = ""
var FormattedPrice: Int = 0
}
struct Ticketing {
var Name: String = ""
var FormattedPrice: Int = 0
var Date: String = ""
}
class TicketDetailView: UIView {
var lbDate = UILabel()
var lbTitle = UILabel()
}
//模擬資料
let items: [Any] = [
Ticketing(), Ticketing(), Ticketing(), Ticketing(),
FilmFestival(), FilmFestival(), FilmFestival(), FilmFestival(), FilmFestival(), FilmFestival()
]
@objc protocol Tappable {
func didTap(_ gesture: UIGestureRecognizerState)
}
extension UITableViewCell {
open override func awakeFromNib() {
super.awakeFromNib()
if let t = self as? Tappable {
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(t.didTap(_:))))
}
}
}
//目前可以通用一個 delegate
protocol IAPCellDelegate: class {
func iapCell(_ cell: UITableViewCell, title: String, date: String?)
}
class TicketCell: UITableViewCell, Tappable {
weak var delegate: IAPCellDelegate?
var data: Ticketing!
func didTap(_ gesture: UIGestureRecognizerState) {
self.delegate?.iapCell(self, title: "\(data.Name) \(data.FormattedPrice)", date: data.Date)
}
}
class FilmFestivalCell: UITableViewCell, Tappable {
weak var delegate: IAPCellDelegate?
var data: FilmFestival!
func didTap(_ gesture: UIGestureRecognizerState) {
self.delegate?.iapCell(self, title: "\(data.Name) \(data.FormattedPrice)", date: nil)
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(TicketCell.self, forCellReuseIdentifier: "TicketCell")
self.tableView.register(FilmFestivalCell.self, forCellReuseIdentifier: "FilmFestivalCell")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard indexPath.section == 1 else {
return UITableViewCell()
}
let data = items[indexPath.row]
if let data = data as? Ticketing {
let cell = tableView.dequeueReusableCell(withIdentifier: "TicketCell") as! TicketCell
cell.data = data
cell.delegate = self
return cell
}
if let data = data as? FilmFestival {
let cell = tableView.dequeueReusableCell(withIdentifier: "TicketCell") as! FilmFestivalCell
cell.data = data
cell.delegate = self
return cell
}
return UITableViewCell()
}
}
extension ViewController: IAPCellDelegate {
func iapCell(_ cell: UITableViewCell, title: String, date: String?) {
// 顯示 Detail
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment