可以使用NSTimer
用于显示到下面的代码,如:
myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("myPerformeCode:"), userInfo: nil, repeats: true)
和辅助方法是:
func myPerformeCode(timer : NSTimer) {
count++
if count < totalCount {
let filePath = NSBundle.mainBundle().pathForResource("\(count)", ofType: "gif")
let gif = NSData(contentsOfFile: filePath!)
self.webView.loadData(gif!, MIMEType: "image/gif",textEncodingName: String(), baseURL: NSURL())
self.webView.userInteractionEnabled = false
} else {
count = 1
}
}
完整代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
let totalCount = 61
var count = 1
var myTimer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func start(sender: AnyObject) {
myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("myPerformeCode:"), userInfo: nil, repeats: true)
}
func myPerformeCode(timer : NSTimer) {
print("Called")
count++
if count < totalCount {
let filePath = NSBundle.mainBundle().pathForResource("\(count)", ofType: "gif")
let gif = NSData(contentsOfFile: filePath!)
self.webView.loadData(gif!, MIMEType: "image/gif",textEncodingName: String(), baseURL: NSURL())
self.webView.userInteractionEnabled = false
} else {
count = 1
}
}
}
结果:
之后,你可以随时进行切换视图与myTimer.invalidate()
删除定时器。
如果您的目的只是显示GIF图像,请考虑使用FLAnimatedImage。 –