UITapGestureRecognizer只调用一次

时间:2016-03-11 13:15:53

标签: ios swift uigesturerecognizer uitapgesturerecognizer

我有这段代码:

func initPlaceHolder(width: CGFloat, height: CGFloat){
    var firstPlaceHolderPosition: CGFloat = 0;

    UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, 0)

    let context = UIGraphicsGetCurrentContext()
    let rectangle = CGRect(x: 0, y: 0, width: width, height: height)

    CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextSetStrokeColorWithColor(context, UIColor.blackColor().CGColor)
    CGContextSetLineWidth(context, 1)

    CGContextAddRect(context, rectangle)
    CGContextDrawPath(context, .FillStroke)

    let img = UIGraphicsGetImageFromCurrentImageContext()

    for i in 1...4 {
        let imageView = StompUIImageView(frame: CGRect(x: firstPlaceHolderPosition, y: 0, width: width, height: height))
        let tap = UITapGestureRecognizer(target: self, action: "doubleTapped:")

        tap.numberOfTapsRequired = 2

        imageView.image = img
        imageView.addGestureRecognizer(tap)
        imageView.userInteractionEnabled = true
        imageView.stompID = String(i)
        imageView.stompSlot = i

        addSubview(imageView)

        firstPlaceHolderPosition = firstPlaceHolderPosition + width + 10

        UIGraphicsEndImageContext()
    }
}

func doubleTapped(sender: UITapGestureRecognizer) {
    let view = sender.view as! StompUIImageView
    print(view.stompID)
}

基本上,doubleTapped处理程序仅针对第一个UIImageView而不是针对所有4个调用。 对不起是ios开发的新手我很难理解。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

试试这个......

用这个替换你的代码......

我希望这会对你有所帮助。

func initPlaceHolder(width: CGFloat, height: CGFloat){
    var firstPlaceHolderPosition: CGFloat = 0;

    UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, 0)

    let context = UIGraphicsGetCurrentContext()
    let rectangle = CGRect(x: 0, y: 0, width: width, height: height)

    CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextSetStrokeColorWithColor(context, UIColor.blackColor().CGColor)
    CGContextSetLineWidth(context, 1)

    CGContextAddRect(context, rectangle)
    CGContextDrawPath(context, .FillStroke)

    let img = UIGraphicsGetImageFromCurrentImageContext()

    let tap = UITapGestureRecognizer(target: self, action: "doubleTapped:")

    tap.numberOfTapsRequired = 2


    for i in 1...4 {
        let imageView = StompUIImageView(frame: CGRect(x: firstPlaceHolderPosition, y: 0, width: width, height: height))

        imageView.image = img
        imageView.addGestureRecognizer(tap)
        imageView.userInteractionEnabled = true
        imageView.stompID = String(i)
        imageView.stompSlot = i

        addSubview(imageView)

        firstPlaceHolderPosition = firstPlaceHolderPosition + width + 10

        UIGraphicsEndImageContext()
    }
}

func doubleTapped(sender: UITapGestureRecognizer) {
    let view = sender.view as! StompUIImageView
    print(view.stompID)
}

答案 1 :(得分:0)

我的代码很好地回答了我自己的问题。 问题是这一行:addSubview(imageView)

我在一个小于所有占位符宽度总和的容器中添加了4个占位符(大约300px)。 虽然我看到所有占位符都正确显示,但框架不足以容纳它们,因此无法识别双击。 使容器更大化解决了这个问题。

相关问题