How to initialize properties that depend on each other

后端 未结 4 967
心在旅途
心在旅途 2020-11-21 05:10

I want a picture to move to the bottom. If I press a button the pic should move down by 1.

I added the picture and a button:

var corX = 0
var corY =         


        
4条回答
  •  被撕碎了的回忆
    2020-11-21 05:49

    //
    //  ViewController.swift
    //
    //  Created by Shivank Agarwal on 19/05/18.
    //  Copyright © 2018 Shivank Agarwal. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
    
        var corX = 0
        var corY = 0
        var runter: UIButton = UIButton()
        var image = UIImage(named: "panzerBlau.jpg")
        var panzer = UIImageView()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            panzer.image = image;
            self.view.addSubview(panzer);
            panzer.frame = CGRect(x: CGFloat(corX), y: CGFloat(corY), width: 30, height: 40)
            runter.backgroundColor = UIColor.red
            view.addSubview(runter)
            view.addSubview(panzer)
            runter.addTarget(self, action: Selector(("fahren")), for:UIControlEvents.touchUpInside)
        }
    
        private func fahren(){
            corY += 100
        }
    
        private func updatePanzerFrame(){
            panzer.frame = CGRect(x: CGFloat(corX), y: CGFloat(corY), width: 30, height: 40)
        }
    }
    
    Note: Do not add panzer imageView every time when user tap only add it on viewDidLoad()
    

提交回复
热议问题