6月13日作業方塊的移動

來源:互聯網
上載者:User

標籤:class   tar   color   get   width   set   

import UIKit

 

class ViewController: UIViewController {

    var greenView:UIView!

    

    

    override func viewDidLoad() {

        

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        

        //建立四個system類型的按鈕,分別用來實現視圖的向左,向右,向上,向下的移動

        

        //建立讓視圖左移的system類型的按鈕

        var leftbutton: UIButton = UIButton.buttonWithType(UIButtonType.System)as UIButton

        //設定按鈕大小和位置

        leftbutton.frame = CGRect(x:60,y:200,width:30,height:30)

        //設定標題

       

        leftbutton.setTitle ("←", forState: UIControlState.Normal)

        leftbutton.setTitle ("左", forState: UIControlState.Highlighted)

        //添加動作

        leftbutton.addTarget(self,action:"didClicked1:",forControlEvents:UIControlEvents.TouchUpInside)

        //顯示按鈕

        self.view.addSubview(leftbutton)

        

        //建立讓視圖右移的system類型的按鈕

        var rightbutton: UIButton = UIButton.buttonWithType(UIButtonType.System)as UIButton

        //設定按鈕大小和位置

        rightbutton.frame = CGRect(x:100,y:200,width:30,height:30)

        //設定標題

        

        rightbutton.setTitle ("→", forState: UIControlState.Normal)

        rightbutton.setTitle ("右", forState: UIControlState.Highlighted)

        //添加動作

        rightbutton.addTarget(self,action:"didClicked2:",forControlEvents:UIControlEvents.TouchUpInside)

        //顯示按鈕

        self.view.addSubview(rightbutton)

        

        //建立讓視圖上移的system類型的按鈕

        var upbutton: UIButton = UIButton.buttonWithType(UIButtonType.System)as UIButton

        //設定按鈕大小和位置

        upbutton.frame = CGRect(x:140,y:200,width:30,height:30)

        //設定標題

        

        upbutton.setTitle ("↑", forState: UIControlState.Normal)

        upbutton.setTitle ("上", forState: UIControlState.Highlighted)

        //添加動作

        upbutton.addTarget(self,action:"didClicked3:",forControlEvents:UIControlEvents.TouchUpInside)

        //顯示按鈕

        self.view.addSubview(upbutton)

        

        //建立讓視圖下移的system類型的按鈕

        var downbutton: UIButton = UIButton.buttonWithType(UIButtonType.System)as UIButton

        //設定按鈕大小和位置

        downbutton.frame = CGRect(x:180,y:200,width:30,height:30)

        //設定標題

        

        downbutton.setTitle ("↓", forState: UIControlState.Normal)

        downbutton.setTitle ("下", forState: UIControlState.Highlighted)

        //添加動作

        downbutton.addTarget(self,action:"didClicked4:",forControlEvents:UIControlEvents.TouchUpInside)

        //顯示按鈕

        self.view.addSubview(downbutton)

 

        

        //建立一個視圖

        var f1 = CGRect(x:50,y:50,width:70,height:100)

        greenView = UIView(frame:f1)

        //定義視圖的背景色為綠色

        greenView.backgroundColor = UIColor.greenColor()

        //顯示視圖

        self.view.addSubview(greenView)

        

        

        

        

 

    }

    //建立方法實現視圖的移動

    

    func didClicked1(sender: UIButton) {

        

        var c = greenView.frame

        if c.origin.x == 0

        {

            return

        }

        else

        {

            var newFrame = CGRect(x: c.origin.x - 10,y: c.origin.y,width:c.size.width,height:c.size.height)

            greenView.frame = newFrame

        }

        

 

        

       

    }

    

    func didClicked2(sender: UIButton) {

        

        var c = greenView.frame

        if c.origin.x == 250

        {

            return

        }

        else

        {

            var newFrame = CGRect(x: c.origin.x + 10,y: c.origin.y,width:c.size.width,height:c.size.height)

            greenView.frame = newFrame

        }

        

        

        

        

    }

    

    func didClicked3(sender: UIButton) {

        

        var c = greenView.frame

        if c.origin.y == 0

        {

            return

        }

        else

        {

            var newFrame = CGRect(x: c.origin.x,y: c.origin.y - 10,width:c.size.width,height:c.size.height)

            greenView.frame = newFrame

        }

        

        

        

        

    }

    

    func didClicked4(sender: UIButton) {

        

        var c = greenView.frame

        if c.origin.y == 380

        {

            return

        }

        else

        {

            var newFrame = CGRect(x: c.origin.x,y: c.origin.y + 10,width:c.size.width,height:c.size.height)

            greenView.frame = newFrame

        }

        

        

        

        

    }

    

 

 

override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }

 

 

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.