-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsliderViewController.py
231 lines (192 loc) · 9.56 KB
/
sliderViewController.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from enum import Enum
from pyrubicon.objc.api import ObjCClass, ObjCInstance
from pyrubicon.objc.api import objc_method
from pyrubicon.objc.runtime import send_super, objc_id, SEL
from pyrubicon.objc.types import NSInteger
from rbedge.enumerations import (
UIControlEvents,
UIControlState,
UIUserInterfaceIdiom,
UIImageSymbolScale,
UIImageSymbolWeight,
UIBehavioralStyle,
)
from rbedge.pythonProcessUtils import (
mainScreen_scale,
dataWithContentsOfURL,
)
from rbedge import pdbr
from caseElement import CaseElement
from pyLocalizedString import localizedString
from baseTableViewController import BaseTableViewController
from storyboard.sliderViewController import prototypes
UIImage = ObjCClass('UIImage')
UIImageSymbolConfiguration = ObjCClass('UIImageSymbolConfiguration')
UIColor = ObjCClass('UIColor')
# Cell identifier for each slider table view cell.
# スライダー テーブル ビューの各セルのセル識別子。
class SliderKind(Enum):
sliderDefault = 'sliderDefault'
sliderTinted = 'sliderTinted'
sliderCustom = 'sliderCustom'
sliderMaxMinImage = 'sliderMaxMinImage'
class SliderViewController(BaseTableViewController):
@objc_method
def initWithStyle_(self, style: NSInteger) -> ObjCInstance:
send_super(__class__,
self,
'initWithStyle:',
style,
restype=objc_id,
argtypes=[
NSInteger,
])
self.setupPrototypes_(prototypes)
return self
# MARK: - View Life Cycle
@objc_method
def viewDidLoad(self):
send_super(__class__, self, 'viewDidLoad') # xxx: 不要?
self.navigationItem.title = localizedString('SlidersTitle') if (
title := self.navigationItem.title) is None else title
self.testCells_extend([
CaseElement(localizedString('DefaultTitle'),
SliderKind.sliderDefault.value,
self.configureDefaultSlider_),
])
# todo: `@available(iOS 15.0, *)`
self.testCells_extend([
CaseElement(localizedString('CustomTitle'),
SliderKind.sliderCustom.value, self.configureCustomSlider_),
CaseElement(localizedString('MinMaxImagesTitle'),
SliderKind.sliderMaxMinImage.value,
self.configureMinMaxImageSlider_),
CaseElement(localizedString('TintedTitle'),
SliderKind.sliderTinted.value, self.configureTintedSlider_),
])
# MARK: - Configuration
@objc_method
def configureDefaultSlider_(self, slider):
slider.minimumValue = 0
slider.maximumValue = 100
slider.value = 42
slider.isContinuous = True
slider.addTarget_action_forControlEvents_(self,
SEL('sliderValueDidChange:'),
UIControlEvents.valueChanged)
# todo: `@available(iOS 15.0, *)`
@objc_method
def configureTintedSlider_(self, slider):
'''
To keep the look the same betwen iOS and macOS:
For minimumTrackTintColor, maximumTrackTintColor to work in Mac Catalyst, use UIBehavioralStyle as ".pad",
Available in macOS 12 or later (Mac Catalyst 15.0 or later).
Use this for controls that need to look the same between iOS and macOS.
'''
'''
iOS と macOS で見た目を同じにするには:
minimumTrackTintColor、maximumTrackTintColor を Mac Catalyst で機能させるには、UIBehavioralStyle を「.pad」として使用します。
macOS 12以降(Mac Catalyst 15.0以降)で利用可能です。
iOS と macOS の間で同じように見える必要があるコントロールにこれを使用します。
'''
if slider.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.mac:
slider.preferredBehavioralStyle = UIBehavioralStyle.pad
slider.minimumTrackTintColor = UIColor.systemBlueColor()
slider.maximumTrackTintColor = UIColor.systemPurpleColor()
slider.addTarget_action_forControlEvents_(self,
SEL('sliderValueDidChange:'),
UIControlEvents.valueChanged)
# todo: `@available(iOS 15.0, *)`
@objc_method
def configureCustomSlider_(self, slider):
'''
To keep the look the same betwen iOS and macOS:
For setMinimumTrackImage, setMaximumTrackImage, setThumbImage to work in Mac Catalyst, use UIBehavioralStyle as ".pad",
Available in macOS 12 or later (Mac Catalyst 15.0 or later).
Use this for controls that need to look the same between iOS and macOS.
'''
'''
iOS と macOS で見た目を同じにするには:
setMinimumTrackImage、setMinimumTrackImage、setThumbImage を Mac Catalyst で動作させるには、UIBehavioralStyle を「.pad」として使用します。
macOS 12以降(Mac Catalyst 15.0以降)で利用可能です。
iOS と macOS の間で同じように見える必要があるコントロールにこれを使用します。
'''
if slider.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.mac:
slider.preferredBehavioralStyle = UIBehavioralStyle.pad
scale = int(mainScreen_scale)
leftTrack_str = f'./UIKitCatalogCreatingAndCustomizingViewsAndControls/UIKitCatalog/Assets.xcassets/slider_blue_track.imageset/slider_blue_track_{scale}x.png'
rightTrack_str = f'./UIKitCatalogCreatingAndCustomizingViewsAndControls/UIKitCatalog/Assets.xcassets/slider_green_track.imageset/slider_green_track_{scale}x.png'
leftTrackImage = UIImage.alloc().initWithData_scale_(
dataWithContentsOfURL(leftTrack_str), scale)
rightTrackImage = UIImage.alloc().initWithData_scale_(
dataWithContentsOfURL(rightTrack_str), scale)
slider.setMinimumTrackImage_forState_(leftTrackImage,
UIControlState.normal)
slider.setMaximumTrackImage_forState_(rightTrackImage,
UIControlState.normal)
# Set the sliding thumb image (normal and highlighted).
# スライドサム画像(通常およびハイライト)を設定します。
# For fun, choose a different image symbol configuraton for the thumb's image between macOS and iOS.
# お楽しみとして、macOS と iOS の間でサムの画像に異なる画像シンボル構成を選択してください。
thumbImageConfig: 'UIImage.SymbolConfiguration'
if slider.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.mac:
thumbImageConfig = UIImageSymbolConfiguration.configurationWithScale_(
UIImageSymbolScale.large)
else:
thumbImageConfig = UIImageSymbolConfiguration.configurationWithPointSize_weight_scale_(
30.0, UIImageSymbolWeight.heavy, UIImageSymbolScale.large)
thumbImage = UIImage.systemImageNamed_withConfiguration_(
'circle.fill', thumbImageConfig)
thumbImageHighlighted = UIImage.systemImageNamed_withConfiguration_(
'circle', thumbImageConfig)
slider.setThumbImage_forState_(thumbImage, UIControlState.normal)
slider.setThumbImage_forState_(thumbImageHighlighted,
UIControlState.highlighted)
slider.minimumValue = 0
slider.maximumValue = 100
slider.isContinuous = False
slider.value = 84
slider.addTarget_action_forControlEvents_(self,
SEL('sliderValueDidChange:'),
UIControlEvents.valueChanged)
@objc_method
def configureMinMaxImageSlider_(self, slider):
'''
To keep the look the same betwen iOS and macOS:
For setMinimumValueImage, setMaximumValueImage to work in Mac Catalyst, use UIBehavioralStyle as ".pad",
Available in macOS 12 or later (Mac Catalyst 15.0 or later).
Use this for controls that need to look the same between iOS and macOS.
'''
'''
iOSとmacOSで見た目を同じにするためです:
Mac CatalystでsetMinimumValueImage、setMaximumValueImageを動作させるには、UIBehavioralStyleを".pad "として使用してください、
macOS 12以降(Mac Catalyst 15.0以降)で使用できます。
iOSとmacOSで同じ外観にする必要があるコントロールに使用します。
'''
# xxx: あとで調整
if True: # xxx: `#available(iOS 15, *)`
if slider.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.mac:
slider.preferredBehavioralStyle = UIBehavioralStyle.pad
slider.minimumValueImage = UIImage.systemImageNamed_('tortoise')
slider.maximumValueImage = UIImage.systemImageNamed_('hare')
slider.addTarget_action_forControlEvents_(self,
SEL('sliderValueDidChange:'),
UIControlEvents.valueChanged)
# MARK: - Actions
@objc_method
def sliderValueDidChange_(self, slider):
formattedValue = f'{slider.value:.2f}'
print(f'Slider changed its value: {formattedValue}')
if __name__ == '__main__':
from rbedge.functions import NSStringFromClass
from rbedge.enumerations import (
UITableViewStyle,
UIModalPresentationStyle,
)
from rbedge import present_viewController
table_style = UITableViewStyle.grouped
main_vc = SliderViewController.alloc().initWithStyle_(table_style)
_title = NSStringFromClass(SliderViewController)
main_vc.navigationItem.title = _title
presentation_style = UIModalPresentationStyle.fullScreen
present_viewController(main_vc, presentation_style)