1

I have this code:

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)
}

Basically the doubleTapped handler is called only for the first UIImageView and not for all 4. Sorry being new to ios development I have difficulties to understand way.

Thanks for any help

iFrankz
  • 95
  • 5
  • At the first glance nothing's wrong with your code. And you're right that you use separate gestures for imageviews. Don't use single gesture as suggested in the answer below for multiple imageViews - in this case the gesture will work only for the last view. Where do you call `initPlaceHolder`? Is this a part of another uiview? how this uiview is used? – Nimble Mar 11 '16 at 13:46

2 Answers2

0

Try this ...

Replace your code with this one....

I hope this will help you.

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)
}
DJ1
  • 936
  • 15
  • 29
  • Thanks for your reply. Your code was my first solution and it didn't work at all. I am not sure but maybe you can have just one view per gesture? This is why I am creating multiple instance of the same recognizer. – iFrankz Mar 11 '16 at 13:32
  • @iFrankz you can give tag to image view and then differentiate `imageView` gesture by using tags. – DJ1 Mar 11 '16 at 13:35
  • for reference http://stackoverflow.com/questions/4747238/can-you-attach-a-uigesturerecognizer-to-multiple-views – Nimble Mar 11 '16 at 13:48
0

My code was fine answering my own issue. The problem was the line: addSubview(imageView)

I was adding 4 placeholder (about 300px) to a container smaller than the sum of the widths of all my placeholders. Although I was seeing all the placeholders correctly displayed, the frame wasn't big enough to contain them and so the double tap not recognised. Making the container bigger solved the issue.

iFrankz
  • 95
  • 5