琴颈
使用CoreGraphics为iOS、tvOS和macOS提供的可定制的吉他琴颈视图。
演示
Mac
iOS
Apple TV
Storyboard
需求
- Swift 4.0+
- iOS 8.0+
- tvOS 9.0+
- macOS 10.9+
安装
use_frameworks!
pod 'Fretboard'
使用
通过代码或从故事板中创建一个 FretboardView
,它是 UIView
的子类。
音符选择
指板使用 MusicTheory
库来渲染音阶、和弦或音符。
您需要在 FretboardView
实例的 fretboard
属性上选择/取消选择音符、和弦或音阶。
let chord = Chord(type: ChordType(third: .major), key: Key(type: .a))
fretboardView?.fretboard.select(chord: chord)
let scale = Scale(type: .major, key: Key(type: .e, accidental: .flat))
fretboardView?.fretboard.select(scale: scale)
let note = Pitch(key: Key(type: .a), octave: 2)
fretboardView?.fretboard.select(note: note)
fretboardView?.fretboard.unselect(note: note)
fretboardView?.fretboard.unselectAll()
如果您只想渲染弦上最低音的音符,可以将 isChordModeOn
属性设置为 true。
我建议使用 3、4 或 5 个品位来实现这一功能。
调音
指板具有一个功能齐全的调音属性,这是一个在指板上的 FretboardTuning
协议,可以让你定义弦和它们在指板上的代表音符。
它包含一些预制调音,如 GuitarTuning
、BassTuning
和 UkeleleTuning
枚举。
您还可以使用具有自定义字符串计数量的 CustomTuning
结构定义自定义调音。
let tuning = CustomTuning(
strings: [
Pitch(key: Key(type: .g), octave: 2),
Pitch(key: Key(type: .d), octave: 2),
Pitch(key: Key(type: .a), octave: 1),
Pitch(key: Key(type: .e), octave: 1)
],
description: "My Custom Tuning")
fretboardView?.fretboard.tuning = tuning
方向
您可以使用 direction
属性在指板的类型 FretboardDirection
上渲染水平或垂直的指板。
freboardView?.fretboard.direction = .horizontal
freboardView?.fretboard.direction = .vertical
品格
您可以使用品格上的startIndex
和count
属性来渲染任意范围的品格。
startIndex
是起始品格,0代表空弦,默认值为0。
count
是品格数量,默认值为5。
定制
您可以更改品格和弦的线条宽度和颜色。
您可以通过代码或剧本更改品格编号、弦名和音符的颜色。
在按下音符时渲染音符名称是可选的。
渲染品格编号和弦名也是可选的。
请参阅FretboardView
的属性。
品格滚动视图
在您的iOS/tvOS/macOS应用程序中,您还可以使用一个滚动视图,在其中可以滚动您的品格。它有一个可以自定义您的品格的FretboardView
实例。
@IBOutlet weak var scrollView: FretboardScrollView?
scrollView?.fretboardView.fretStartIndex = appState.fretStartIndex
scrollView?.fretboardView.fretCount = appState.fretCount
scrollView?.fretboardView.fretboard.tuning = appState.tuning.tuning
FretBud
该库用于我的iOS/tvOS应用程序FretBud,欢迎查看!