簡體   English   中英

在uicollectionviewCell內聚焦uibutton

[英]Focus uibutton inside uicollectionviewCell

我有一個自定義UICollectionView Cell 單元格的布局看起來像這樣

 - ContentView
    - UILabel
    - UIView --> buttonView
          - UIButton

ButtonViews hidden屬性標准設置為true。 當用戶點擊單元格時。 ButtonView變為可見。

我現在的問題是,我沒有將注意力集中在buttonView內的uibutton

這是我的代碼:

在我的viewController中:

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! ShoppingCartCell

        cell.buttonView.hidden = false
        cell.buttonView.updateFocusIfNeeded()

    }

    func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
            return true
    }

在我的自定義單元格中:

   public override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        if (self.focused)
        {
            self.transform = CGAffineTransformMakeScale(1.1, 1.1)
        }
        else
        {
            self.transform = CGAffineTransformIdentity
           buttonView.hidden = true
        }


        super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)

        guard let nextFocusedView = context.nextFocusedView else { return }

        switch nextFocusedView {
        case self.buttonView:
            self.focusGuide.preferredFocusedView = self.deleteButton
        default:
            self.focusGuide.preferredFocusedView = nil
        }

    }

您為特定條件隱藏了按鈕視圖,但對於其他條件則不再顯示。 試試這個代碼,

public override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        if (self.focused)
        {
            self.transform = CGAffineTransformMakeScale(1.1, 1.1)
            buttonView.hidden = false
        }
        else
        {
            self.transform = CGAffineTransformIdentity
           buttonView.hidden = true
        }

在嘗試了不同的方法之后,我將UIButton子類化為您在單元格中使用的按鈕,並注意到該調用

self.focusGuide.preferredFocusedView = cell.aButton

這不能使按鈕成為焦點,針對該按鈕的self.focused為false,以及未調用該按鈕類中的didUpdateFocusInContext。

作為臨時修復和/或將來的探索/實驗,您可以“假裝”專注於按鈕。 為此,正如我之前提到的,從UIButton繼承子類並創建自己的按鈕類。

在該類中添加以下內容:

func makeFocused()
{
        UIView.animateWithDuration(0.2, animations:
            {
                self.transform = CGAffineTransformMakeScale(1.1, 1.1)
            },
            completion:
            {
                finished in

                UIView.animateWithDuration(0.2, animations:
                    {
                        self.transform = CGAffineTransformIdentity
                    },
                    completion: nil)
        })

        self.layer.shadowOffset = CGSizeMake(10, 10);
        self.layer.shadowOpacity = 0.6;
        self.layer.shadowRadius = 15;
        self.layer.shadowColor = UIColor.blackColor().CGColor;
}

同樣添加散焦方法。 然后在以下按鈕的內部調用焦點/未焦點:

class YourCustomCell: UICollectionViewCell {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var aButton: CustomButton!

private var focusGuide = UIFocusGuide()

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
{
    super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)

    guard let nextFocusedView = context.nextFocusedView else { return }
    guard let prevFocusedView = context.previouslyFocusedView else { return }

    if let cell = nextFocusedView as? YourCustomCell
    {
        cell.aButton.makeFocused()
        self.setNeedsFocusUpdate()
        return
    }
}

希望這可以幫助 :)

暫無
暫無

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

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