如何在Swift中使用外部类作为NSTimer.scheduledTimerWithTimeInterval的目标

时间:2016-04-06 21:39:05

标签: ios swift selector nstimer target

我有一个Swift类,我想重复使用按钮动画。

//
//  ButtonAnimation.swift

import Foundation
import UIKit


@objc class ButtonAnimation : NSObject {

    let animationButtion :UIButton!
    let animationLastStep : Int
    let animationStepIncrement : Int
    let timer : NSTimer
    let baseName : String
    var animationStep: Int


    required init?(animationTimer: NSTimer) {

        self.timer = animationTimer
        let configuration: [String : AnyObject] = self.timer.userInfo as! [String : AnyObject]

        self.animationButtion =  configuration["Button"] as! UIButton
        self.animationLastStep = configuration["stop"] as! Int
        self.animationStep = configuration["start"] as! Int
        self.baseName = configuration["basefilename"] as! String

        if (self.animationLastStep > self.animationStep) {
            self.animationStepIncrement = 1
        } else { self.animationStepIncrement = -1 }

    }


    @objc
    func animateButton() {

        self.animationStep += self.animationStepIncrement

        if (self.animationStep == self.animationLastStep) {
            self.timer.invalidate()
        }

        print("image \(self.baseName)\(String(format:"%02i",self.animationStep))")

        if let image = UIImage(named: String(self.baseName ) + String(format:"%02i",self.animationStep)) {
            self.animationButtion.setImage(image, forState: .Normal)
        }

    }

}

此类用于viewController并由

调用
    let dictionary: [String : AnyObject] = ["Button" : sortOnSeverityButton,
                                            "start": 0,
                                            "stop": 20,
                                            "basefilename": severityButtonAnimBasename]

    severityButtonTimer = NSTimer.scheduledTimerWithTimeInterval(severityButtonTimeInterval, target: ButtonAnimation.self , selector: #selector(ButtonAnimation.animateButton), userInfo: dictionary, repeats: true)

编译代码时没有错误,但在执行开始动画的代码时会出现异常。

  

***由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:   ' + [homecaremonitor.ButtonAnimation animateButton]:无法识别   选择器发送到类0x103353c30'

如果我正在解释异常,那么选择器就会出现问题。但我不知道如何解决这个问题。

任何帮助表示赞赏! 彼得

我现在改变了调用

        let buttonAnimation :ButtonAnimation = ButtonAnimation()

        severityButtonTimer = NSTimer.scheduledTimerWithTimeInterval(timeButtonTimeInterval, target: buttonAnimation , selector: #selector(ButtonAnimation.self.animateButton), userInfo: dictionary, repeats: true)

最初将初始化程序中的代码移动到func animateButton

    if (self.timer == nil) {

        self.timer = animationTimer
            let configuration: [String : AnyObject] = self.timer.userInfo as! [String : AnyObject]
                self.animationButton =  configuration["Button"] as! UIButton
                self.animationLastStep = configuration["stop"] as! Int
                self.animationStep = configuration["start"] as! Int
                self.baseName = configuration["basefilename"] as! String

                if (self.animationLastStep > self.animationStep) {
                    self.animationStepIncrement = 1
                } else { self.animationStepIncrement = -1 }

        self.startValue = self.animationStep

    }

我不得不在创建计时器之前创建一个实例。将self作为Target调用时,NSTimer实例将传递给初始化程序。当我想使用外部类时,这似乎是不可能的。因此,我必须在Selector调用的函数中创建一个初始化器。

0 个答案:

没有答案