标签:class tar color get width set
import UIKit
class ViewController: UIViewController {
var greenView:UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//创建四个system类型的按钮,分别用来实现视图的向左,向右,向上,向下的移动
//创建让视图左移的system类型的按钮
var leftbutton: UIButton = UIButton.buttonWithType(UIButtonType.System)as UIButton
//设置按钮大小和位置
leftbutton.frame = CGRect(x:60,y:200,width:30,height:30)
//设置标题
leftbutton.setTitle ("←", forState: UIControlState.Normal)
leftbutton.setTitle ("左", forState: UIControlState.Highlighted)
//添加动作
leftbutton.addTarget(self,action:"didClicked1:",forControlEvents:UIControlEvents.TouchUpInside)
//显示按钮
self.view.addSubview(leftbutton)
//创建让视图右移的system类型的按钮
var rightbutton: UIButton = UIButton.buttonWithType(UIButtonType.System)as UIButton
//设置按钮大小和位置
rightbutton.frame = CGRect(x:100,y:200,width:30,height:30)
//设置标题
rightbutton.setTitle ("→", forState: UIControlState.Normal)
rightbutton.setTitle ("右", forState: UIControlState.Highlighted)
//添加动作
rightbutton.addTarget(self,action:"didClicked2:",forControlEvents:UIControlEvents.TouchUpInside)
//显示按钮
self.view.addSubview(rightbutton)
//创建让视图上移的system类型的按钮
var upbutton: UIButton = UIButton.buttonWithType(UIButtonType.System)as UIButton
//设置按钮大小和位置
upbutton.frame = CGRect(x:140,y:200,width:30,height:30)
//设置标题
upbutton.setTitle ("↑", forState: UIControlState.Normal)
upbutton.setTitle ("上", forState: UIControlState.Highlighted)
//添加动作
upbutton.addTarget(self,action:"didClicked3:",forControlEvents:UIControlEvents.TouchUpInside)
//显示按钮
self.view.addSubview(upbutton)
//创建让视图下移的system类型的按钮
var downbutton: UIButton = UIButton.buttonWithType(UIButtonType.System)as UIButton
//设置按钮大小和位置
downbutton.frame = CGRect(x:180,y:200,width:30,height:30)
//设置标题
downbutton.setTitle ("↓", forState: UIControlState.Normal)
downbutton.setTitle ("下", forState: UIControlState.Highlighted)
//添加动作
downbutton.addTarget(self,action:"didClicked4:",forControlEvents:UIControlEvents.TouchUpInside)
//显示按钮
self.view.addSubview(downbutton)
//创建一个视图
var f1 = CGRect(x:50,y:50,width:70,height:100)
greenView = UIView(frame:f1)
//定义视图的背景色为绿色
greenView.backgroundColor = UIColor.greenColor()
//显示视图
self.view.addSubview(greenView)
}
//创建方法实现视图的移动
func didClicked1(sender: UIButton) {
var c = greenView.frame
if c.origin.x == 0
{
return
}
else
{
var newFrame = CGRect(x: c.origin.x - 10,y: c.origin.y,width:c.size.width,height:c.size.height)
greenView.frame = newFrame
}
}
func didClicked2(sender: UIButton) {
var c = greenView.frame
if c.origin.x == 250
{
return
}
else
{
var newFrame = CGRect(x: c.origin.x + 10,y: c.origin.y,width:c.size.width,height:c.size.height)
greenView.frame = newFrame
}
}
func didClicked3(sender: UIButton) {
var c = greenView.frame
if c.origin.y == 0
{
return
}
else
{
var newFrame = CGRect(x: c.origin.x,y: c.origin.y - 10,width:c.size.width,height:c.size.height)
greenView.frame = newFrame
}
}
func didClicked4(sender: UIButton) {
var c = greenView.frame
if c.origin.y == 380
{
return
}
else
{
var newFrame = CGRect(x: c.origin.x,y: c.origin.y + 10,width:c.size.width,height:c.size.height)
greenView.frame = newFrame
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
标签:class tar color get width set
原文地址:http://www.cnblogs.com/changningios/p/3787636.html