AYVibrantButton 是一个具有 iOS 8 活力效果的时尚按钮。它是 UIButton
的子类,外观简单而优雅,并内置了对 iOS 8 中引入的 UIVisualEffectView
和 UIVibrancyEffect
类的支持。然而,它可以在不带活力效果的情况下用于 iOS 7。
可以用支持的三个按钮样式之一配置活力按钮,即 invert、translucent 和 fill(以下有示例)。
可以通过轻松更改一些基本属性,如 图标、文本、字体、alpha、圆角半径、边框宽度 和 背景颜色(对于无活力效果)。
默认的活力效果是模糊效果 UIBlurEffectStyleLight
。它可以设置为任何 UIVibrancyEffect
实例。对于今天的扩展,应设置为 [UIVibrancyEffect notificationCenterVibrancyEffect]
。
尽管活力按钮可以放置在任何地方,但建议将具有活力效果的活力按钮放置在 UIVisualEffectView
的 contentView
中(除 Today 视图外)。
UIVisualEffectView
可以按如下方式创建。
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
effectView.frame = self.view.bounds;
[self.view addSubview:effectView];
只需将 AYVibrantButton.h
和 AYVibrantButton.m
添加到您的项目中。
以下图像显示了正常和突出显示(按中)按钮的外观。
AYVibrantButton *invertButton = [[AYVibrantButton alloc] initWithFrame:CGRectZero style:AYVibrantButtonStyleInvert];
invertButton.vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
invertButton.text = @"Invert";
invertButton.font = [UIFont systemFontOfSize:18.0];
[effectView.contentView addSubview:invertButton];
AYVibrantButton *translucentButton = [[AYVibrantButton alloc] initWithFrame:CGRectZero style:AYVibrantButtonStyleTranslucent];
translucentButton.vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
translucentButton.text = @"Translucent";
translucentButton.font = [UIFont systemFontOfSize:18.0];
[effectView.contentView addSubview:translucentButton];
AYVibrantButton *button = [[AYVibrantButton alloc] initWithFrame:CGRectZero style:AYVibrantButtonStyleTranslucent];
button.vibrancyEffect = nil;
button.text = @"Any Color";
button.font = [UIFont systemFontOfSize:18.0];
button.backgroundColor = [UIColor blackColor];
[effectView.contentView addSubview:button];
invertAlphaHighlighted
属性UIButton
改为 UIControl
backgroundColor
改为 tintColor
AYVibrantButtonStyleFill
Alan Yip
The MIT License (MIT)
Copyright (c) 2014 Alan Yip
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.