CHITouchySuperButton
是一个可以直接使用的 UIButton 子类,它允许你仅用几行代码就能创建和自定义一个高度交互的按钮。 这个类兼容 UIView 的可动画属性:transform
,alpha
,center
,frame
,bounds
和 backgroundColor
。 这个类被广泛用于所有 SongShift 应用中,使其感觉更具互动性。 此软件包仅与 iOS 应用兼容,并以 iOS 10+ 为目标。
背景颜色 | 形变 |
---|---|
![]() |
![]() |
要安装 CHITouchySuperButton
,你必须手动添加 源文件,或使用 Swift Package Manager。 要使用 Swift Package Manager 安装,请按照 Apple 官方文档 中的说明进行操作。
CHITouchySuperButton
的用法非常简单。 首先导入模块
import CHITouchySuperButton
然后,像创建标准 UIButton
一样创建 CHITouchySuperButton
的实例。 这可以通过界面构建器或以编程方式完成
let button = CHITouchySuperButton(frame: .zero)
// add a target for .touchUpInside just like a normal UIButton
button.addTarget(self, action: #selector(buttonPressed(_:)), for: .touchUpInside)
button.backgroundColor = UIColor.systemBlue
button.layer.masksToBounds = true
button.layer.cornerRadius = 13
button.setTitle("Touchy Super Button", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
view.addConstraints([
button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30),
button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
button.heightAnchor.constraint(equalToConstant: 65)
])
现在,要告诉 CHITouchySuperButton
你希望它在按下和释放时如何显示,你需要实现两个 TouchBlock
// tell CHITouchySuperButton what to do when pressed down
button.buttonPressed = { button in
// modify transform, alpha, center, frame, bounds, or backgroundColor
button?.transform = CGAffineTransform(scaleX: 0.92, y: 0.92)
}
// tell CHITouchySuperButton what to do when released
button.buttonReleased = { button in
// do NOT put your actions here, just your animations. use addTarget to add an action.
button?.transform = .identity
}
就是这样! 上面的代码将产生位于本自述文件顶部的“形变”效果。
默认情况下,touchy 按钮在释放时会产生触觉反馈。 要禁用此功能,只需将你的 CHITouchySuperButton
上的 hapticFeedbackEnabled
设置为 false 即可。
根据 MIT 许可证 开源。
MIT License
Copyright (c) 2018-2019 SongShift, LLC.
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.