-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageViewController.py
169 lines (137 loc) · 5.5 KB
/
imageViewController.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
"""
note: Storyboard 未定義
"""
import ctypes
from pyrubicon.objc.api import ObjCClass
from pyrubicon.objc.api import objc_method, objc_property
from pyrubicon.objc.runtime import send_super
from pyrubicon.objc.types import CGRect
from rbedge.enumerations import (
UIViewAutoresizing,
UIViewContentMode,
)
from rbedge.pythonProcessUtils import dataWithContentsOfURL
from rbedge.functions import NSStringFromClass
from rbedge import pdbr
from pyLocalizedString import localizedString
UIViewController = ObjCClass('UIViewController')
NSLayoutConstraint = ObjCClass('NSLayoutConstraint')
UIImageView = ObjCClass('UIImageView')
UIImage = ObjCClass('UIImage')
NSURL = ObjCClass('NSURL')
UIToolTipInteraction = ObjCClass('UIToolTipInteraction')
UIColor = ObjCClass('UIColor')
class ImageViewController(UIViewController):
imageView: UIImage = objc_property()
@objc_method
def dealloc(self):
# xxx: 呼ばない-> `send_super(__class__, self, 'dealloc')`
print(f'\t - {NSStringFromClass(__class__)}: dealloc')
#pass
@objc_method
def loadView(self):
send_super(__class__, self, 'loadView')
#print(f'\t{NSStringFromClass(__class__)}: loadView')
# MARK: - View Life Cycle
@objc_method
def viewDidLoad(self):
send_super(__class__, self, 'viewDidLoad')
self.navigationItem.title = localizedString('ImageViewTitle') if (
title := self.navigationItem.title) is None else title
self.view.backgroundColor = UIColor.systemBackgroundColor()
imageView = UIImageView.alloc().init()
# --- Layout
self.view.addSubview_(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = False
#areaLayoutGuide = self.view.safeAreaLayoutGuide
areaLayoutGuide = self.view
NSLayoutConstraint.activateConstraints_([
imageView.centerXAnchor.constraintEqualToAnchor_(
areaLayoutGuide.centerXAnchor),
imageView.centerYAnchor.constraintEqualToAnchor_(
areaLayoutGuide.centerYAnchor),
imageView.widthAnchor.constraintEqualToAnchor_multiplier_(
areaLayoutGuide.widthAnchor, 1.0),
imageView.heightAnchor.constraintEqualToAnchor_multiplier_(
areaLayoutGuide.heightAnchor, 1.0),
])
self.imageView = imageView
self.configureImageView()
@objc_method
def viewWillAppear_(self, animated: bool):
send_super(__class__,
self,
'viewWillAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print(f'\t{NSStringFromClass(__class__)}: viewWillAppear_')
@objc_method
def viewDidAppear_(self, animated: bool):
send_super(__class__,
self,
'viewDidAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print(f'\t{NSStringFromClass(__class__)}: viewDidAppear_')
@objc_method
def viewWillDisappear_(self, animated: bool):
send_super(__class__,
self,
'viewWillDisappear:',
animated,
argtypes=[
ctypes.c_bool,
])
# print(f'\t{NSStringFromClass(__class__)}: viewWillDisappear_')
@objc_method
def viewDidDisappear_(self, animated: bool):
send_super(__class__,
self,
'viewDidDisappear:',
animated,
argtypes=[
ctypes.c_bool,
])
print(f'\t{NSStringFromClass(__class__)}: viewDidDisappear_')
@objc_method
def didReceiveMemoryWarning(self):
send_super(__class__, self, 'didReceiveMemoryWarning')
print(f'{__class__}: didReceiveMemoryWarning')
# MARK: - Configuration
@objc_method
def configureImageView(self):
# The root view of the view controller is set in Interface Builder and is an UIImageView.
# ビュー コントローラーのルート ビューは Interface Builder で設定され、UIImageView です。
# todo: 上記コメントと実装方法が違う。`self.imageView` は`self.view` で`addSubview_` してる。
if (imageView := self.imageView).isKindOfClass_(UIImageView):
# xxx: `lambda` の使い方が悪い
flowers_str = lambda index: f'./UIKitCatalogCreatingAndCustomizingViewsAndControls/UIKitCatalog/Assets.xcassets/Flowers_{index}.imageset/Flowers_{index}.png'
# Fetch the images (each image is of the format Flowers_number).
self.imageView.animationImages = [
UIImage.alloc().initWithData_scale_(
dataWithContentsOfURL(flowers_str(i)), 1) for i in range(1, 3)
]
# We want the image to be scaled to the correct aspect ratio within imageView's bounds.
self.imageView.contentMode = UIViewContentMode.scaleAspectFit
self.imageView.animationDuration = 5
self.imageView.startAnimating()
self.imageView.isAccessibilityElement = True
self.imageView.accessibilityLabel = localizedString('Animated')
if True: # wip: `available(iOS 15, *)`
interaction = UIToolTipInteraction.alloc().initWithDefaultToolTip_(
localizedString('ImageToolTipTitle'))
self.imageView.addInteraction_(interaction)
if __name__ == '__main__':
from rbedge.app import App
from rbedge.enumerations import UIModalPresentationStyle
main_vc = ImageViewController.new()
_title = NSStringFromClass(ImageViewController)
main_vc.navigationItem.title = _title
#presentation_style = UIModalPresentationStyle.fullScreen
presentation_style = UIModalPresentationStyle.pageSheet
app = App(main_vc, presentation_style)
app.present()