cha_work 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e1969ed96ff2fd51dd034be2c71b2692140526c9
4
+ data.tar.gz: 4f16cadc51ebc23f881ffbc16e989ba86d92cd96
5
+ SHA512:
6
+ metadata.gz: 8054d0394c31ab8d0a14c5602a8662f87276cd2acb334b28d36d1cfc3ca9eaf1ecb7f835b7c764fc11aededb548c3defe7b5c90abc82c47394baa69797966a1a
7
+ data.tar.gz: 7354b14ed80cd52f68e1c98a92670b54116446180b79cfbc1d832ca52df9940e4c38957e0e282a3a0724c765c3888146e1877fb47575a37af43fd7d7126907a6
@@ -0,0 +1,23 @@
1
+ # ChaWork
2
+
3
+
4
+ ## 安装说明
5
+
6
+ 1、安装gem
7
+ ```
8
+ gem install cha_work
9
+ ```
10
+ ## ToDo
11
+
12
+
13
+ ## 联系作者
14
+ email: sam@chamobile.com
15
+ wechat: smartweb
16
+ twitter: @samchueng
17
+
18
+ ## 参与
19
+ Fork and Push
20
+
21
+ ## License
22
+ MIT
23
+
@@ -0,0 +1,13 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The cha_work gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ Motion::Project::App.setup do |app|
6
+ core_lib = File.join(File.dirname(__FILE__), 'cha_work')
7
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
8
+
9
+ Dir.glob(File.join(core_lib, '**/*.rb')).reverse.each do |file|
10
+ app.files.insert(insert_point, file)
11
+ end
12
+
13
+ end
@@ -0,0 +1,93 @@
1
+ class ChaMenu < UIView
2
+ attr_accessor :delegate, :data, :selected_index, :scroll_view
3
+ attr_accessor :font_size, :normal_color, :selected_color, :highlight_color
4
+ attr_accessor :background_color
5
+ attr_accessor :selected_index
6
+
7
+ def setup
8
+ self.autoresizingMask = UIViewAutoresizingFlexibleWidth
9
+ @font_size ||= 16
10
+ @normal_color ||= UIColor.blackColor
11
+ @highlight_color||= UIColor.grayColor
12
+ @selected_color ||= UIColor.blueColor
13
+ @selected_index ||= 0
14
+ @scroll_view = UIScrollView.alloc.initWithFrame(self.bounds)
15
+ @scroll_view.backgroundColor = @background_color || UIColor.whiteColor
16
+ @scroll_view.autoresizingMask = UIViewAutoresizingFlexibleWidth
17
+ @scroll_view.showsVerticalScrollIndicator = false
18
+ @scroll_view.showsHorizontalScrollIndicator = false
19
+ @scroll_view.delegate = self
20
+
21
+ self.addSubview @scroll_view
22
+ reload_data
23
+ end
24
+
25
+ def set_select_index(index)
26
+ return if @selected_index == index
27
+ @selected_index = index
28
+ end
29
+
30
+ def reload_data
31
+ @scroll_view.subviews.each {|v| v.removeFromSuperview }
32
+
33
+ x = 0
34
+ padding = 10
35
+
36
+ selected_frame = [[0,0], [0,0]]
37
+ @data.each_with_index do |text, index|
38
+ x += padding
39
+ font = UIFont.systemFontOfSize(@font_size)
40
+ size = text.sizeWithFont(font, constrainedToSize:[10000, 300], lineBreakMode:NSLineBreakByCharWrapping)
41
+ frame = [[x, padding], [size.width, size.height]]
42
+ selected_frame = frame if index == @selected_index
43
+ @scroll_view.addSubview menu_button(text, index, frame)
44
+ x += size.width
45
+ end
46
+
47
+ content_width = x + padding
48
+ @scroll_view.setContentSize([content_width, @scroll_view.frame.size.height])
49
+ if @selected_index > 0
50
+ @scroll_view.scrollRectToVisibleCenteredOn(selected_frame, animated:false)
51
+ end
52
+ end
53
+
54
+ def menu_button(text, index, frame)
55
+ button = UIButton.alloc.initWithFrame(frame)
56
+ button.titleLabel.textAlignment = UITextAlignmentCenter
57
+ button.titleLabel.font = UIFont.systemFontOfSize(@font_size)
58
+ button.setTitle(text, forState:UIControlStateNormal)
59
+ if index == @selected_index
60
+ button.setTitleColor(@selected_color, forState:UIControlStateNormal)
61
+ else
62
+ button.setTitleColor(@normal_color, forState:UIControlStateNormal)
63
+ end
64
+ button.setTitleColor(@highlight_color, forState:UIControlStateHighlighted)
65
+ #button.setTitleColor(@selected_color, forState:UIControlStateSelected)
66
+ button.addTarget(self, action: 'menu_select:', forControlEvents:UIControlEventTouchUpInside)
67
+ button.tag = index
68
+ button
69
+ end
70
+
71
+ def menu_select(sender)
72
+ UIView.animateWithDuration(0.2, delay:0, options:UIViewAnimationOptionCurveLinear, animations: -> {
73
+ @scroll_view.scrollRectToVisibleCenteredOn(sender.frame, animated:false)
74
+ }, completion:nil)
75
+
76
+ @selected_index = sender.tag
77
+ reload_data
78
+ if @delegate.respond_to?(:selected_menu)
79
+ @delegate.selected_menu(@selected_index)
80
+ else
81
+ NSLog("Warning: Did not find selected_menu method.")
82
+ end
83
+ end
84
+
85
+ def delegate=(parent)
86
+ @delegate = WeakRef.new(parent)
87
+ end
88
+
89
+ def scrollViewDidScroll(scrollView)
90
+
91
+ end
92
+
93
+ end
@@ -0,0 +1,556 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module ChaWork
3
+ module Basic
4
+ def main
5
+ UIApplication.sharedApplication.delegate
6
+ end
7
+
8
+ def push(view, bool=true, hide=true)
9
+ view.hidesBottomBarWhenPushed = hide
10
+ self.navigationController.pushViewController(view, animated:bool)
11
+ end
12
+
13
+ def show(view, bool=true)
14
+ # NOTE: -[UIViewController presentModalViewController] is deperated!
15
+ self.presentViewController(view, animated:bool, completion: nil)
16
+ # self.presentModalViewController(view, animated:true)
17
+ end
18
+
19
+ def hide(bool=true)
20
+ self.dismissModalViewControllerAnimated(bool)
21
+ end
22
+
23
+ def pop(bool=true)
24
+ self.navigationController.popViewControllerAnimated(bool)
25
+ end
26
+
27
+ def pop_to_root(bool=true)
28
+ self.navigationController.popToRootViewControllerAnimated(bool)
29
+ end
30
+
31
+
32
+ # '0,0,0,0' = 'x,y,w,h', 若x为负数则表示向右对齐, 若为负数,则表示view.height - 数值
33
+
34
+ def get_frame(str)
35
+ if str.nil?
36
+ warn('frame为nil')
37
+ return [[0,0],[0,0]]
38
+ end
39
+
40
+ return str if str.class == Array
41
+
42
+ frame = str.split(',')
43
+
44
+ x = get_value(frame[0])
45
+ y = get_value(frame[1])
46
+ width = get_value(frame[2])
47
+ height= get_value(frame[3])
48
+ [[x, y], [width, height]]
49
+ end
50
+
51
+ # 格式,100/80, /号前是4寸屏值,/号后是3.5寸屏值
52
+ def get_value(val)
53
+ if val.nil?
54
+ NSLog("frame error, missing")
55
+ return 0
56
+ end
57
+ array = val.split("/")
58
+ # 若只有一个值,直接返回
59
+ return val.to_i if array.length == 1
60
+ return UIScreen.mainScreen.bounds.size.height > 480 ? array[0].to_i : array[1].to_i
61
+ end
62
+
63
+ alias _ get_frame
64
+
65
+ def full_view(color='#FFFFFF', opacity=1)
66
+ frame = [[0,0], [UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height]]
67
+ view = UIView.alloc.initWithFrame(frame)
68
+ view.backgroundColor = "#{color}".uicolor.colorWithAlphaComponent(opacity)
69
+ view
70
+ end
71
+
72
+ def create_view(frame, color='#FFFFFF', opacity=1, shadow=false)
73
+ frame = get_frame(frame)
74
+ view = UIView.alloc.initWithFrame(frame)
75
+ opacity = 1 if opacity.nil?
76
+ view.backgroundColor = "#{color}".uicolor.colorWithAlphaComponent(opacity)
77
+ if shadow
78
+ #view.layer.masksToBounds = NO
79
+ #view.layer.cornerRadius = 8
80
+ view.layer.shadowOffset = CGSizeMake(1, 2)
81
+ view.layer.shadowRadius = 1
82
+ view.layer.shadowOpacity = 0.3
83
+ end
84
+ view
85
+ end
86
+
87
+ def bottom_line(frame, color='#ECECEC')
88
+ frame = get_frame(frame)
89
+ new_frame = [[frame[0][0], frame[0][1] + frame[1][1] - 0.5], [frame[1][0], 0.5]]
90
+ bg = create_view(new_frame, color, 1)
91
+ bg.autoresizingMask = UIViewAutoresizingFlexibleTopMargin
92
+ bg
93
+ end
94
+
95
+ def footer(color='#FFFFFF')
96
+ view = UIView.alloc.init
97
+ view.backgroundColor = color.uicolor
98
+ view
99
+ end
100
+
101
+ def label(text, frame, size=16, bold=false, color='#000000', align=nil, background=nil, shadow=false, shadow_color='#CCCCCC')
102
+ frame = get_frame(frame)
103
+ lbl = UILabel.alloc.initWithFrame(frame)
104
+ lbl.text = text.to_s
105
+
106
+ if background.nil?
107
+ lbl.backgroundColor = UIColor.clearColor
108
+ else
109
+ lbl.backgroundColor = "#{background}".uicolor
110
+ end
111
+
112
+ lbl.highlightedTextColor = :white.uicolor
113
+
114
+ lbl.textColor = color.uicolor unless color.nil?
115
+ font_size = size.nil? ? 16 : size
116
+ lbl.font = bold ? UIFont.boldSystemFontOfSize(font_size) :
117
+ UIFont.systemFontOfSize(font_size)
118
+
119
+ case align
120
+ when nil, :left, 'left'
121
+ lbl.textAlignment = UITextAlignmentLeft
122
+ when :right, 'right'
123
+ lbl.textAlignment = UITextAlignmentRight
124
+ when :center, 'center'
125
+ lbl.textAlignment = UITextAlignmentCenter
126
+ end
127
+
128
+ if shadow
129
+ shadow_color = '#CCCCCC' if shadow_color.nil?
130
+ lbl.shadowColor = shadow_color.uicolor
131
+ lbl.shadowOffset = CGSizeMake(0, 1)
132
+ end
133
+ lbl
134
+ end
135
+
136
+ #自适应高度label
137
+ def context(text, frame, font_size = 16, color='#000000', absolute=false, align=nil, length=nil)
138
+ frame = get_frame(frame)
139
+ color ||= '#000000'
140
+ font_size ||= 16
141
+ lbl = UILabel.new
142
+ lbl.backgroundColor = UIColor.clearColor
143
+ lbl.textColor = color.uicolor
144
+
145
+ font = UIFont.systemFontOfSize(font_size)
146
+ width = frame[1][0]
147
+
148
+ if absolute
149
+ lbl.frame = frame
150
+ else
151
+ lbl.frame = CGRectMake(frame[0][0], frame[0][1], width, text.height(width: width, size: font_size))
152
+ end
153
+
154
+ case align
155
+ when nil, :left, 'left' then lbl.textAlignment = NSTextAlignmentLeft
156
+ when :right, 'right' then lbl.textAlignment = NSTextAlignmentRight
157
+ when :center, 'center' then lbl.textAlignment = NSTextAlignmentCenter
158
+ end
159
+
160
+ lbl.font = font
161
+ lbl.numberOfLines = 0
162
+ lbl.lineBreakMode = NSLineBreakByWordWrapping
163
+ lbl.text = text
164
+ lbl
165
+ end
166
+
167
+ def button(text, frame, size=16, color='#1D92B5', action=nil, param=nil, align=nil, bold=false)
168
+ size ||= 16
169
+ color ||= '#1D92B5'
170
+ frame = get_frame(frame)
171
+
172
+ button = UIButton.alloc.initWithFrame(frame)
173
+ button.titleLabel.font = bold ? UIFont.boldSystemFontOfSize(size) : UIFont.systemFontOfSize(size)
174
+ button.setTitle(text, forState:UIControlStateNormal)
175
+ button.setTitleColor(color.uicolor, forState:UIControlStateNormal)
176
+ # button.setTitleColor(:darkgray.uicolor, forState:UIControlStateHighlighted)
177
+
178
+ case align
179
+ when nil, :center, 'center'
180
+ button.setContentHorizontalAlignment(UIControlContentHorizontalAlignmentCenter)
181
+ when :right, 'right'
182
+ button.setContentHorizontalAlignment(UIControlContentHorizontalAlignmentRight)
183
+ when :left, 'left'
184
+ button.setContentHorizontalAlignment(UIControlContentHorizontalAlignmentLeft)
185
+ end
186
+
187
+
188
+ case action
189
+ when 'back' then button.on(:touch) { pop }
190
+ when 'hide' then button.on(:touch) { hide }
191
+ when 'user' then button.on(:touch) { push UserDetail.alloc.initWithId(param) } # TODO: Decouple UserDetail
192
+ else
193
+ unless action.nil?
194
+ if param.nil?
195
+ button.on(:touch) { send(action) }
196
+ else
197
+ button.on(:touch) { send(action, param) }
198
+ end
199
+ end
200
+ end
201
+
202
+ button
203
+ end
204
+
205
+
206
+ def bbutton(text, frame, type=:info, action=nil)
207
+ frame = get_frame(frame)
208
+ symbol = {info: BButtonTypeInfo,
209
+ success: BButtonTypeSuccess,
210
+ danger: BButtonTypeDanger,
211
+ default: BButtonTypeDefault,
212
+ primary: BButtonTypePrimary,
213
+ warning: BButtonTypeWarning}
214
+ type = symbol[type] || BButtonTypeInfo
215
+ btn = BButton.alloc.initWithFrame(frame, type:type, style:BButtonStyleBootstrapV3)
216
+ btn.setTitle(text, forState:UIControlStateNormal)
217
+ btn.on(:touch) { send(action) } if action
218
+ btn
219
+ end
220
+
221
+ def change_btn_image(btn, name)
222
+ btn.setImage(name.uiimage, forState:UIControlStateNormal)
223
+ end
224
+
225
+ def image_btn(name, frame, action=nil, param=nil)
226
+ frame = get_frame(frame)
227
+ button = UIButton.alloc.initWithFrame frame
228
+ if name.include? ','
229
+ val = name.split(",")
230
+ button.setImage(val[0].uiimage, forState:UIControlStateNormal)
231
+ button.setImage(val[1].uiimage, forState:UIControlStateHighlighted)
232
+ else
233
+ button.setImage(name.uiimage, forState:UIControlStateNormal)
234
+ end
235
+
236
+ case action
237
+ when 'back' then button.on(:touch) { pop }
238
+ when 'hide' then button.on(:touch) { hide }
239
+ else
240
+ unless action.nil?
241
+ if param.nil?
242
+ button.on(:touch) { send(action) }
243
+ else
244
+ button.on(:touch) { send(action, param, button) }
245
+ end
246
+ end
247
+ end
248
+
249
+ button
250
+ end
251
+
252
+ def image(url, frame, placeholder='photo', round=false, fit=false)
253
+ placeholder ||= 'photo'
254
+
255
+ frame = get_frame(frame)
256
+ image_view = UIImageView.alloc.initWithFrame frame
257
+
258
+ if url.nil? || url == ''
259
+ image_view.image = placeholder.uiimage
260
+ elsif url.class == UIImage
261
+ image_view.image = url
262
+ elsif url.downcase.include? 'http'
263
+ image_view.setImageWithURL(url.nsurl, placehoderImage:placeholder.uiimage)
264
+
265
+ if fit
266
+ image_view.contentMode = UIViewContentModeScaleAspectFill
267
+ image_view.clipsToBounds = true
268
+
269
+ # image_view.setImageWithURL(url.nsurl,
270
+ # success:-> image, cached do
271
+ # ratio = image.size.width / 320
272
+ # height= 200 #frame[1][1]
273
+ # rect = [[0, 0], [image.size.width, height * ratio]]
274
+ # imageRef = CGImageCreateWithImageInRect(image.CGImage, rect)
275
+ # img = UIImage.imageWithCGImage imageRef
276
+ # CGImageRelease(imageRef)
277
+ # image_view.image = img
278
+ # end, failure:nil)
279
+
280
+ end
281
+
282
+ # warn("1:#{url}")
283
+ elsif url.class == String
284
+
285
+ image_view.image = url.uiimage
286
+
287
+ else
288
+ image_view.image = placeholder.uiimage
289
+ end
290
+
291
+ if round
292
+ image_view.layer.cornerRadius = frame[1][1] / 2
293
+ image_view.layer.masksToBounds = true
294
+ end
295
+ image_view
296
+ end
297
+
298
+ def update_image(image_view, url, placeholder='photo')
299
+ return if url.nil? || url == ''
300
+
301
+ if url.class == UIImage
302
+ image_view.image = url
303
+ elsif url.downcase.include? 'http'
304
+ image_view.setImageWithURL(url.nsurl, placehoderImage:placeholder.uiimage)
305
+ elsif url.class == String
306
+ image_view.image = url.uiimage
307
+ end
308
+ image_view
309
+ end
310
+
311
+ def map(location ,frame, span=0.1, type='standard', show_me=false)
312
+ map_view = MKMapView.alloc.initWithFrame(frame)
313
+ map_view.mapType = (type == 'satellite') ? MKMapTypeSatellite : MKMapTypeStandard
314
+
315
+ # 显示用户当前的坐标
316
+ map_view.showsUserLocation=true if show_me
317
+ region = MKCoordinateRegion.new
318
+ region.center.latitude = location[0]
319
+ region.center.longitude = location[1]
320
+
321
+ span = 0.1 if span.nil?
322
+ region.span.latitudeDelta = span
323
+ region.span.longitudeDelta = span
324
+ map_view.region = region
325
+ map_view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
326
+
327
+ map_view
328
+ end
329
+
330
+ def custom_item(view)
331
+ UIBarButtonItem.alloc.initWithCustomView(view)
332
+ end
333
+
334
+ def bar_item(title, action_name)
335
+ item = UIBarButtonItem.alloc.initWithTitle(title,
336
+ style:UIBarButtonItemStyleBordered,
337
+ target:self,
338
+ action:action_name)
339
+ item
340
+ end
341
+
342
+ def image_item(image_name, action_name)
343
+ UIBarButtonItem.alloc.initWithImage(image_name.uiimage, style: UIBarButtonItemStyleBordered, target: self, action: action_name)
344
+ end
345
+
346
+ def action_item(action_name)
347
+ item = UIBarButtonItem.alloc.
348
+ initWithBarButtonSystemItem(UIBarButtonSystemItemAction,
349
+ target:self,
350
+ action:action_name)
351
+ item
352
+ end
353
+
354
+ def system_item(symbol, action_name)
355
+ name = {
356
+ done: UIBarButtonSystemItemDone,
357
+ cancel: UIBarButtonSystemItemCancel,
358
+ edit: UIBarButtonSystemItemEdit,
359
+ save: UIBarButtonSystemItemSave,
360
+ add: UIBarButtonSystemItemAdd,
361
+ flexible_space: UIBarButtonSystemItemFlexibleSpace,
362
+ fixed_space: UIBarButtonSystemItemFixedSpace,
363
+ compose: UIBarButtonSystemItemCompose,
364
+ reply: UIBarButtonSystemItemReply,
365
+ action: UIBarButtonSystemItemAction,
366
+ organize: UIBarButtonSystemItemOrganize,
367
+ bookmarks: UIBarButtonSystemItemBookmarks,
368
+ search: UIBarButtonSystemItemSearch,
369
+ refresh: UIBarButtonSystemItemRefresh,
370
+ stop: UIBarButtonSystemItemStop,
371
+ camera: UIBarButtonSystemItemCamera,
372
+ trash: UIBarButtonSystemItemTrash,
373
+ play: UIBarButtonSystemItemPlay,
374
+ pause: UIBarButtonSystemItemPause,
375
+ rewind: UIBarButtonSystemItemRewind,
376
+ fast_forward: UIBarButtonSystemItemFastForward,
377
+ undo: UIBarButtonSystemItemUndo,
378
+ redo: UIBarButtonSystemItemRedo,
379
+ page_curl: UIBarButtonSystemItemPageCurl
380
+ }[symbol] || UIBarButtonSystemItemDone
381
+
382
+ item = UIBarButtonItem.alloc.
383
+ initWithBarButtonSystemItem(name,
384
+ target:self,
385
+ action:action_name)
386
+ item
387
+ end
388
+
389
+ # 系统add按扭
390
+ def add_item(action_name)
391
+ item = UIBarButtonItem.alloc.
392
+ initWithBarButtonSystemItem(UIBarButtonSystemItemAdd,
393
+ target:self,
394
+ action:action_name)
395
+ item
396
+ end
397
+
398
+ # 系统refresh按扭
399
+ def refresh_item(action_name)
400
+ item = UIBarButtonItem.alloc.
401
+ initWithBarButtonSystemItem(UIBarButtonSystemItemRefresh,
402
+ target:self,
403
+ action:action_name)
404
+ item
405
+ end
406
+
407
+ def textfield(frame, placeholder='', text='', is_secure=false)
408
+ frame = get_frame(frame)
409
+ field = UITextField.alloc.initWithFrame(frame)
410
+ field.placeholder = placeholder.to_s
411
+ field.secureTextEntry = is_secure
412
+ field.borderStyle = UITextBorderStyleNone
413
+ # UITextBorderStyleRoundedRect
414
+ field.returnKeyType = UIReturnKeyNext
415
+ field.delegate = self
416
+ field.text = text.to_s
417
+ field
418
+ end
419
+
420
+ def textview(args={})
421
+ frame = get_frame(args[:frame])
422
+ tv = UITextView.alloc.initWithFrame(frame)
423
+ tv.font = UIFont.systemFontOfSize(args[:font_size]) if args[:font_size]
424
+ tv.layer.cornerRadius = args[:radius] if args[:radius]
425
+ tv.delegate = self
426
+ tv
427
+ end
428
+
429
+ def password_field(frame, placeholder='', text='')
430
+ textfield(frame, placeholder.to_s, text.to_s, true)
431
+ end
432
+
433
+ def add_subviews(view, objs)
434
+ objs.each { |obj| view.addSubview(obj) if !obj.nil? }
435
+ view
436
+ end
437
+
438
+ def set_image(btn, name)
439
+ btn.setImage(name.uiimage, forState:UIControlStateNormal)
440
+ btn
441
+ end
442
+
443
+
444
+ # 生成segement
445
+ def segment(frame, items, actions, default=0, color='#FF9900')
446
+
447
+ items = items.split(",")
448
+ actions = actions.split(",")
449
+
450
+ #初始化UISegmentedControl
451
+ segmentedControl = UISegmentedControl.alloc.initWithItems(items)
452
+ segmentedControl.frame = get_frame(frame)
453
+ segmentedControl.selectedSegmentIndex = default
454
+ segmentedControl.tintColor = color.uicolor
455
+ segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain
456
+ segmentedControl.on(:value_changed) do
457
+ if actions.length >= segmentedControl.selectedSegmentIndex + 1
458
+ action = actions[segmentedControl.selectedSegmentIndex]
459
+ send(action)
460
+ end
461
+ end
462
+
463
+ segmentedControl
464
+ end
465
+
466
+ def friendly_time(from_time, include_seconds = false)
467
+ if from_time.class == Bignum
468
+ time_value = from_time - Time.now.to_i
469
+ else
470
+ time_value = from_time - Time.now
471
+ end
472
+ distance_in_minutes = (((time_value).abs)/60).round
473
+ distance_in_seconds = ((time_value).abs).round
474
+
475
+ case distance_in_minutes
476
+ when 0..1
477
+ return (distance_in_minutes == 0) ? '不到1分钟' : '1分钟' unless include_seconds
478
+ case distance_in_seconds
479
+ when 0..4 then '刚刚'
480
+ when 5..9 then '10秒前'
481
+ when 10..19 then '20秒前'
482
+ when 20..39 then '半分钟前'
483
+ when 40..59 then '不到1分钟'
484
+ else '1分钟'
485
+ end
486
+
487
+ when 2..44 then "#{distance_in_minutes}分钟前"
488
+ when 45..89 then '1小时前'
489
+ when 90..1439 then "#{(distance_in_minutes.to_f / 60.0).round} 小时前"
490
+ when 1440..2879 then '1天前'
491
+ when 2880..43199 then "#{(distance_in_minutes / 1440).round}天前"
492
+ when 43200..86399 then '1个月前'
493
+ when 86400..525599 then "#{(distance_in_minutes / 43200).round}个月"
494
+ when 525600..1051199 then '1年左右'
495
+ else "#{(distance_in_minutes / 525600).round}年"
496
+ end
497
+ end
498
+
499
+ def log(msg)
500
+ NSLog(msg)
501
+ end
502
+
503
+
504
+ def empty_cell
505
+ cell = UITableViewCell.alloc.init
506
+ cell.contentView.subviews.each do |subview|
507
+ subview.removeFromSuperview
508
+ end
509
+ cell
510
+ end
511
+
512
+ def loading_view(height=nil)
513
+ height = height || self.view.frame.size.height
514
+ bg = create_view("0,0,320,#{height}", '#FFFFFF', 0)
515
+
516
+ progressInd = UIActivityIndicatorView.alloc.initWithActivityIndicatorStyle(UIActivityIndicatorViewStyleGray)
517
+ bg.addSubview progressInd
518
+ progressInd.center= [110, height/2]
519
+ progressInd.startAnimating
520
+ bg.addSubview label('请稍后...', [[130, height/2-7], [100,16]], 16, false, '#878787')
521
+ bg
522
+ end
523
+
524
+
525
+ def open_url(url)
526
+ unless url.is_a?(NSURL)
527
+ url = NSURL.URLWithString(url)
528
+ end
529
+ UIApplication.sharedApplication.openURL(url)
530
+ end
531
+
532
+
533
+ # Displays a UIAlertView.
534
+ #
535
+ # title - The title as a String.
536
+ # args - The title of the cancel button as a String, or a Hash of options.
537
+ # (Default: { cancel_button_title: 'OK' })
538
+ # cancel_button_title - The title of the cancel button as a String.
539
+ # message - The main message as a String.
540
+ # block - Yields the alert object if a block is given, and does so before the alert is shown.
541
+ #
542
+ # Returns an instance of BW::UIAlertView
543
+
544
+ def alert(title, args={})
545
+ alert = UIAlertView.alloc.initWithTitle(title,
546
+ message:'',
547
+ delegate:self,
548
+ cancelButtonTitle:"确定",
549
+ otherButtonTitles:nil)
550
+
551
+ alert.show
552
+ alert
553
+ end
554
+
555
+ end
556
+ end