[英]Swift add Gesture to UIImageView?
Now i am working on IOS project with Swift! 现在,我正在使用Swift进行IOS项目! I already made coverflow object.
我已经做了Coverflow对象。 And now i want some function to run when i click on each images in that coverflow.
现在,我想在单击该Coverflow中的每个图像时运行某些功能。
I tried to add some gesture on it but seems didn't work. 我试图在上面添加一些手势,但是似乎没有用。 This is my code below.
这是我的下面的代码。
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")
}
So if someone know why it didn't work please help me Thanks! 因此,如果有人知道为什么它不起作用,请帮助我,谢谢!
Your function viewIsClicked() doesn't contain any parameters, but your selector contains a colon - "viewIsClicked:", so you probably getting runtime error. 您的函数viewIsClicked()不包含任何参数,但是您的选择器包含一个冒号-“ viewIsClicked:”,因此您可能会遇到运行时错误。
Try to change: 尝试更改:
viewRec.addTarget(self, action: "viewIsClicked:")
To: 至:
viewRec.addTarget(self, action: "viewIsClicked")
Changing following snippet of code will do your work 更改以下代码段即可完成工作
viewRec = UITapGestureRecognizer(target: self, action: "viewIsClicked")
viewRec.numberOfTapsRequired = 1
viewRec.numberOfTouchesRequired = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.