Absolute Swift初学者。
我一直试图让一套简单的png动画。
这就是我所拥有的:
非常感谢任何帮助。
由于
#import <UIKit/UIKit.h>
class ViewController: UIViewController {
@IBOutlet var myImageView : UIImageView
@IBOutlet var animationBtn : UIButton
var imageList = UIImage[]()
@IBAction func animationBtnClicked(sender : AnyObject) {
startAnimation()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for i in 1...13
{
let imageName = "\(i)"
imageList += UIImage(named: imageName)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func startAnimation() -> Void
{
if !myImageView.isAnimating()
{
myImageView.animationImages = imageList
myImageView.startAnimating()
animationBtn.setTitle("Stop Animation", forState: UIControlState.Normal)
} else
{
myImageView.stopAnimating()
myImageView.image = UIImage(named:"bomb.jpg")
animationBtn.setTitle("Start Animation", forState: UIControlState.Normal)
}
}
}
以下是调整后的代码,如果它可以帮助任何人。 干杯
导入UIKit
类ViewController:UIViewController {
@IBOutlet var myImageView: UIImageView!
@IBOutlet var animationBtn: UIButton!
var imageList = [UIImage]()
@IBAction func animationBtnClicked(sender: AnyObject) {
startAnimation()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for i in 1...99
{
let imageName = "\(i)"
imageList.append(UIImage(named: imageName)!)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func startAnimation() -> Void
{
myImageView.animationImages = imageList
myImageView.startAnimating()
}
}
答案 0 :(得分:2)
这些错误消息显示确切的问题。
要修复has no initializers
,您需要在班级中至少实现一个init函数。
要修复IBOutlet has non-optional
,您需要将IBOutlet
变量更改为选项。
@IBOutlet var myImageView : UIImageView!
@IBOutlet var animationBtn : UIButton!
用于修复[UIImage] is not identical to UInt8
。 +=
不能用于将UIImage
附加到[UIImage]
您需要更改:
imageList += UIImage(named: imageName)
到
imageList.append(UIImage(named: imageName))