在Xcode 8上按按钮移动图像

时间:2017-01-01 13:00:30

标签: swift image animation uiimageview



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) {
    
       
        UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseInOut, animations: {
            self.imageView.frame.origin.x += 50
        }, completion: nil)
    
       
}
    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.
    }
    */

}




the code

我使用swift 3 Xcode 8和单个视图应用程序。我的问题是我想使用按钮使图像向右移动。很多关于这个的视频和其他问题都不适用于Xcode 8,所以任何建议都会非常感激。

1 个答案:

答案 0 :(得分:0)

您是否尝试过以下操作?

var imageView = UIImageView() //initialize properly, this is just for reference

@IBAction func buttonPressed(sender: UIButton){
    UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseInOut, animations: {
        imageView.frame.origin.x -= 50
    }, completion: nil)
}

要初始化您的图片,您应该执行以下操作:

var image = UIImage(named: "yourImageName.png")

var imageView = UIImageView(image: image)

请注意,您使用的照片应导入Xcode,并且您需要确保名称与image

的初始化完全匹配