我想要做的就是为所有状态的按钮添加背景颜色。但是我希望在tvOS
故事板中使用系统按钮时保持“免费”获得的自动焦点阴影。到目前为止,我还没有找到允许这种情况的组合。
或者,我也有兴趣在按钮聚焦时以编程方式添加阴影的方法,但是没有子按钮(我还没有尝试过),我不知道怎么做
答案 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;
}
答案 1 :(得分:7)
对简单的颜色更改不满意,所以我创建了一个自定义按钮子类,看起来更像是系统按钮带来的默认动画 -
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)
}
}
没有什么太复杂,但完成工作
答案 2 :(得分:6)
覆盖didUpdateFocusInContext
方法并检查下一个焦点视图是否为按钮,如果是,则自定义其UI,并将其设置回原始状态检查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
}
}
答案 3 :(得分:3)
终极解决方案,灵感来自SomaMan。只需将所有自定义按钮子类化,即可获得:
包括:点击动画并释放并拖走。
...
caster.addr = m;
BOOST_ASSERT((caster.addr_uint64 & boost::uint64_t(3)) == 0);
max_count = boost::uint32_t(caster.addr_uint64 >> 32);
**initial_count = boost::uint32_t(caster.addr_uint64);**
initial_count = initial_count/4;
//Make sure top two bits are zero
BOOST_ASSERT((max_count & boost::uint32_t(0xC0000000)) == 0);
//Set quasi-top bit
max_count |= boost::uint32_t(0x40000000);
...
答案 4 :(得分:2)
我找到了更好的东西:
-(void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
[coordinator addCoordinatedAnimations:^{
if (self.focused) {
self.backgroundColor = [UIColor whiteColor];
}
else {
self.backgroundColor = [UIColor clearColor];
}
} completion:nil];
}
答案 5 :(得分:1)
Swift 4 / tvOS11及更高版本:
在“界面生成器”按钮属性中将ButtonType设置为“普通”。
将此私人扩展名添加到您的班级:
private extension UIImage {
static func imageWithColor(color: UIColor, size: CGSize) -> UIImage? {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
然后在已连接的IBOutlet中为按钮的聚焦状态设置背景图像:
@IBOutlet weak var myButton: UIButton! {
didSet {
let backgroundImageSelected = UIImage.imageWithColor(color: .red, size: myButton.bounds.size)
myButton.setBackgroundImage(backgroundImageSelected, for: .focused)
}
}
答案 6 :(得分:0)
您可以使用UIButton
方法setBackgroundImage(image: UIImage?, forState state: UIControlState)
并传递平面颜色和状态.Normal
的图片。
可以使用UIColor
和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
}
答案 7 :(得分:0)
您可以将故事板中的背景图像设置为包含您想要的颜色的图像