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. 前台ajax验证技术采用正则表达式

    var flag1,flag2,flag3,flag4,flag5,flag6=false; function val(obj) { var regBox = { /* regEmail : /^([ ...

  2. iOS Core Animation具体解释(四)AutoLayout中的动画

    原创blog.转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK具体解释专栏 http://blog.csdn.net/column/details/huang ...

  3. selector-item属性

    今天做的项目,发现通过键盘选中一个button以后,并没有出现button样式的变化,后来发现是selector里面缺少状态配置的原因.这里就顺便把用到的属性记录了下来,方便以后使用.当然它的属性很多 ...

  4. es6 -- 透彻掌握Promise的使用,读这篇就够了

    Promise的重要性我认为我没有必要多讲,概括起来说就是必须得掌握,而且还要掌握透彻.这篇文章的开头,主要跟大家分析一下,为什么会有Promise出现. 在实际的使用当中,有非常多的应用场景我们不能 ...

  5. HDU 2564 饭卡

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  6. 【Django】ContentType组件

    目录 理解 表结构 使用 @ 好,现在我们有这样一个需求,我们的商城里有很多的商品,然而节日要来了,我们要搞活动. 那么,我们就要设计优惠券,优惠券都有什么类型呢?满减的.折扣的.立减的.等等等... ...

  7. day 5 集合

    # -*- coding: utf_8 _*_# Author:Vi#集合是无序的 list_1 = [1,2,3,2,3,5,7]list_1 = set(list_1)#将列表转变成集合list_ ...

  8. XMPP开发之从零開始

    对于server的搭建和设置.我在这里就不再多说了.有好多前辈已经帮大家攻克了.能够參考下这篇博客 XMPPserver配置 我依照这个博客配置好了,server后,然后在网上參照代码写了一个小的de ...

  9. system.setting-全局变量数据监听

    今天在setting里添加了一个新的变量,想要实现对这个变量的监听.现在记录下方法 首先就是明白一点,我们在system.setting里添加的变量,都会被保存在data/data/com.andro ...

  10. C#发送邮件DEMO

    虽然网上有很多类似的DEMO,但是还是整个封装好的例子,以便以后用: 发送邮箱是直接在web.config配置的. protected void Button1_Click(object sender ...