一个退休的老DEC PDP 8汇编程序员需要另一组眼睛。我创建了一个Hello World程序来显示该家庭的图像。动画效果很好,但是我希望用户能够停止动画,所以我添加了一个按钮来“暂停”图像,以便用户可以随意查看。但是,.stopAnimation函数会清除视图。如何防止这种情况。请客气。
import UIKit
class FamilyUnitController: UIViewController {
@IBOutlet weak var familyMember: UILabel!
@IBOutlet weak var familyPhotos: UIImageView!
var familyName: String = ""
var familyUnitArray = [String]()
@IBOutlet weak var pauseButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
enter code here
configureImageView()
}
@IBAction func pauseImage(_ sender: UIButton) {
if familyPhotos.isAnimating {
familyPhotos.stopAnimating()
pauseButton.setTitle("Paused", for: [])
} else {
familyPhotos.startAnimating()
pauseButton.setTitle("Pause", for: [])
}
}
func configureImageView() {
familyMember.text = familyUnitArray[0]
familyMember.sizeToFit()
let imagePrefix = familyUnitArray[1]
print ("configureImageView called...")
let maxImageCnt = 10
if let imageView = familyPhotos {
print ("Processing images...")
let fileManager = FileManager.default
let path = Bundle.main.resourcePath!
print (#function,">Path> ",path)
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: path)!
var imageArr = [UIImage]()
var imageCnt: Int = 0
while let element = enumerator.nextObject() as? String {
print (#function,"> element.description",element.debugDescription)
let tmpStr = element.lowercased()
let tmpImagePrefix = imagePrefix.lowercased()
if (element.hasSuffix("png") || element.hasSuffix("jpg") || element.hasSuffix("jpeg"))
&& tmpStr.hasPrefix(tmpImagePrefix)
&& imageCnt < maxImageCnt {
let elementWithPath = path+"/"+element
imageArr.append(UIImage(contentsOfFile: elementWithPath)!)
imageCnt += 1
}
}
imageView.isUserInteractionEnabled = true
imageView.animationImages = imageArr
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = UIColor.white
imageView.animationDuration = 15
imageView.startAnimating()
}
}
}
答案 0 :(得分:0)
您可以参考此处以暂停动画/不停止动画。
Is there a way to pause a CABasicAnimation?
在您的情况下,代码应类似于以下内容,您可能需要稍作更改以使其完美。
@IBAction func pauseImage(_ sender: UIButton) {
if familyPhotos.layer.speed != 0 {
let time = familyPhotos.layer.convertTime(CACurrentMediaTime(), to: nil)
familyPhotos.layer.speed = 0
familyPhotos.layer.timeOffset = time
pauseButton.setTitle("Paused", for: [])
} else {
familyPhotos.layer.beginTime = familyPhotos.layer.timeOffset - CACurrentMediaTime()
familyPhotos.layer.timeOffset = 0
familyPhotos.layer.speed = 1
pauseButton.setTitle("Pause", for: [])
}
}