2015-09-22 10 views
9

Tutto ciò che voglio fare è aggiungere un colore di sfondo a un pulsante per tutti gli stati. Ma voglio mantenere l'ombra di messa a fuoco automatica che si ottiene "gratuitamente" quando si utilizza un pulsante di sistema nello storyboard tvOS. Finora non sono stato in grado di trovare una combinazione che permetta questo.Come posso creare un pulsante con un colore di sfondo per tvOS mentre mostro ancora lo stato attivo?

In alternativa, sarei interessato anche a un modo per aggiungere l'ombra a livello di codice quando il pulsante è focalizzato, ma a parte la sottoclasse del pulsante (che non ho ancora provato), non so come farlo o.

+0

La ragione per questo è particolarmente frustrante è che se si aggiunge l'ombra di programmazione, si perde tutta la mezza fresco movimento rimbalzante. – livingtech

+0

Un ingegnere Apple (nei forum dev) ha affermato che è possibile riprodurre il movimento utilizzando la nuova API 'UIMotionEffect': https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UIMotionEffect_class/index. html Non so come, ma potrei giocarci. – livingtech

risposta

6

Override didUpdateFocusInContext metodo e controllare se il prossimo focus vista è il tasto, se sì, quindi personalizzare la propria interfaccia utente, e per impostare di nuovo a orignal assegno Stato context.previousFocusedView era quel pulsante, qualcosa di simile al di sotto

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator 
{ 
    if (context.nextFocusedView == _button) 
    { 
     // set background color 
    } 
    else if (context.previousFocusedView == _button) 
    { 
     // set background color to background 
    } 
} 
+0

Sì, questo è quello che ho fatto ieri. Mi piacerebbe ancora sapere se la mia prima domanda è possibile. – livingtech

+2

Questo non funziona. Non è ancora possibile ignorare il colore di sfondo bianco che viene aggiunto al pulsante attivo. –

+0

La domanda originale era come cambiare il colore di sfondo del pulsante, non il colore di messa a fuoco, e la risposta è la soluzione per quello –

8

È possono aggiungere un'ombra per il pulsante personalizzato come questo:

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator 
{ 
    context.nextFocusedView.layer.shadowOffset = CGSizeMake(0, 10); 
    context.nextFocusedView.layer.shadowOpacity = 0.6; 
    context.nextFocusedView.layer.shadowRadius = 15; 
    context.nextFocusedView.layer.shadowColor = [UIColor blackColor].CGColor; 
    context.previouslyFocusedView.layer.shadowOpacity = 0; 
} 
2

ho trovato qualcosa di meglio:

-(void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator { 
    [coordinator addCoordinatedAnimations:^{ 
     if (self.focused) { 
      self.backgroundColor = [UIColor whiteColor]; 
     } 
     else { 
      self.backgroundColor = [UIColor clearColor]; 
     } 
    } completion:nil]; 
} 
+0

a cosa si riferisce? è un metodo sottoclasse? –

6

non era felice con un semplice cambiamento di colore, così ho fatto un pulsante personalizzato sottoclasse a guardare più come l'animazione di default che si ottiene con i tasti del sistema -

class CustomButton: UIButton 
{ 
    private var initialBackgroundColour: UIColor! 


    required init?(coder aDecoder: NSCoder) 
    { 
     super.init(coder: aDecoder) 

     initialBackgroundColour = backgroundColor 
    } 

    override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) 
    { 
     coordinator.addCoordinatedAnimations(
     { 
      if self.focused 
      { 
       self.backgroundColor = UIColor.whiteColor() 

       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) 
       }) 
      } 
      else 
      { 
       self.backgroundColor = self.initialBackgroundColour 
      } 
     }, 
     completion: nil) 
    } 
} 

Niente di troppo complicato, ma ottiene il lavoro

0

È possibile utilizzare il metodo UIButtonsetBackgroundImage(image: UIImage?, forState state: UIControlState) e passare attraverso un'immagine di colore piatto e lo stato .Normal.

Questa immagine può essere facilmente creato programatically da una e una dimensione di 1x1 UIColor:

