2016-04-06 36 views
1

我有一個通用的NSObject動畫班,我爲一個新項目創建了一個動畫班,我試圖用新班級重構一些舊的項目。一切都很好,直到我找到了一些動畫w /完成塊,這就拋棄了我的計劃,從我的視圖控制器中刪除了過多的冗餘代碼。下面是我有...在Swift中創建一個通用的完成處理程序

Animator.swift

class Animator: NSObject { 
    var control: UIControl? // Can accept everything that's a subclass of UIControl 

    override init() { 
     super.init() 
    } 

    // FIXME: figure out how to add a completion block as a parameter on a method call 
    func animateControl(control: UIControl) { 
     control.transform = CGAffineTransformMakeScale(0.75, 0.75) 
     UIView.animateWithDuration(0.5, 
            delay: 0, 
            usingSpringWithDamping: 0.3, 
            initialSpringVelocity: 5.0, 
            options: UIViewAnimationOptions.AllowUserInteraction, 
            animations: { 
            control.transform = CGAffineTransformIdentity 
     }) { (value: Bool) -> Void in 
      // completion block 
      // ** method as a parameter goes here? ** 
     } 
    } 
} 

要使用它,而不是所有的東西輸入到動畫按鈕,只需要調用類從MyViewController.swift

MyViewController.swift

// Property declaration 
let animator = Animator() 

// Tie it to an IBAction 
@IBAction func myButtonAction(sender: UIButton) { 
    animator.animateControl(sender, methodIWantToRunAfterAnimateControlFinishes) 
} 

func methodIWantToRunAfterAnimateControlFinishes() { 
    // Do Stuff 
} 

我怎麼養活animateControl的初始化程序是作爲完成塊運行的一種方法?我看着this(不是我的網站,但這是我的感受),但我無法讓它工作。

更新

我曾與語法搏鬥了一點點,但這裏是讓我在終點線的初始化代碼:

func animateControl(control: UIControl, completion: (() -> Void)?) { 
    control.transform = CGAffineTransformMakeScale(0.75, 0.75) 
    UIView.animateWithDuration(0.5, 
           delay: 0, 
           usingSpringWithDamping: 0.3, 
           initialSpringVelocity: 5.0, 
           options: UIViewAnimationOptions.AllowUserInteraction, 
           animations: { 
           control.transform = CGAffineTransformIdentity 
    }) { _ in 
     // completion block 
     completion?() 
    } 
} 

此處理完成塊W /碼nil完成塊。

回答

3

作爲參數使用時一個非常簡單的完成塊看起來像這樣:

onCompletion: (() -> Void)? 

之所以() -> Void可爲空?是要在Objective-C使用。如果不需要,那麼你不需要將它放在括號內,也不要將它標記爲可空?

這將允許您傳遞一個函數,該函數不需要任何參數並且不返回任何內容。如果你想參數添加到

func animateControl(control: UIControl, onCompletion: (() -> Void)?) 

,將它們添加到結束塊的簽名:你會像這樣添加到您的簽名

func animateControl(control: UIControl, onCompletion: ((value: AnyObject) -> Void)?) 

這同樣適用於return語句:

func animateControl(control: UIControl, onCompletion: ((value: AnyObject) -> AnyObject)?) 

一旦它在你的函數的簽名,你可以通過它的名字來稱呼它:

func animateControl(control: UIControl, onCompletion: ((value: AnyObject) -> Void)?) { 
    //do stuff here 
    onCompletion(obj) 
} 
+0

謝謝你讓我擺脫這一點。不勝感激。 – Adrian

1

你可以像這樣添加一個完成處理程序,你應該可以在你的例子中調用它。

func animateControl(control: UIControl, _ completion: (() ->())? = nil) { 
    control.transform = CGAffineTransformMakeScale(0.75, 0.75) 
    UIView.animateWithDuration(0.5, 
           delay: 0, 
           usingSpringWithDamping: 0.3, 
           initialSpringVelocity: 5.0, 
           options: UIViewAnimationOptions.AllowUserInteraction, 
           animations: { 
           control.transform = CGAffineTransformIdentity 
    }) { _ in 
     completion?() 
    } 
} 
+0

謝謝你對此的幫助!我非常感謝。 – Adrian

相關問題