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. Visual Studio Set Project Environment Variables

    Visual Studio Set Project Environment Variables eryar@163.com In Visual Studio you can specify chang ...

  2. Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.3

    3.Spark MLlib Deep Learning Convolution Neural Network(深度学习-卷积神经网络)3.3 http://blog.csdn.net/sunbow0 ...

  3. jq---方法总结

    1. 什么是jQuery 在使用jQuery之前,我们必须先了解什么是jQuery,它能够干什么(不然我们为啥要用它). jQuery是一个非常流行的快速.小巧.功能强大的开源JavaScript库. ...

  4. 【Django】AJAX

    目录 JSON 序列化 stringify 反序列化 parse JSON与XML对比 AJAX简介 AJAX常见应用场景 jQuery实现AJAX JS实现AJAX AJAX请求设置csrf_tok ...

  5. gSOAP 使用WebServer心得

    关于正常怎么使用gSOAP的话,下面那篇博客已经讲得非常详细,我就不再赘述了 https://www.cnblogs.com/dengpeng1004/p/6165751.html 问题1: WCF ...

  6. 【hdu 3987】Harry Potter and the Forbidden Forest

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=3987 [Description] 给出一张有n个点的图,有的边又向,有的边无向,现在要你破坏一些路 ...

  7. Jenkins学习总结(2)——Jenkins+Maven进行Java项目持续集成

    最近配置了Jenkins服务器,记录下基本过程.(当然还遇到了若干小问题,兵来将挡水来土掩就是了) Jenkins安装 安装Tomcat 从Jenkins官网下载jenkins.war文件.官网地址: ...

  8. 【hoj】2160 bin packing 二分、贪心

    这个题是在二分的题单上的,可是依据二分法写出来的会在oj上超时.依据题目以下给出的提示能够发现能通过贪心法每次都找最能满足的情况去填充每个包,这样就能保证使用的包的数量是最少的 二分法解法: #inc ...

  9. 负载均衡器&http正向代理

    透明的负载均衡器&http正向代理 * master-workers架构,http正向代理由独立的dns请求以及缓冲进程  * 使用epoll(ET)模式,採用全异步方式(双缓存,实现双向同一 ...

  10. count 变量的使用

    count:统计出现的次数,当某种情况发生时,执行 +1 的动作,+1 的动作常置于循环体内.基本结构如下: count = 0 while count < vnum and **: ... c ...