现在我正在使用Swift开发IOS项目! 我已经制作了coverflow对象。 现在,当我点击该封面流中的每个图像时,我想要运行一些功能。
我尝试在其上添加一些手势,但似乎无效。 这是我的代码。
func carousel(carousel: iCarousel!, viewForItemAtIndex index: Int, var reusingView view: UIView!) -> UIView!
{
var label: UILabel! = nil
var labelSecond: UILabel! = nil
var labelVol: UILabel! = nil
var viewRec: UITapGestureRecognizer! = nil
var itemsVol = [itemsInside01.count,itemsInside02.count,itemsInside03.count,itemsInside04.count,itemsInside05.count]
println(itemsVol[2])
//create new view if no view is available for recycling
if (view == nil)
{
//don't do anything specific to the index within
//this `if (view == nil) {...}` statement because the view will be
//recycled and used with other index values later
view = UIImageView(frame:CGRectMake(0, 0, 200, 156))
(view as UIImageView!).image = UIImage(named: "coverFlowBg.png")
view.contentMode = .Center
var wBounds = view.bounds.width
var hBounds = view.bounds.height
var labelSize = CGRect(x: 0, y: hBounds, width: wBounds, height: 52)
var labelSmallSize = CGRect(x: 0, y: hBounds + 30, width: wBounds, height: 24)
label = UILabel()
label.frame = labelSize
label.backgroundColor = UIColor.clearColor()
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
label.font = UIFont(name: "supermarket" , size: 18)
label.tag = 1
labelSecond = UILabel()
labelSecond.frame = labelSize
labelSecond.backgroundColor = UIColor.clearColor()
labelSecond.textAlignment = .Center
labelSecond.textColor = UIColor.whiteColor()
labelSecond.font = UIFont(name: "supermarket" , size: 18)
labelSecond.tag = 1
labelVol = UILabel()
labelVol.frame = labelSmallSize
labelVol.backgroundColor = UIColor.clearColor()
labelVol.textAlignment = .Center
labelVol.textColor = UIColor.whiteColor()
labelVol.font = UIFont(name: "supermarket" , size: 12)
labelVol.tag = 1
imageTypeIcon = UIImage(named: cardTypeImageSrc[index])
imageTypeIconView = UIImageView(image: imageTypeIcon)
imageTypeIconView.contentMode = .Center
imageTypeIconView.frame = CGRect(x: 0, y: 0, width: wBounds, height: hBounds)
viewRec = UITapGestureRecognizer()
viewRec.addTarget(self, action: "viewIsClicked:")
imageTypeIconView.addGestureRecognizer(viewRec)
imageTypeIconView.userInteractionEnabled = true
view.addSubview(label)
//view.addSubview(labelSecond)
view.addSubview(labelVol)
view.addSubview(imageTypeIconView)
}
else
{
//get a reference to the label in the recycled view
label = view.viewWithTag(1) as UILabel!
}
//set item label
//remember to always set any properties of your carousel item
//views outside of the `if (view == nil) {...}` check otherwise
//you'll get weird issues with carousel item content appearing
//in the wrong place in the carousel
label.text = "\(items[index])"
//labelSecond.text = "\(items[index])"
labelVol.text = "\(itemsVol[index]) " + "ชุดการ์ด"
return view
}
func carousel(carousel: iCarousel!, valueForOption option: iCarouselOption, withDefault value: CGFloat) -> CGFloat
{
if (option == .Spacing)
{
return value * 1
}
return value
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func goBackToIndex(sender: AnyObject) {
if let navController = self.navigationController {
navController.popViewControllerAnimated(true)
}
}
func viewIsClicked(){
println("uuteosuteo")
}
所以,如果有人知道它为什么不起作用,请帮助我 谢谢!
答案 0 :(得分:1)
您的函数 viewIsClicked()不包含任何参数,但您的选择器包含冒号 - &#34; viewIsClicked:&#34;,因此您可能会收到运行时错误。< / p>
尝试改变:
viewRec.addTarget(self, action: "viewIsClicked:")
要:
viewRec.addTarget(self, action: "viewIsClicked")
答案 1 :(得分:1)
更改以下代码段将完成您的工作
viewRec = UITapGestureRecognizer(target: self, action: "viewIsClicked")
viewRec.numberOfTapsRequired = 1
viewRec.numberOfTouchesRequired = 1