2015-09-22 7 views
9

Wszystko, co chcę zrobić, to dodać kolor tła do przycisku dla wszystkich stanów. Ale chcę zachować automatyczny cień skupienia, który otrzymujesz "za darmo", gdy używasz przycisku systemowego w scenorysie tvOS. Do tej pory nie byłem w stanie znaleźć kombinacji, która na to pozwala.Jak mogę utworzyć przycisk z kolorem tła dla TVOS, nadal wyświetlając fokus?

Alternatywnie, byłbym również zainteresowany sposobem programowego dodawania cienia, gdy przycisk jest skupiony, ale brakuje podklasy przycisku (którego jeszcze nie próbowałem), nie wiem jak to zrobić zarówno.

+0

Powodem jest to szczególnie frustrujące jest to, że jeśli dodać cień programowo, można stracić wszystkie fajne pół- bounciness ruchu. – livingtech

+0

Inżynier Apple (na forach programistów) powiedział, że możesz odtworzyć ruch za pomocą nowego API "UIMotionEffect": https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UIMotionEffect_class/index. html Nie jestem pewien jak, ale mogę się z tym bawić. – livingtech

Odpowiedz

6

Zastąp didUpdateFocusInContext metoda i sprawdź, czy obok widok koncentruje się przycisk, jeśli tak następnie dostosować jego UI, i ustawić ją z powrotem do orignal sprawdzić stan context.previousFocusedView było to, że przycisk, coś jak poniżej

- (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

Tak, właśnie to zrobiłem wczoraj. Nadal chciałbym wiedzieć, czy moje pierwsze pytanie jest możliwe. – livingtech

+2

To nie działa. Nadal nie możesz przesłonić białego tła, które zostanie dodane do przycisku skupienia. –

+0

Orignal pytanie brzmiało jak zmienić kolor tła przycisku, a nie kolor ostrości, a odpowiedź jest rozwiązaniem dla tego –

8

You można dodać cień do przycisku niestandardowego tak:

- (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

Znalazłem coś lepszego:

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

o czym jest mowa? to jest metoda podklasy? –

6

nie był zadowolony z prostej zmiany koloru, więc zrobiłem podklasę przycisk zwyczaj wyglądają bardziej jak domyślnej animacji można uzyskać za pomocą przycisków systemowych -

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) 
    } 
} 

Nic skomplikowane, ale wykonuje pracę

0

Można użyć metody UIButtonsetBackgroundImage(image: UIImage?, forState state: UIControlState) i przejść przez obraz o jednolitym kolorze i stanie .Normal.

Ten obraz może być łatwo tworzone programowo z UIColor i wielkości 1x1:

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

ostateczne rozwiązanie z inspiracji SomaMan. Tylko podklasy wszystkie niestandardowe przyciski i masz to:

enter image description here

obejmuje: o animacji z kranu i zwolnij i przeciągnij go.

// 
// 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

Można ustawić obraz tła w serii ujęć do obrazka, który zawiera kolor chciałbyś