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. 搭建个人博客 方式2 使用jekyll

    孙广东  2016.3.12 环境安装:1.通过 RailsInstaller  来安装 Ruby https://www.ruby-lang.org/zh_cn/documentation/inst ...

  2. 第一天,Mysql安装,DDL(数据库定义语言),DBA,DML(数据库操纵语言),导入外面的sql文件

    把“D:\mysql-5.6.22-winx64\bin”添加到系统环境变量path中了,然后在任意目录可访问mysql等命令,这样如登录等操作就不需要进入MySQL安装目录才好执行! MySQL下载 ...

  3. 一些优秀的学习网站(Android)

    突然发现自己学习没有总结,从今天开始会持续更新此博文,总结自己的学习情况,也便于自己时常查阅.官方文档就列举了,因为那是必读资料. 一.GitHub部分 1.我的github仓库地址 收藏了我常看的开 ...

  4. 关于router-link的传参以及参数的传递

    1.路径:http://localhost:8081/#/test?name=1 <router-link :to="{path:'/test',query: {name: id}}& ...

  5. C/C++(指针数组)

    指针数组 指针数组的本质是数组,数组指针的本质是指针 一个数组中的各个元素都是字符指针,即为字符指针数组,或者指针数组. int arr[] = {1,2,23,45,6};//整形数组 char c ...

  6. golang语言入门及安装

    golang语言入门及安装 go语言是google在2009年发布的开源编程语言使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全.支持并行进程. 本次讲解在windows上安装go语言的开 ...

  7. 多类别分类问题由 confusion matrix 到分类准确率(accuracy)的计算

    conf_mat = confusionmat(y_true, y_pred); % 首先根据数据集上的真实 label 值,和训练算法给出的预测 label 值, % 计算 confusion ma ...

  8. 18.Node.js 事件循环

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node ...

  9. sql查看依赖关系

    select OBJECT_NAME(object_id) as name,object_NAME(referenced_major_id) as ref from sys.sql_dependenc ...

  10. 企业网管软件实战之看视频学装Cisco Works 2000

    650) this.width=650;" border="0" alt="125711349.jpg" src="http://img1. ...