• 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.

        }



    }



  • 相关阅读:
    计算函数执行时间
    Go语言生成随机数
    413 Request Entity Too Large
    JavaScript变量与数据类型详解
    OAuth2.0认证详解
    prompt的工作原理
    JS 实现上传图片
    移动端的长按事件的实现
    实现自动关闭当前页面的效果
    前端异常捕获与上报
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6477880.html
Copyright © 2020-2023  润新知