Swift语言之View,Button控件实现小方块在界面上的移动(纯代码实现)

SwiftUI中视图移动
本文介绍了一个使用SwiftUI创建的简单应用,该应用通过按钮控制屏幕上的方形视图进行上下左右移动。文章详细展示了如何定义视图及其初始位置,并为每个方向设置按钮以触发相应方向的移动。

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.

    }



}



转载于:https://www.cnblogs.com/Free-Thinker/p/6477880.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值