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 ==

{

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 ==

{

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 ==

{

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 ==

{

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.

}

}

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

  1. [原创]在Framelayout中放置button控件出现的覆盖问题

    android Framelayout(帧布局)是很常用的布局,主要用来处理需要多个view叠加显示的情况. 然而在使用中,我发现Framelayout中的Button控件,会挡住所有其他控件,而不论 ...

  2. listView中的button控件获取item的索引

    在listview中的listitem设置事件响应,如果listitem中有button控件,这时候listitem就不会捕获到点击事件,而默认的是listitem中的button会捕获点击事件.那么 ...

  3. 【Android开发日记】之入门篇(十四)——Button控件+自定义Button控件

        好久不见,又是一个新的学期开始了,为什么我感觉好惆怅啊!这一周也发生了不少事情,节假日放了三天的假(好久没有这么悠闲过了),实习公司那边被组长半强制性的要求去解决一个后台登陆的问题,结果就是把 ...

  4. Xamarin XAML语言教程构建ControlTemplate控件模板 (四)

    Xamarin XAML语言教程构建ControlTemplate控件模板 (四) 2.在页面级别中构建控件模板 如果开发者要在页面级别中构建控件模板,首先必须将ResourceDictionary添 ...

  5. Xamarin XAML语言教程构建ControlTemplate控件模板 (二)

    Xamarin XAML语言教程构建ControlTemplate控件模板 (二) (2)打开MainPage.xaml文件,编写代码,将构建的控件模板应用于ContentView中.代码如下: &l ...

  6. asp.net动态生成按钮Button控件

    1.动态生成button控件及响应服务端和客户端事件 void BindButtons(){ foreach (var item in items) { Button Btn = new Button ...

  7. WPF--Blend制作Button控件模板--问题补充

    补充记录Button控件模板 控件模板制作过程中出现下图问题:动画对象不能用于动画属性"Fill” 并且这类问题Blend4中包括VS2010中仍然可以运行,但是只有VS2010中会报错:如 ...

  8. WPF--Blend制作Button控件模板

    博客园新人,WPF初学者.不涉及理论知识,直接进入操作. 记录一下使用Blend制作Button控件模板过程中,学到Blend几个知识点: 1.渐变画笔编辑器的Alpha选项可以调控件的透明度.即下图 ...

  9. C# Windows - Button 控件

    .Net Framework提供了一个派生于Control的类System.Windows.Forms.ButtonBase,它实现了Button控件所需的基本功能. System.Windows.F ...

随机推荐

  1. hdu1533Going Home KM算法

    //给一个n*m的图, //m表示人,h表示房子 //问全部人走回家的最小步数 //每一个人仅仅能进一间房 //非常明显的最大带权匹配 //每一个人到每每间房的距离即为权值 //因为是求最小,仅仅要改 ...

  2. 【Swift初见】Swift数组(二)

    在苹果的开发文档中对Array还提供了其它的操作算法: 1.Sort函数: 对数组进行排序.依据指定的排序规则,看以下的代码: var array = [2, 3, 4, 5] array.sort{ ...

  3. 深入解析开源项目之Universal-Image-Loader(二)内存---缓存篇

    珍惜作者劳动成果,如需转载,请注明出处. http://blog.csdn.net/zhengzechuan91/article/details/50292871 Universal-Image-Lo ...

  4. Raid阵列之简单介绍

    1.raid分类 软raid:用软件模拟raid芯片 硬raid:集成的后来添加的 2.raid基本简介 (1)raid是由廉价磁盘冗余阵列发展成为独立磁盘冗余阵列 (2)linux是借助MD(Mui ...

  5. 基于Linux平台的Lotus Domino 8系统部署五部曲(全视频展示)

    基于Linux平台的Lotus Domino 8系统部署五部曲(全视频展示),学习就像看电影 第一部:安装部署 第二部:配置Domino 第三部:Notes8客户端配置 第四部:为Domino系统加装 ...

  6. Codefroces B. New Skateboard

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. type---显示指定命令的类型

    type命令用来显示指定命令的类型,判断给出的指令是内部指令还是外部指令. 命令类型: alias:别名. keyword:关键字,Shell保留字. function:函数,Shell函数. bui ...

  8. Python批量重命名指定目录下文件的两种方法

    #法一 import os path = "C://Python34//" for file in os.listdir(path): if os.path.isfile(os.p ...

  9. shiro session管理

    http://shiro.apache.org/session-management.html Using Sessions The SessionManager Session Timeout Pe ...

  10. iPad之Linux平台实践

    updata.... 本文出自 "李晨光原创技术博客" 博客,谢绝转载!