samedi 27 juin 2015

UIButton not clickable under UIImageView (swift)

Thanks for reading.

My program adds UIImageViews upon tapping inside a UICollectionViewCell. Once the image is added, there is a separate, static UIButton set to delete any image(s) which intersect the button. The deleting code works fine, but the problem is that the button is not clickable under the programatically-added UIImageView. (If I click on a portion of the button which is not covered by the UIImageView, the button's function is called and the image(s) are properly deleted.)

I have tried view.bringSubviewToFront(UIButton). After doing this, the button worked fine, but now the button was over the UIImageView, so I couldn't move the image outside of the button.

Here is my code for the UIPanGestureRecognizer and for the UIButton:



func handlepan(recognizer: UIPanGestureRecognizer) {
    let movingview = recognizer.view!
    let translation = recognizer.translationInView(recognizer.view)
    view.bringSubviewToFront(movingview)
    movingview.center = CGPoint(x: movingview.center.x + translation.x, y: movingview.center.y + translation.y)

    recognizer.setTranslation(CGPointZero, inView: recognizer.view)
    view.bringSubviewToFront(hungryman)

}


@IBOutlet weak var hungryman: UIButton!

@IBAction func deleteimage(sender: AnyObject) {

    var count = 0
    indexestodelete = []

    for i in unlockeddisplaying {


        if i.frame.intersects(hungryman.frame) {

            i.removeFromSuperview()
            indexestodelete.append(count)
        }
        count = count + 1

    }

    count = 0

    for i in indexestodelete {

        unlockeddisplaying.removeAtIndex(i - count)
        unlockeddisplayingtypes.removeAtIndex(i - count)
        count = count + 1

    }
}

Thanks!

TL;DR: How to make UIButton always clickable, even when hidden under UIImageView?

Aucun commentaire:

Enregistrer un commentaire