gpt4 book ai didi

swift - 如何在 swift UIWebView 中重新加载 gif

转载 作者:行者123 更新时间:2023-11-28 06:57:15 25 4
gpt4 key购买 nike

/* 我正在尝试在 UIwebView 中快速重新加载 gif,但我不知道如何在连续循环中重新加载 gif。加载请求不起作用,甚至 reload() */

filePath = NSBundle.mainBundle().pathForResource("alienImage", ofType: "gif")

let gif = NSData(contentsOfFile: filePath!)

let webViewBG = UIWebView(frame: self.view.frame)

webViewBG.loadData(gif!, MIMEType: "image/gif",textEncodingName: String(), baseURL: NSURL())

webViewBG.userInteractionEnabled = false;

self.view.addSubview(webViewBG)

最佳答案

您可以使用 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
}
}
}

结果:

enter image description here

之后,您可以在切换 View 时使用 myTimer.invalidate() 删除计时器。

关于swift - 如何在 swift UIWebView 中重新加载 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33116853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com