快速获取点击的图像视图
Posted
技术标签:
【中文标题】快速获取点击的图像视图【英文标题】:get the clicked imageview swift 【发布时间】:2014-11-12 06:42:06 【问题描述】:我想知道我正在使用以下代码的用户单击了哪个图像视图,但它没有调用leftTapView
和rightTapView
函数
class CustomTableViewCell : UITableViewCell
@IBOutlet var leftTeamImage: UIImageView!
@IBOutlet var rightTeamImage: UIImageView!
@IBOutlet var rightTeamNameLabel: UILabel!
@IBOutlet var leftTeamNameLabel: UILabel!
@IBOutlet var leftTeamScoreLabel: UILabel!
@IBOutlet var rightTeamScoreLabel: UILabel!
@IBOutlet var leftView : UIView!
@IBOutlet var rightView : UIView!
let lettTapRec = UITapGestureRecognizer()
let rightTapRec = UITapGestureRecognizer()
ScoreLabel.text = leftTeamScore
rightTeamNameLabel.text = rightTeamName.uppercaseString
rightTeamScoreLabel.text = rightTeamScore
func load(#leftTeamName: String, rightTeamName: String, leftTeamScore: Int, rightTeamScore: Int)
leftTeamNameLabel.text = leftTeamName.uppercaseString
leftTeamScoreLabel.text = String(leftTeamScore)
rightTeamNameLabel.text = rightTeamName.uppercaseString
rightTeamScoreLabel.text = String(rightTeamScore)
lettTapRec.addTarget(self, action: "leftTapView")
leftView.userInteractionEnabled = true
leftView.addGestureRecognizer(lettTapRec)
//
rightTapRec.addTarget(self, action: "rightTapView")
rightView.userInteractionEnabled = true
rightView.addGestureRecognizer(rightTapRec)
func leftTapView(sender: AnyObject)
println("left")
func rightTapView(sender: AnyObject)
println("left")
protocol ImageTappedProtocols
func leftImageTapped(imageView : UIImage)
func rightImageTapped(imageView: UIImage)
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
@IBOutlet var tableView : UITableView!
var items: [(String, String)] = [
("My", "swift 1.jpeg"),
("Name", "swift 2.jpeg"),
("is", "swift 3.jpeg"),
("Atif", "swift 4.jpeg"),
("Farrukh", "swift 5.jpeg")
]
var team1 :[String] = []
var team2 :[String] = []
var id : [String] = []
var team1_bets : [Int] = []
var team2_bets : [Int] = []
var end_date : [String] = []
var list = Dictionary<String, String>()
var count : Int = 0
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return count;
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
var cell:CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("customCell") as CustomTableViewCell
// this is how you extract values from a tuple
var (title, image) = items[indexPath.row]
var teama: String = team1[indexPath.row]
var teamb: String = team2[indexPath.row]
var teamAScore : Int = team1_bets[indexPath.row]
var teamBScore : Int = team2_bets[indexPath.row]
cell.backgroundColor = UIColor(red: 123, green: 225, blue: 38, alpha: 0.2)
return cell
func tableView(tableView : UITableView, didSelectedRowAtIndexPath indexPath : NSIndexPath)
//tableView.deselectRowAtIndexPath(indexPath, animated: true)
println("You selected cell #\(indexPath.row)!")
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
println(currentCell.textLabel.text)
func tableView(tableView : UITableView, didSelectRowAtIndexPath indexPath : NSIndexPath)
//tableView.deselectRowAtIndexPath(indexPath, animated: true)
println("You selected cell #\(indexPath.row)!")
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
println(currentCell.contentView)
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let urlAsString = "http://codespikestudios.com/betting_app/bet/get_events/1"
//let urlAsString = "http://api.topcoder.com/v2/challenges?pageSize=2"
let url: NSURL = NSURL(string: urlAsString)!
let urlSession = NSURLSession.sharedSession()
//2
let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: data, response, error -> Void in
if (error != nil)
println(error.localizedDescription)
var err: NSError?
// 3
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSArray
if (err != nil)
println("JSON Error \(err!.localizedDescription)")
// 4
println(jsonResult)
println(jsonResult.count)
println(jsonResult[1].valueForKey("cat_name") as String)
var dictionary = Dictionary<String, String>()
self.count = jsonResult.count
for var i = 0; i < self.count; i++
self.team1 = jsonResult.valueForKey("team1") as Array
self.team2 = jsonResult.valueForKey("team2") as Array
self.team1_bets = jsonResult.valueForKey("team1_bets") as Array
self.team2_bets = jsonResult.valueForKey("team2_bets") as Array
dispatch_async(dispatch_get_main_queue(), () -> Void in
self.tableView.reloadData()
)
//let jsonDate: String! = jsonResult["date"] as NSString
//let jsonTime: String! = jsonResult["time"] as NSString
//println(jsonTime)
)
// 5
jsonQuery.resume()
var nib = UINib(nibName: "MyTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "customCell")
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
这就是我得到的全部
You selected cell #3!
<UITableViewCellContentView: 0x7f871b687350; frame = (0 0; 580 136.5); opaque = NO; gestureRecognizers = <NSArray: 0x7f871b68c180>; layer = <CALayer: 0x7f871b687420>>
【问题讨论】:
【参考方案1】:您需要在初始化手势识别器时添加一个目标。例如,这看起来像:
let leftTapRect = UIGestureRecognizer(target: self, action: "leftTapView:")
此外,您需要向事件处理程序 leftTapView 和 rightTapView 添加一个 sender 参数:
func leftTapView(sender: AnyObject)
...
【讨论】:
编辑了我的问题,请看一下。还是一样的反应。 我相信将事件发送给像您的点击识别器这样的表格视图单元格的子级会带来一些并发症。我建议朝那个方向搜索。祝你好运。 谢谢你解决了我的问题,实际上我已经将视图的 alpha 设置为0
,当我将它设置为1
并将背景设置为Clear Colour
它开始工作了,你能请告诉为什么制作 alpha 0 不起作用?【参考方案2】:
解决了我的问题,实际上我已经将视图的 alpha 设置为 0,当我将其设置为 1 并将背景设置为 Clear Colour
它开始工作。完美地休息工作。如果有人可以解释为什么设置 alpha 不会触发点击手势的原因,我很乐意接受 hi/她的回答。
【讨论】:
【参考方案3】:alpha
为 0.0 的视图不会接收触摸事件。
来自Event Handling 指南:
“请注意,如果视图是隐藏的或透明的,它也不会接收触摸事件。”
来自Creating Views 文档:
“隐藏视图不接收来自系统的触摸事件。”
【讨论】:
以上是关于快速获取点击的图像视图的主要内容,如果未能解决你的问题,请参考以下文章