zoukankan      html  css  js  c++  java
  • Swift语言之View,Button控件实现小方块在界面上的移动(纯代码实现)

    import UIKit


    class ViewController: UIViewController {

        var diamonds:UIView!

        var diamondsXY = CGRectMake(0,200,50,50)

        override func viewDidLoad() {

            super.viewDidLoad()

            //定义一个方块

            diamonds = UIView(frame: diamondsXY)

            diamonds.backgroundColor = UIColor.redColor()

            self.view.addSubview(diamonds)


            //定义向上移动的按钮

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

            btUp.frame = CGRectMake(0,30,80,20)

            btUp.setTitle("UP",forState:UIControlState.Normal)

            btUp.addTarget(self,action:"upMove:",forControlEvents:UIControlEvents.TouchUpInside)

            self.view.addSubview(btUp)

            //定义向下移动的按钮

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

            btDown.frame = CGRectMake(50,30,80,20)

            btDown.setTitle("Down",forState:UIControlState.Normal)

            btDown.addTarget(self,action:"downMove:",forControlEvents:UIControlEvents.TouchUpInside)

            self.view.addSubview(btDown)

            //定义向左移动的按钮

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

            btLeft.frame = CGRectMake(100,30,80,20)

            btLeft.setTitle("Left",forState:UIControlState.Normal)

            btLeft.addTarget(self,action:"leftMove:",forControlEvents:UIControlEvents.TouchUpInside)

            self.view.addSubview(btLeft)

            //定义向右移动的按钮

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

            btRight.frame = CGRectMake(150,30,80,20)

            btRight.setTitle("Right",forState:UIControlState.Normal)

            btRight.addTarget(self,action:"rightMove:",forControlEvents:UIControlEvents.TouchUpInside)

            self.view.addSubview(btRight)

        }

            func upMove(sender: UIButton)// 调用向上移动的方法

            {

              var c = diamonds.frame

              if c.origin.y == 60

              {

                return

              }

              else

              {

                var newXY = CGRectMake(c.origin.x,c.origin.y - 10,c.size.width,c.size.height)

                diamonds.frame = newXY

              }

            }

            func downMove(sender: UIButton)// 调用向下移动的方法

            {

                var c = diamonds.frame

                if c.origin.y == 430

                {

                    return

                }

                else

                {

                    var newXY = CGRectMake(c.origin.x,c.origin.y + 10,c.size.width,c.size.height)

                    diamonds.frame = newXY

                }

            }

            func leftMove(sender: UIButton)// 调用向左移动的方法

            {

                var c = diamonds.frame

                if c.origin.x == 0

                {

                    return

                }

                else

                {

                    var newXY = CGRectMake(c.origin.x - 10,c.origin.y,c.size.width,c.size.height)

                    diamonds.frame = newXY

                }

            }

            func rightMove(sender: UIButton)// 调用向左移动的方法

            {

                var c = diamonds.frame

                if c.origin.x == 270

                {

                    return

                }

                else

                {

                    var newXY = CGRectMake(c.origin.x + 10,c.origin.y,c.size.width,c.size.height)

                    diamonds.frame = newXY

                }

            }

            

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

        


        override func didReceiveMemoryWarning() {

            super.didReceiveMemoryWarning()

            // Dispose of any resources that can be recreated.

        }



    }



  • 相关阅读:
    TypeError: Object(…) is not a function
    解决 OSError: [WinError 126] 找不到指定的模块
    LeetCode——和等于 k 的最长子数组长度
    LeetCode——判断子序列
    LeetCode——递增的三元子序列
    LeetCode——字符串相乘
    LeetCode——课程安排 IV
    LeetCode——最小移动次数使数组元素相等
    同源时钟、同相位时钟、同时钟域
    C++ 创建动态二维数组 使用vect<vec> 并初始化为0
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6477880.html
Copyright © 2011-2022 走看看