2015-09-22 13 views
9

Tất cả những gì tôi muốn làm là thêm màu nền vào nút cho tất cả các tiểu bang. Nhưng tôi muốn duy trì bóng tiêu điểm tự động mà bạn nhận được "miễn phí" khi sử dụng nút hệ thống trong bảng phân cảnh tvOS. Cho đến nay tôi đã không thể tìm thấy một sự kết hợp cho phép điều này.Làm cách nào để tạo nút có màu nền cho tvOS trong khi vẫn hiển thị tiêu điểm?

Ngoài ra, tôi cũng sẽ quan tâm đến cách thêm bóng khi lập trình khi nút được tập trung, nhưng ngắn phân lớp nút (mà tôi chưa thử), tôi không biết cách thực hiện hoặc.

+0

Lý do điều này đặc biệt khó chịu là nếu bạn thêm vào bóng tối theo chương trình, bạn sẽ mất tất cả các nửa mát chuyển động bounciness. – livingtech

+0

Một kỹ sư táo (trên diễn đàn dev) cho biết bạn có thể tạo lại chuyển động bằng cách sử dụng API 'UIMotionEffect' mới: https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UIMotionEffect_class/index. html Tôi không chắc chắn như thế nào, nhưng tôi có thể chơi với nó. – livingtech

Trả lời

6

Override didUpdateFocusInContext phương pháp và kiểm tra xem tiếp theo quan điểm trọng tâm là nút, nếu có thì tùy chỉnh giao diện của nó, và đặt nó trở lại orignal kiểm tra trạng thái context.previousFocusedView là nút đó, một cái gì đó như dưới đây

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

Vâng, đây là những gì tôi đã làm hôm qua. Tôi vẫn muốn biết nếu câu hỏi đầu tiên của tôi là có thể. – livingtech

+2

Điều này không hoạt động. Bạn vẫn không thể ghi đè lên màu nền trắng được thêm vào nút lấy nét. –

+0

Câu hỏi Orignal là làm thế nào để thay đổi màu nền của nút, không phải màu tiêu điểm, và câu trả lời là giải pháp cho rằng –

8

Bạn có thể thêm một cái bóng cho nút tùy chỉnh của bạn như thế này:

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

tôi đã tìm thấy một cái gì đó tốt hơn:

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

tự đề cập đến điều gì? đây có phải là phương thức phân lớp không? –

6

Chẳng phải hài lòng với một sự thay đổi màu sắc đơn giản, vì vậy tôi làm một nút tùy chỉnh lớp con để trông giống như các hình ảnh động mặc định bạn sẽ có được với các nút hệ thống -

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

Không có gì quá phức tạp, nhưng được hoàn thành công việc

0

Bạn có thể sử dụng phương pháp UIButtonsetBackgroundImage(image: UIImage?, forState state: UIControlState) và chuyển qua một hình ảnh có màu phẳng và trạng thái .Normal.

Hình ảnh này có thể dễ dàng được tạo ra từ một programatically và kích thước UIColor của 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

giải pháp cuối cùng với cảm hứng từ SomaMan. Chỉ cần phân lớp tất cả các nút tùy chỉnh của bạn và bạn Get này:

enter image description here

bao gồm: trên hình ảnh động vòi và thả và kéo đi.

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

Bạn có thể thiết lập các hình nền trong kịch bản cho một hình ảnh có chứa các màu sắc mà bạn muốn

Các vấn đề liên quan