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. /dev/shm和swap差别与联系

    1.基本理论 /dev/shm这个文件是寄生虫,寄存在内存中 swap是暂时在硬盘中划分一个区域,把它作为内存使用 2.怎样查看 使用df -lh能够查看/dev/shm 使用free -m能够查看s ...

  2. Onvif开发之服务端发现篇

    服务端的开发相对来说比客户端稍微难一点,也就是给填充相关结构体的时候,需要一点一点的去查阅,验证各个结构中各个成员各自代表什么意思,以及对应的功能需要是那个接口实现,这是开发服务端最头疼的事情.(在开 ...

  3. DbSet<>.Find()

    第一篇为大家带来新的API,DbSet<>.Find(). 过去我们常常用Where或First(FirstOrDefault)方法来查找对应的实体,比如: var people = fr ...

  4. 修改url中某个参数

    function changeURLArg(url,arg,arg_val){ var pattern=arg+'=([^&]*)'; var replaceText=arg+'='+arg_ ...

  5. 小米开源文件管理器MiCodeFileExplorer-源码研究(1)-2个模型Model

    上篇说到,把小米的Java代码整理成了5个包,其中1个是net.micode.fileexplorer.model.这个包就2个模型类,最基本了,FileInfo和FavoriteItem. pack ...

  6. [Angular & Unit Testing] Testing Component with Store

    When using Ngrx, we need to know how to test the component which has Router injected. Component: imp ...

  7. Css3 过渡(Transition)特效回调函数

    Css3 出来之后,能够说是替代了Flash,通过使用Html5和Css3的完美结合.就能够做出不论什么你想得到的特效,这里不再阐述... 近期在做一个喝水签到的功能.在想签到成功之后,签到框能够模拟 ...

  8. IPv6地址表示方法详解

    IPv6是互联网协议的第六版:最初它在IETF的 IPng选取过程中胜出时称为互联网新一代网际协议(IPng),IPv6是被正式广泛使用的第二版互联网协议. 现有标准IPv4只支持大概40亿(4×10 ...

  9. php全局变量的使用

    php全局变量的使用 一.总结 1.php的全局变量:php的全局变量和C++,Java的全局变量不一样 2.页面嵌套php:我在html页面中嵌套php代码的时候,php的全局变量好像和C++,Ja ...

  10. java创建节点和单向链表

    package datastructure; public class Node { private Object data; private Node next; public Node() { t ...