2015-09-22 6 views
9

मैं बस इतना करना चाहता हूं कि सभी राज्यों के लिए एक बटन में पृष्ठभूमि रंग जोड़ें। लेकिन मैं tvOS स्टोरीबोर्ड में सिस्टम बटन का उपयोग करते समय स्वचालित फोकस छाया को बनाए रखना चाहता हूं जिसे आपको "मुफ्त में" मिलता है। अब तक मैं एक संयोजन खोजने में सक्षम नहीं हूं जो इसे अनुमति देता है।टीवीओएस के लिए पृष्ठभूमि रंग के साथ एक बटन कैसे बना सकता हूं जबकि अभी भी फोकस दिखा रहा है?

वैकल्पिक रूप से, बटन पर ध्यान केंद्रित करते समय प्रोग्रामेटिक रूप से छाया जोड़ने के तरीके में दिलचस्पी होगी, लेकिन बटन को उपclassing (जो मैंने अभी तक कोशिश नहीं की है) से कम है, मुझे नहीं पता कि यह कैसे करना है या तो।

+0

कारण यह विशेष रूप से निराशाजनक है कि यदि आप छाया प्रोग्राम के रूप में जोड़ने के लिए, आप सभी शांत आधा खोना है गति उछाल। – livingtech

+0

एक सेब इंजीनियर (देव मंचों पर) ने कहा कि आप नए 'UIMotionEffect' API का उपयोग करके गति को पुन: पेश कर सकते हैं: https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UIMotionEffect_class/index। एचटीएमएल मुझे यकीन नहीं है कि कैसे, लेकिन मैं इसके साथ खेल सकता हूं। – livingtech

उत्तर

6

अवहेलना didUpdateFocusInContext विधि और देखें कि क्या अगले फोकस दृश्य बटन है, अगर हाँ तो इसके यूआई अनुकूलित, और इसे वापस सेट करने के लिए orignal करने के लिए राज्य की जांच context.previousFocusedView कि बटन था, नीचे

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

हां, यही वह है जो मैंने कल किया था। मैं अभी भी जानना चाहूंगा कि मेरा पहला सवाल संभव है या नहीं। – livingtech

+2

यह काम नहीं करता है। आप अभी भी सफेद पृष्ठभूमि रंग को ओवरराइड नहीं कर सकते हैं जो फोकस बटन में जोड़ा जाता है। –

+0

ओरिगनल प्रश्न यह था कि बटन के पृष्ठभूमि रंग को कैसे बदलें, फोकस रंग नहीं, और उत्तर –

8

आप की तरह कुछ इस तरह अपने कस्टम बटन के लिए एक छाया जोड़ सकते हैं:

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

मैंने पाया बेहतर कुछ:

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

स्वयं का क्या जिक्र है? क्या यह सबक्लास विधि है? –

6

एक साधारण रंग बदलने के साथ खुश नहीं था, इसलिए मैं एक कस्टम बटन उपवर्ग डिफ़ॉल्ट एनीमेशन आप सिस्टम बटन के साथ मिल की तरह अधिक देखने के लिए बनाया -

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

कुछ नहीं भी जटिल, लेकिन काम पूरा हो गया

0

आप UIButton विधि setBackgroundImage(image: UIImage?, forState state: UIControlState) का उपयोग कर सकते हैं और एक फ्लैट रंग और राज्य .Normal। SomaMan से प्रेरणा के साथ

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

अंतिम समाधान:

इस छवि को आसानी से एक UIColor और एक 1x1 के आकार से प्रोग्राम के बनाया जा सकता है। बस अपने सभी कस्टम बटन उपवर्ग और आप इस प्राप्त करें:

enter image description here

में शामिल हैं: नल एनीमेशन पर और जारी करने और दूर खींचें।

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

आप एक छवि है कि रंग शामिल करने के लिए स्टोरीबोर्ड में पृष्ठभूमि छवि आप चाहते हैं निर्धारित कर सकते हैं

संबंधित मुद्दे

 संबंधित मुद्दे