2017-01-07 26 views
0

我正在嘗試在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. 
    } 
    */ 

} 
+0

你最初在哪裏設置你的'UIImageView'的框架? – toddg

+0

您是否對imageView設置了任何約束? – shallowThought

+0

'buttonPressed'函數中的代碼是否執行?您可以在該行上放置一箇中斷點,以查看應用程序是否停留在該中斷點,如果不是,那麼機會就是您的按鈕的'onTouchUpInside'事件未連接。 – Zhang

回答

0

從代碼中刪除您的按鈕,然後用鼠標右鍵單擊您的按鈕,然後點擊「X」來刪除舊代碼的連接。 enter image description here

現在添加按鈕。我認爲你的按鈕設置錯誤。 當您右鍵單擊該按鈕時,看看發送事件是什麼。 應該這樣設置。

如果這是你正在尋找的答案不要忘記除了答案。

enter image description here

+0

只有prolem是我正在使用單個視圖應用程序 – QUINN

+0

您是否刪除了按鈕並重做它? – dscrown

+0

是的,它仍然不起作用 – QUINN

0

好吧,我想我看到它。你需要控制你的ImageView拖動到你的代碼,並將其連接爲一個插座,然後將ImageView的出口。你正在移動變量。

enter image description here

enter image description here

如果這是你要找的人不要忘記,除了答案的答案。