func getImageWithColor(color: UIColor, size: CGSize) -> UIImage { 
    let rect = CGRectMake(0, 0, size.width, size.height) 
    UIGraphicsBeginImageContextWithOptions(size, false, 0) 
    color.setFill() 
    UIRectFill(rect) 
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image 
} 
3

La soluzione definitiva con ispirazione da SomaMan. Basta sottoclasse tutti i tuoi pulsanti personalizzati e si ottiene questo:

enter image description here

include: sull'animazione rubinetto e rilasciare e trascinare via.

// 
// CustomFocusButton.swift 
// 

import UIKit 

class CustomFocusButton: UIButton { 


let focusedScaleFactor : CGFloat = 1.2 
let focusedShadowRadius : CGFloat = 10 
let focusedShadowOpacity : Float = 0.25 
let shadowColor = UIColor.blackColor().CGColor 
let shadowOffSetFocused = CGSizeMake(0, 27) 
let animationDuration = 0.2 

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) 
{ 
    coordinator.addCoordinatedAnimations({ 
      if self.focused{ 
       UIView.animateWithDuration(self.animationDuration, animations:{ [weak self]() -> Void in 

         guard let weakSelf = self else {return} 
         weakSelf.transform = CGAffineTransformMakeScale(weakSelf.focusedScaleFactor, weakSelf.focusedScaleFactor) 
         weakSelf.clipsToBounds = false 
         weakSelf.layer.shadowOpacity = weakSelf.focusedShadowOpacity 
         weakSelf.layer.shadowRadius = weakSelf.focusedShadowRadius 
         weakSelf.layer.shadowColor = weakSelf.shadowColor 
         weakSelf.layer.shadowOffset = weakSelf.shadowOffSetFocused 

        },completion:{ [weak self] finished in 

         guard let weakSelf = self else {return} 
         if !finished{ 
          weakSelf.transform = CGAffineTransformMakeScale(weakSelf.focusedScaleFactor, weakSelf.focusedScaleFactor) 
          weakSelf.clipsToBounds = false 
          weakSelf.layer.shadowOpacity = weakSelf.focusedShadowOpacity 
          weakSelf.layer.shadowRadius = weakSelf.focusedShadowRadius 
          weakSelf.layer.shadowColor = weakSelf.shadowColor 
          weakSelf.layer.shadowOffset = weakSelf.shadowOffSetFocused 
         } 

        }) 
      } else { 
       UIView.animateWithDuration(self.animationDuration, animations:{ [weak self]() -> Void in 
        guard let weakSelf = self else {return} 

        weakSelf.clipsToBounds = true 
        weakSelf.transform = CGAffineTransformIdentity 

        }, completion: {[weak self] finished in 
         guard let weakSelf = self else {return} 
         if !finished{ 
          weakSelf.clipsToBounds = true 
          weakSelf.transform = CGAffineTransformIdentity 
         } 
       }) 
      } 
     }, completion: nil) 
} 

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) { 
    UIView.animateWithDuration(animationDuration, animations: { [weak self]() -> Void in 

     guard let weakSelf = self else {return} 
     weakSelf.transform = CGAffineTransformIdentity 
     weakSelf.layer.shadowOffset = CGSizeMake(0, 10); 

    }) 
} 

override func pressesCancelled(presses: Set<UIPress>, withEvent event: UIPressesEvent?) { 

    if focused{ 
     UIView.animateWithDuration(animationDuration, animations: { [weak self]() -> Void in 
      guard let weakSelf = self else {return} 
      weakSelf.transform = CGAffineTransformMakeScale(weakSelf.focusedScaleFactor, weakSelf.focusedScaleFactor) 
      weakSelf.layer.shadowOffset = weakSelf.shadowOffSetFocused 
     }) 
    } 

} 

override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) { 

    if focused{ 
     UIView.animateWithDuration(animationDuration, animations: {[weak self]() -> Void in 

      guard let weakSelf = self else {return} 
      weakSelf.transform = CGAffineTransformMakeScale(weakSelf.focusedScaleFactor, weakSelf.focusedScaleFactor) 
      weakSelf.layer.shadowOffset = weakSelf.shadowOffSetFocused 
     }) 
    } 
} 
} 
0

È possibile impostare l'immagine di sfondo in storyboard a un'immagine che contiene il colore che si desidera

Problemi correlati