• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift,demo,ios8

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

swift交流群:342581988,欢迎增加。

刚刚写的小 demo。搞得还是不是太好。请大家拍砖!

能够直接复制执行

import UIKit

class ViewController: UIViewController {
    var l : UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let labelRect = CGRectMake(140,75,50,50)
        var label = UILabel(frame : labelRect)
        label.backgroundColor = (UIColor.cyanColor())
        self.view.addSubview(label)
        l = label
        
        
        var rect = CGRectMake(150,340,50,50)
        var upButton = UIButton(frame: rect)
        upButton.setTitle("up", forState: UIControlState.Normal)
        upButton.setTitleColor(UIColor.blueColor(),forState: UIControlState.Normal)
        upButton.tag = 1
        upButton.setTitleColor(UIColor.blackColor(),forState: UIControlState.Highlighted)
        self.view.addSubview(upButton)
        upButton.addTarget(self,action:"moveClick:",forControlEvents:UIControlEvents.TouchUpInside);
        
        rect = CGRectMake(150,420,50,50)
        var downButton = UIButton(frame: rect)
        downButton.setTitle("down", forState: UIControlState.Normal)
        downButton.setTitleColor(UIColor.blueColor(),forState: UIControlState.Normal)
        downButton.tag = 2
        downButton.setTitleColor(UIColor.blackColor(),forState: UIControlState.Highlighted)
        self.view.addSubview(downButton)
        downButton.addTarget(self,action:"moveClick:",forControlEvents:UIControlEvents.TouchUpInside)
        
        rect = CGRectMake(80,380,50,50)
        var leftButton = UIButton(frame: rect)
        leftButton.setTitle("left", forState: UIControlState.Normal)
        leftButton.setTitleColor(UIColor.blueColor(),forState: UIControlState.Normal)
        leftButton.tag = 3
        leftButton.setTitleColor(UIColor.blackColor(),forState: UIControlState.Highlighted)
        self.view.addSubview(leftButton)
        leftButton.addTarget(self,action:"moveClick:",forControlEvents:UIControlEvents.TouchUpInside)
        
        rect = CGRectMake(220,380,50,50)
        var rigthButton = UIButton(frame: rect)
        rigthButton.setTitle("right", forState: UIControlState.Normal)
        rigthButton.setTitleColor(UIColor.blueColor(),forState: UIControlState.Normal)
        rigthButton.tag = 4
        rigthButton.setTitleColor(UIColor.blackColor(),forState: UIControlState.Highlighted)
        self.view.addSubview(rigthButton)
        rigthButton.addTarget(self,action:"moveClick:",forControlEvents:UIControlEvents.TouchUpInside)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func moveClick(sender : UIButton!){

        var centerPoint = l.center
        
        if sender.tag == 1{
            var newPoint = CGPointMake(centerPoint.x, centerPoint.y-1)
            l.center = newPoint
        }else if sender.tag == 2{
            var newPoint = CGPointMake(centerPoint.x, centerPoint.y+1)
            l.center = newPoint
        }else if sender.tag == 3{
            var newPoint = CGPointMake(centerPoint.x-1, centerPoint.y)
            l.center = newPoint
        }else if sender.tag == 4{
            var newPoint = CGPointMake(centerPoint.x+1, centerPoint.y)
            l.center = newPoint
        }
        
    }

}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift学习笔记(变量、数组和字典)发布时间:2022-07-13
下一篇:
UIWebView开发中,js与oc,js与swift交互,相互传递参数的方法发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap