通过 Xcode 中的按钮移动图像 8
moving an image by a button in 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.
}
*/
}
从您的代码中删除您的按钮,然后右键单击您的按钮并单击 "x" 以删除与旧代码的连接。
现在重新添加按钮。我认为你的按钮设置错误。
当您右键单击该按钮时,查看发送事件是什么。
应该这样设置。
如果这是您正在寻找的答案,请不要忘记排除答案。
好的,我想我看到了。您需要控制将 imageView 拖到您的代码中并将其连接为插座,然后移动 imageView 插座。你正在移动变量。
如果这是您正在寻找的答案,请不要忘记排除答案。
我正在尝试移动 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.
}
*/
}
从您的代码中删除您的按钮,然后右键单击您的按钮并单击 "x" 以删除与旧代码的连接。
现在重新添加按钮。我认为你的按钮设置错误。 当您右键单击该按钮时,查看发送事件是什么。 应该这样设置。
如果这是您正在寻找的答案,请不要忘记排除答案。
好的,我想我看到了。您需要控制将 imageView 拖到您的代码中并将其连接为插座,然后移动 imageView 插座。你正在移动变量。
如果这是您正在寻找的答案,请不要忘记排除答案。