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.

    }



}



posted @   brave-sailor  阅读(485)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2016-02-28 Android 4.2蓝牙介绍
2015-02-28 Attempt to call getDuration without a valid mediaplayer
2015-02-28 Mac环境下svn的使用
2015-02-28 MediaController
2014-02-28 Android进阶2之APK方式换肤
2014-02-28 Android APK方式换肤实现原理
点击右上角即可分享
微信分享提示