2016-02-15 50 views
0

我有一個自定義UICollectionView Cell。單元格的佈局看起來像這樣uicollectionviewCell中的重點uibutton

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

ButtonViews隱藏屬性是標準設置爲true。 當用戶點擊單元格時。 ButtonView變得可見。

什麼我現在的問題是,是,我沒有得到集中到uibuttonbuttonView

這是我的代碼段內:

在我的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 
     } 

    } 
+0

它是一個自定義的UIButton?如果是這樣,你需要自己處理焦點外觀。 – Michael

+0

不,這是一個標準的UIButton – Steaphann

+0

@Steaphann試試我的答案。 –

回答

0

隱藏buttonview對於特定的情況,但你不會再顯示另一個條件。試試這個代碼,

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 
     } 
+0

我試過了,但沒有任何區別 – Steaphann

0

嘗試不同的東西,我子類的UIButton你的按鈕,在單元格中使用後,並注意到呼籲

self.focusGuide.preferredFocusedView = cell.aButton 

未進行對焦按鈕,self.focused在那個按鈕上是錯誤的,以及那個按鈕類中的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 
    } 
} 

希望這有助於:)

相關問題