通过Xcode 8中的按钮移动图像

时间:2017-01-07 17:29:28

标签: ios swift cocoa-touch xcode8.1

我正在尝试在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.
    }
    */

}

2 个答案:

答案 0 :(得分:0)

从代码中删除按钮,然后右键单击按钮并单击“x”以删除与旧代码的连接。 enter image description here

现在添加按钮。我认为你的按钮设置错了。 当你右键单击按钮外观并查看发送事件是什么时。 它应该以这种方式设置。

如果这是您正在寻找的答案,请不要忘记除了答案。

enter image description here

答案 1 :(得分:0)

好的我觉得我看到了..您需要控制将imageView拖到代码中并将其作为插座连接,然后移动imageView插座。你正在移动变量。

enter image description here

enter image description here

如果这是你要找的答案,除了答案之外别忘了。