6月13日作业方块的移动

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:10,y:10,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:290,y:10,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:80,y:10,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:200,y:10,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,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

        {

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

            greenView.frame = newFrame

        }

        

        

       

    }

    

    //创建方法实现视图的右移

    func didClicked2(sender: UIButton) {

        

        var c = greenView.frame

        if c.origin.x < 250

        {

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

            greenView.frame = newFrame

        }

        

        

        

        

    }

    

   //创建方法实现视图的上移 

    func didClicked3(sender: UIButton) {

        

        var c = greenView.frame

        if c.origin.y > 40

        {

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

            greenView.frame = newFrame

        }

        

        

        

        

    }

    

    //创建方法实现视图的下移

    func didClicked4(sender: UIButton) {

        

        var c = greenView.frame

        if c.origin.y < 380

       

        {

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

            greenView.frame = newFrame

        }

        

        

        

        

    }

    

override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }

}

原文地址:https://www.cnblogs.com/changningios/p/3787636.html