【问题标题】:Swift add Gesture to UIImageView?Swift将手势添加到UIImageView?
【发布时间】:2015-04-20 12:29:05
【问题描述】:

现在我正在使用 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")
    }

所以如果有人知道它为什么不起作用,请帮助我 谢谢!

【问题讨论】:

    标签: ios iphone xcode swift uiimageview


    【解决方案1】:

    更改以下代码的 sn-p 即可完成您的工作

    viewRec = UITapGestureRecognizer(target: self, action: "viewIsClicked")
    viewRec.numberOfTapsRequired = 1
    viewRec.numberOfTouchesRequired = 1
    

    【讨论】:

      【解决方案2】:

      您的函数 viewIsClicked() 不包含任何参数,但您的选择器包含一个冒号 - “viewIsClicked:”,因此您可能会遇到运行时错误。

      尝试改变:

          viewRec.addTarget(self, action: "viewIsClicked:")
      

      收件人:

          viewRec.addTarget(self, action: "viewIsClicked")
      

      【讨论】:

      • 这是正确的。如果你想访问手势(使用带有冒号的选择器),你必须接受手势作为参数:func viewIsClicked(gesture: UITapGestureRecognizer) { }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多