UIButton在UIImageView(swift)下无法点击

时间:2015-06-28 02:47:54

标签: ios iphone swift uiimageview uibutton

感谢阅读。

我的程序在点击UICollectionViewCell时添加了UIImageViews。添加图像后,会有一个单独的静态UIButton集,用于删除与按钮相交的任何图像。删除代码工作正常,但问题是按钮在编程添加的UIImageView下无法点击。 (如果我点击UIImageView未覆盖的按钮部分,则会调用按钮的功能并正确删除图像。)

编辑:我使用UIGestureRecognizer和句柄功能在屏幕上移动添加的图片视图。

我尝试过view.bringSubviewToFront(UIButton)。执行此操作后,按钮工作正常,但现在按钮位于UIImageView上方,因此我无法将图像移动到按钮之外。

编辑2:我找到了一种解决方法,它可以一起绕过按钮功能,如果图像的视图与平移手势结束时的按钮视图相交,则只删除图像。

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

谢谢!

TL; DR:如何使UIButton始终可点击,即使隐藏在UIImageView下也是如此?

1 个答案:

答案 0 :(得分:1)

在图片视图中,将userInteractionEnabled设置为false,触摸将通过它。