美文网首页
iOS-NSTimer()

iOS-NSTimer()

作者: Swift从入门到崩溃 | 来源:发表于2016-08-02 23:49 被阅读0次

NSTimer

每隔一定时间执行某个函数

//创建定时器
//参数1.定时时间
//参数2.调用方法的对象
//参数3.定时时间到了自动调用的方法
//参数4.nil
//参数5.是否重复

NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: #selector(timmer), userInfo: nil, repeats: true)

实例:使用NSTimer实现帧动画

import UIKit
class ViewController: UIViewController {
var imageview = UIImageView()
override func viewDidLoad() {
    super.viewDidLoad()
    backImage()
    creatUI()
    //创建定时器
    //参数1.定时时间
    //参数2.调用方法的对象
    //参数3.定时时间到了自动调用的方法
    //参数4.nil
    //参数5.是否重复
    
    NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: #selector(timmer), userInfo: nil, repeats: true)

}
//MARK:定时器到时会调用的方法
func timmer() {
    //改变图片的位置
    //1.先拿到图片原来的位置
    let frame = imageview.frame
    imageview.frame = CGRectMake(frame.origin.x+10,frame.origin.y,121,96)
    if frame.origin.x>self.view.bounds.width {
        imageview.frame.origin.x = 0
    }
    
}

//MARK:创建界面
//背景图片
func backImage()  {
    let imageview1 = UIImageView.init(image: UIImage.init(named: "back2.jpg"))
    imageview1.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
    self.view.addSubview(imageview1)
}
//搭建页面
func creatUI() {
    imageview = UIImageView.init(image: UIImage.init(named: "DOVE 1"))
    imageview.frame = CGRectMake(30,300,121,96)
    self.view.addSubview(imageview)
    //设置动画数组
    var imageViewarr = [UIImage]()
    
    for i in 1...18{
         let image1 = UIImage.init(named: "DOVE \(i)")
        imageViewarr.append(image1!)
    }
    imageview.animationImages = imageViewarr
    imageview.animationDuration = 1
    imageview.animationRepeatCount = 0
    imageview.startAnimating()
   }
}

相关文章

  • iOS-NSTimer()

    NSTimer 每隔一定时间执行某个函数 //创建定时器//参数1.定时时间//参数2.调用方法的对象//参数3....

  • iOS-NSTimer

    NSTimer是iOS常见定时器。它经过特定时间间隔就会触发,将指定的消息发送到目标对象。定时器是线程通知自己做某...

  • iOS-NSTimer 使用

    1.NSTimer的创建方法 2. NStimer的开启 3. NStimer的停止 4. NStimer的其他属...

  • iOS-NSTimer强引用

    当使用如上代码创建一个NSTimer之后会出现一个VC-> NSTimer->VC的循环引用。导致界面不能释放。 ...

  • iOS-NSTimer释放的三种方式

    一开始在使用NSTimer的时候,会发现出现了循环引用。 出现循环引用是因为 将一个NSTimer的对象作为一个V...

  • iOS-NSTimer不同创建方式的区别

    先说总结 创建NSTimer必须加入到Runloop中才能生效,不管是手动添加还是系统添加。 当Timer加入到r...

  • iOS-NSTimer真的没有想象中的简单:NSInvocati

    在iOS开发当中,无可避免的会涉及到定时任务,比如在发送验证码时的倒计时: 小编相信每个人都遇到过这样的需求,都很...

网友评论

      本文标题:iOS-NSTimer()

      本文链接:https://www.haomeiwen.com/subject/belqsttx.html