2016-07-19 15 views
0

私は即時IOSに2つのViewControllers(AとB)を持っています。 AとBの両方がインターネットからデータを読み込みます(別途)。読み込み中にactivityIndicatorと表示したい私はコードスウィフトを複製せずに多くのVCでアクティビティインジケータを使用

のViewController A

var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() 

func activityIndicatorBegin() { 
    activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50)) 
    activityIndicator.center = self.view.center 
    activityIndicator.hidesWhenStopped = true 
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray 
    view.addSubview(activityIndicator) 
    activityIndicator.startAnimating() 
    disableUserInteraction() 

    greyView = UIView() 
    greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height) 
    greyView.backgroundColor = UIColor.blackColor() 
    greyView.alpha = 0.5 
    self.view.addSubview(greyView) 
} 

func activityIndicatorEnd() { 
    self.activityIndicator.stopAnimating() 
    enableUserInteraction() 
    self.greyView.removeFromSuperview() 
} 

とViewControllerをBの正確な同じことを行うには、次のように各VCに一度それを宣言することにより、それを悪い方法を行うと、あることactivityIndi​​catorBeginとactivityIndi​​catorEnd関数を呼び出すことができます知っていますBビューコントローラで宣言されています。しかし、私はコードをきれいにしたい。どのようにそれを行うことができますか?私は最近、コードクリーナーを作ろうとしています。

おかげで、

--UPDATE--

私は、次のコードのようなものが働くだろうimageingでしょう。しかし、私は拡張子の変数

var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() 

extension UIViewController { 
    func activityIndicatorBegin() { 
    activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50)) 
    activityIndicator.center = self.view.center 
    activityIndicator.hidesWhenStopped = true 
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray 
    view.addSubview(activityIndicator) 
    activityIndicator.startAnimating() 
    disableUserInteraction() 

    greyView = UIView() 
    greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height) 
    greyView.backgroundColor = UIColor.blackColor() 
    greyView.alpha = 0.5 
    self.view.addSubview(greyView) 
} 

func activityIndicatorEnd() { 
    self.activityIndicator.stopAnimating() 
    enableUserInteraction() 
    self.greyView.removeFromSuperview() 
    } 
} 

答えて

2

を宣言カントので、それはdoesntの1 BaseViewControllerを作成し、これら二つのメソッドを追加し、そのBaseViewControlleractivityIndicatorオブジェクトを宣言。今度はViewControllerの親クラスとしてBaseViewControllerを作成してください。あなたはインジケーターを追加したい場合はその後、単純にこの

1のように呼び出す)BaseViewControllerが

class BaseViewController: UIViewController { 
    var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func activityIndicatorBegin() { 
     activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50)) 
     activityIndicator.center = self.view.center 
     activityIndicator.hidesWhenStopped = true 
     activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray 
     view.addSubview(activityIndicator) 
     activityIndicator.startAnimating() 
     disableUserInteraction() 

     greyView = UIView() 
     greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height) 
     greyView.backgroundColor = UIColor.blackColor() 
     greyView.alpha = 0.5 
     self.view.addSubview(greyView) 
    } 

    func activityIndicatorEnd() { 
     self.activityIndicator.stopAnimating() 
     enableUserInteraction() 
     self.greyView.removeFromSuperview() 
    } 
} 

2)今、この

class ViewController1: BaseViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     //Now if you want to add activityIndicator call like this 
     self.activityIndicatorBegin() 
     //or if you want remove activityIndicator 
     self.activityIndicatorEnd() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 
+0

ん私は物理的に作成する必要があります意味ストーリーボードの別の(3番目の)VCをBaseViewControllerに割り当てても何もしません。私は私の心にあるものをいくつか更新しました – user172902

+0

@EdwardHung編集された答えをチェックしてください。 –

+0

ありがとう、それは働いた。だから基本的に拡張を行うよりも:UIviewcontroller、私はクラスに変更し、別のクラスを継承してください。スマート – user172902

0

のようなあなたのすべてのviewControllerの親としてこのBaseViewControllerを割り当てて作成しましたAクラスとBクラスで複製できるすべてのコードで1つの基本クラスを作成しようとしていますか?このクラスはcompletly異なることをやっている場合は、活動の指標のための拡張機能を追加することができます

+0

1つのVCはtableViewで、もう1つはcollectionViewです。私はどのように拡張することでそれをやっていくのですか?上記のアップデートをご覧ください。ありがとう – user172902

+0

@EdwardHungグローバル活動指標の例がたくさんあります。http://mpclarkson.github.io/2016/01/06/swift-uiviewcontroller_uiactivityindicator_extension/ –

1

SWIFT 4

extension UIView{ 

func activityStartAnimating(activityColor: UIColor, backgroundColor: UIColor) { 
    let backgroundView = UIView() 
    backgroundView.frame = CGRect.init(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height) 
    backgroundView.backgroundColor = backgroundColor 
    backgroundView.tag = 475647 

    var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() 
    activityIndicator = UIActivityIndicatorView(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50)) 
    activityIndicator.center = self.center 
    activityIndicator.hidesWhenStopped = true 
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray 
    activityIndicator.color = activityColor 
    activityIndicator.startAnimating() 
    self.isUserInteractionEnabled = false 

    backgroundView.addSubview(activityIndicator) 

    self.addSubview(backgroundView) 
} 

func activityStopAnimating() { 
    if let background = viewWithTag(475647){ 
     background.removeFromSuperview() 
    } 
    self.isUserInteractionEnabled = true 
} 

}

USAGE:

self.view.activityStartAnimating(activityColor: UIColor.white, backgroundColor: UIColor.black.withAlphaComponent(0.5)) 


self.view.activityStopAnimating() 
関連する問題