UISwitch 子类,当开关打开时,会将其所选颜色“传染”给父视图。灵感来源于这个 Dribble,由Ramotion团队制作。
AMViralSwitch
是UISwitch
的直接替代品
AMViralSwitch
替代UISwitch
onTintColor
属性开关将自动将所选颜色传染给其父视图。
使用animationDuration
属性来控制动画速度
self.toggle.animationDuration = 1.0
可以为on
和off
动画设置完成块
self.toggle.completionOn = {
print("Enabled")
}
self.toggle.completionOff = {
print("Disabled")
}
您可以与开关同时动画化其他视图传染。通常,您会想改变同一父视图中视图或标签的颜色。您可以这样做来动画化CoreAnimation属性
toggle.animationElementsOn = [
[
AMElementView: self.greenView.layer,
AMElementKeyPath: "backgroundColor",
AMElementFromValue: UIColor.blackColor().CGColor,
AMElementToValue: UIColor.whiteColor().CGColor
]
]
self.toggle.animationElementsOn = @[
@[ AMElementView: self.greenView.layer,
AMElementKeyPath: @"backgroundColor",
AMElementFromValue: (id)[UIColor clearColor].CGColor,
AMElementToValue: (id)[UIColor whiteColor].CGColor }
];
要动画化UILabel
的textColor
属性,语法稍有不同
toggle.animationElementsOn = [
[
AMElementView: label,
AMElementKeyPath: "textColor",
AMElementFromValue: UIColor.blackColor(),
AMElementToValue: UIColor.whiteColor()
]
]
self.blueSwitch.animationElementsOn = @[
@{ AMElementView: self.blueLabel,
AMElementKeyPath: @"textColor",
AMElementToValue: [UIColor whiteColor] }
];
按照相同的原理来动画化您UIButton
的tintColor
toggle.animationElementsOn = [
[
AMElementView: self.infoButton,
AMElementKeyPath: "tintColor",
AMElementToValue: UIColor.whiteColor()
]
]
self.blueSwitch.animationElementsOn = @[
@{ AMElementView: self.infoButton,
AMElementKeyPath: @"tintColor",
AMElementToValue: [UIColor whiteColor] }
];
Andrea Mazzini。我提供自由职业工作,欢迎联系我。
想要支持这些免费库的开发?请给我买杯咖啡
感谢各位慷慨提交 pull request 的贡献者。
Copyright (c) 2015 Andrea Mazzini. All rights reserved.
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.