Last active
December 10, 2015 01:38
-
-
Save amazedkoumei/4361041 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class InformView < UIView | |
WIDTH = 280 | |
HEIGHT = 100 | |
# singleton | |
def self.instance() | |
# Dispatch.once を使うとinitメソッドのsuperで以下のwaningがでました。どぼちて? | |
# > unknown: warning: passing a block to an Objective-C method - will be ignored | |
# Dispatch.once { @instance ||= new } | |
@instance = new if @instance.nil? | |
@instance | |
end | |
def self.show(message, target:view, animated:animated) | |
# singltonでインスタンスを作成 | |
@instance = InformView.instance | |
# Superviewからいったんremoveした上でaddSubview | |
@instance.removeFromSuperview() | |
view.addSubview(@instance) | |
# @instanceのインスタンス変数,インスタンスメソッドにアクセスするために instance_eval をコール | |
@instance.instance_eval do | |
@label.text = message | |
@indicator.startAnimating() | |
changeAlpaTo(1.0, animated:animated) | |
end | |
end | |
def self.hide(animated) | |
# instanceが解放されてたら何もしない | |
return if @instance.nil? | |
# @instanceのインスタンス変数にアクセスするために instance_eval をコール | |
@instance.instance_eval do | |
changeAlpaTo(0, animated:animated) | |
@indicator.stopAnimating() | |
end | |
end | |
# ブログ本筋には関係ないけど参考までに | |
def changeAlpaTo(alpha, animated:animated) | |
if animated | |
context = UIGraphicsGetCurrentContext() | |
UIView.beginAnimations(nil, context:context) | |
UIView.setAnimationDuration(0.5) | |
UIView.setAnimationDelegate(self) | |
self.alpha = alpha | |
UIView.commitAnimations() | |
else | |
self.alpha = alpha | |
end | |
end | |
# ブログ本筋には関係ないけど参考までに | |
def init() | |
if super | |
x = (App.window.frame.size.width - WIDTH) / 2 | |
y = (App.window.frame.size.height - HEIGHT) / 2 - 50 | |
self.frame = [[x, y], [WIDTH, HEIGHT]] | |
self.backgroundColor = UIColor.colorWithWhite(0.0, alpha:0.5) | |
self.alpha = 1.0 | |
self.layer.cornerRadius = 8.0 | |
self.layer.masksToBounds = true | |
self.clipsToBounds = true | |
@label = UILabel.new.tap do |l| | |
l.frame = [[0, 60], [WIDTH, 30]] | |
l.textColor = UIColor.whiteColor | |
l.textAlignment = NSTextAlignmentCenter | |
l.font = UIFont.boldSystemFontOfSize(18.0) | |
l.alpha = 1.0 | |
l.backgroundColor = UIColor.clearColor | |
self.addSubview(l) | |
end | |
@indicator = UIActivityIndicatorView.new.tap do |i| | |
i.initWithActivityIndicatorStyle(UIActivityIndicatorViewStyleWhiteLarge) | |
i.frame = [[0, 10], [WIDTH, 50]] | |
i.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite | |
i.backgroundColor = UIColor.clearColor | |
self.addSubview(i) | |
end | |
InformView.hide(false) | |
end | |
self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment