【问题标题】:UIButton not clickable under UIImageView (swift)UIImageView 下的 UIButton 不可点击(swift)
【发布时间】:2015-09-14 17:39:00
【问题描述】:

感谢阅读。

我的程序在 UICollectionViewCell 内点击时添加 UIImageViews。添加图像后,将设置一个单独的静态 UIButton 以删除与按钮相交的任何图像。删除代码工作正常,但问题是按钮在以编程方式添加的 UIImageView 下不可点击。 (如果我单击 UIImageView 未覆盖的按钮部分,则会调用按钮的函数并正确删除图像。)

编辑:我使用 UIGestureRecognizer 和 handlepan 函数在屏幕上移动添加的图像视图。

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

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

//我将这段代码添加到handlepan函数的末尾(并制作了一个名为deleteimage的函数,它执行删除图像的操作):

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

//这是我的 UIPanGestureRecognizer 和 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 下?

【问题讨论】:

    标签: ios iphone swift uiimageview uibutton


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。也许我不够清楚。我使用 handlepan 功能移动图像视图,所以这不会使图像无法移动吗?我试试看。
    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2012-05-10
    • 2019-04-20
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多