簡體   English   中英

UIButton在UIImageView(Swift)下不可單擊

[英]UIButton not clickable under UIImageView (swift)

謝謝閱讀。

我的程序在點擊UICollectionViewCell內部時添加UIImageViews。 添加圖像后,將設置一個單獨的靜態UIButton來刪除與該按鈕相交的所有圖像。 刪除代碼可以正常工作,但是問題在於在以編程方式添加的UIImageView下,該按鈕不可單擊。 (如果單擊按鈕的未被UIImageView覆蓋的部分,則會調用該按鈕的功能,並且已正確刪除圖像。)

編輯:我使用UIGestureRecognizer和handlepan函數在屏幕上移動添加的圖像視圖。

我已經嘗試過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:即使隱藏在UIImageView下,如何使UIButton始終可單擊?

在圖像視圖上,將userInteractionEnabled設置為false ,觸摸就會通過它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM