简体   繁体   中英

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

Edit: I move the added image views around the screen using a UIGestureRecognizer and the handlepan function.

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.

Edit 2: I found a workaround which bypasses the button functionality all together and simply deletes the image if its view intersects the button view at the end of the pan gesture.

//I added this code to the end of the handlepan function (and made a function calleddeleteimage which performs the actions of delete image):

if recognizer.state == UIGestureRecognizerState.Ended {
        calleddeleteimage()
        }

//Here is my original 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?

在图像视图上,将userInteractionEnabled设置为false ,触摸就会通过它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM