NSTimer问题自动开始图像幻灯片显示

时间:2019-02-22 01:40:52

标签: ios swift nstimer

我在启动屏幕后尝试在应用程序上开始图像幻灯片播放时遇到问题。由于某种原因,启动时间太长,然后我必须单击上一个按钮或下一个按钮或我的UISlider来显示幻灯片,然后应用程序崩溃。我的代码中缺少什么吗?我要做的就是自动开始图像幻灯片播放。这是下面的代码。另外,在下面的链接中,我有一个截图显示了我的图像幻灯片的设置方式。

Image Slideshow Setup

var Array = [UIImage]()
var counter = 2
var time = Timer()


@IBOutlet weak var menuButton: UIBarButtonItem!
@IBOutlet weak var ImageView: UIImageView!
@IBOutlet weak var Slider1: UISlider!
@IBAction func Slider(_ sender: UISlider) {
    _ = 0
    let value = Int(sender.value)
    ImageView.image = Array[value]
}

@IBAction func NextButton(_ sender: Any) {
    Slider1.value += 1
    ImageView.image = Array[Int(Slider1.value)]
    self.ImageView.animationImages = self.Array
    self.ImageView.animationDuration = 3.0
    self.ImageView.animationRepeatCount = 0
    self.ImageView.startAnimating()

    UIView.transition(with: self.ImageView, duration: 5.0, options: .transitionCrossDissolve, animations: {self.ImageView.image = self.ImageView.image}, completion: nil)

}


@IBAction func PrevButton(_ sender: Any) {
    Slider1.value -= 1
    ImageView.image = Array[Int(Slider1.value)]
    self.ImageView.animationImages = self.Array
    self.ImageView.animationDuration = 3.0
    self.ImageView.animationRepeatCount = 0
    self.ImageView.startAnimating()

    UIView.transition(with: self.ImageView, duration: 5.0, options: .transitionCrossDissolve, animations: {self.ImageView.image = self.ImageView.image}, completion: nil)
}

 //Set Status Bar to light content (white)
override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

override func viewDidLoad() {
    //Set Navigation Bar color Example Home, Back button
    self.navigationItem.backBarButtonItem?.tintColor = UIColor.white;

   time = Timer.scheduledTimer(timeInterval: 5,
                        target: self,
                        selector: #selector(getter: self.Slider1),
                        userInfo: nil,
                        repeats: true)
super.viewDidLoad()

setup()

   Array = [#imageLiteral(resourceName: "MainImage1.jpg"), #imageLiteral(resourceName: "MainImage2.jpg"), #imageLiteral(resourceName: "MainPage3.jpg"), #imageLiteral(resourceName: "MainImage4.jpg"), #imageLiteral(resourceName: "MainImage5.jpg"), #imageLiteral(resourceName: "MainImage6.jpg"), #imageLiteral(resourceName: "MainImage7.jpg"), #imageLiteral(resourceName: "MainImage8.jpg")]

    sideMenus()

    // Do any additional setup after loading the view.
}


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

func setup(){
self.navigationController?.navigationBar.tintColor = UIColor.white
}

override var prefersStatusBarHidden: Bool{
    return false
}

var navigationBarAppearace = UINavigationBar.appearance()
 override func viewDidAppear(_ animated: Bool){
}



func sideMenus() {

    if revealViewController() != nil {

        menuButton.target = revealViewController()
        menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
        revealViewController().rearViewRevealWidth = 275
        revealViewController().rightViewRevealWidth = 160

        view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }
}
    }` 

1 个答案:

答案 0 :(得分:3)

计时器的目标选择器是在您指定的时间间隔调用的函数。在上面的代码中,您将滑块属性的getter用作计时器要调用的函数-这并没有满足您的要求。计时器将每5秒调用一次该getter,但是getter函数所做的只是“获取” slider属性的值。

例如,您希望计时器的选择器为您某事,以触发“下一步”按钮功能。为什么不使用计时器创建的形式,该形式允许您指定用于包装行为的闭包?像这样:

time = Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { _ in
    self.NextButton(self)
}

(在Swift中,这似乎比使用#selector指向裸露的objc函数要优雅得多。)