- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在游戏中无限滚动背景。当我尝试这样做时,我的应用程序崩溃了:
@IBAction func levelClicked(sender: AnyObject) {
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenHeight = screenSize.height
let imageName = "levelBackground"
let image = UIImage(named: imageName)
imageView = UIImageView(image: image!)
imageView.frame = CGRect (x: 0, y: 0, width: 5500, height: screenHeight)
view.addSubview(imageView)
timer = NSTimer.scheduledTimerWithTimeInterval(0.5,
target: self,
selector: Selector("moveBackground"),
userInfo: nil,
repeats: true)
}
func moveBackground() {
imageView.frame = CGRect(x: imageView.frame.origin.x - 5,
y: imageView.origin.y,
width: 5500,
height: UIScreen.mainScreen().bounds.height)
}
这是我的崩溃日志:
2015-01-03 15:57:56.579 zombieSurvival[3395:708290] -[zombieSurvival.firstBuildingViewController moveBackground]: unrecognized selector sent to instance 0x15e7e150
2015-01-03 15:57:56.585 zombieSurvival[3395:708290] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[zombieSurvival.firstBuildingViewController moveBackground]: unrecognized selector sent to instance 0x15e7e150'
*** First throw call stack:
(0x29dad5f7 0x374e7c77 0x29db2abd 0x29db0999 0x29ce1b88 0x2aac1a45 0x29d73f07 0x29d73a89 0x29d71cf3 0x29cbeb31 0x29cbe943 0x31077051 0x2d2b46f1 0xccc04 0xccc40 0x37a83aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
最佳答案
我不是 swift 方面的专家,但似乎这行不通:
您将 moveBackground
声明为 levelClicked
内的函数,但在计时器上您将 self
引用为目标。该类本身没有方法moveBackground
。它仅在您的函数 levelClicked
内可见。
尝试将整个 moveBackground
移出 levelClicked
。
为了使 moveBackground
工作,我想你必须给 imageView
一个属性。
@IBAction func levelClicked(sender: AnyObject) {
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenHeight = screenSize.height
let imageName = "levelBackground"
let image = UIImage(named: imageName)
self.imageView = UIImageView(image: image!)
self.imageView.frame = CGRect (x: 0, y: 0, width: 5500, height: screenHeight)
view.addSubview(self.imageView)
timer = NSTimer.scheduledTimerWithTimeInterval(0.5,
target: self,
selector: Selector("moveBackground"),
userInfo: nil,
repeats: true)
}
func moveBackground() {
self.imageView.frame = CGRect(x: self.imageView.frame.origin.x - 5,
y: self.imageView.origin.y,
width: 5500,
height: UIScreen.mainScreen().bounds.height)
}
关于swift - 如何在swift中实现无限滚动背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27759523/
我是一名优秀的程序员,十分优秀!