我正在嘗試在Xcode 8中移動圖片。我有它的代碼,但按下按鈕時圖像不會移動。有人可以幫助我解決圖片不移動的問題。用Xcode 8中的按鈕移動圖像
import UIKit
class Level2ViewController: UIViewController {
var image = UIImage(named: "ballon.png")
var imageView = UIImageView(image: "ballon")//initialize properly, this is just for reference
@IBAction func buttonPressed(_ sender: UIButton) {
self.imageView.frame.origin.x += 50
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
你最初在哪裏設置你的'UIImageView'的框架? – toddg
您是否對imageView設置了任何約束? – shallowThought
'buttonPressed'函數中的代碼是否執行?您可以在該行上放置一箇中斷點,以查看應用程序是否停留在該中斷點,如果不是,那麼機會就是您的按鈕的'onTouchUpInside'事件未連接。 – Zhang