zoukankan      html  css  js  c++  java
  • swift学习之UIButton

    //

    //  ViewController.swift

    //  button

    //

    //  Created by su on 15/12/7.

    //  Copyright © 2015年 tian. All rights reserved.

    //

    import UIKit

    class ViewController: UIViewController {

        override func viewDidLoad() {

            super.viewDidLoad()

            //创建一个button

            let button:UIButton = UIButton(frame: CGRect(x: 110, y: 70, 100, height: 50))

            button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

            

            //设置不同状态的文字,依次是普通状态和按下状态

            button.setTitle("你点我呀!", forState: UIControlState.Normal)

            button.setTitle("你还真点啊", forState: UIControlState.Highlighted)

            //通过addTarget方法为按钮添加交互响应,依次为按下事件

            button.addTarget(self, action: "press:", forControlEvents: UIControlEvents.TouchUpInside)

            self.view.addSubview(button)

            

            //创建图形按钮

            let normalImage = UIImage(named: "btn1")

            let highLightedImage = UIImage(named: "btn2")

            let button2 = UIButton(frame: CGRect(x: 110, y: 200, 100, height: 30))

            

            button2.setImage(normalImage, forState: UIControlState.Normal)

            button2.setImage(highLightedImage, forState: UIControlState.Highlighted)

            self.view.addSubview(button2)

            

            //创建一个图片加文字的按钮

            

            let button3 = UIButton(frame: CGRect(x: 0, y: 250, 400, height: 30))

            button3.setImage(UIImage(named: "btn1"), forState:UIControlState.Normal)

            button3.titleLabel?.font = UIFont.systemFontOfSize(14)

            

            button3.imageView?.contentMode = UIViewContentMode.ScaleAspectFit

            button3.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            button3.setTitle("fsdfdsfsdfsadf", forState: UIControlState.Normal)

            self.view.addSubview(button3)

            

            //从系统定义的按钮类型常见button

            let btn4:UIButton = UIButton(type: UIButtonType.Custom)

            btn4.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            btn4.setTitle("12344444", forState: UIControlState.Normal)

            btn4.titleLabel!.font = UIFont.systemFontOfSize(14)

            btn4.frame = CGRect(x: 110, y: 300, 100, height: 100)

            self.view.addSubview(btn4)

            

            

            //创建部分圆角的按钮

            

            let btn5:UIButton = UIButton(type: UIButtonType.Custom)

            btn5.backgroundColor = UIColor.redColor()

            btn5.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            btn5.setTitle("345555", forState: UIControlState.Normal)

            btn5.titleLabel!.font = UIFont.systemFontOfSize(14)

            btn5.frame = CGRect(x: 110, y: 400, 100, height: 100)

            self.view.addSubview(btn5)

    //        UIRectCornerTopLeft     = 1 << 0,

    //        UIRectCornerTopRight    = 1 << 1,

    //        UIRectCornerBottomLeft  = 1 << 2,

    //        UIRectCornerBottomRight = 1 << 3,

    //        UIRectCornerAllCorners  = ~0UL

    //        let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: UIRectCorner.TopLeft|UIRectCorner.TopRight, cornerRadii: CGSize( 15, height: 15))

            let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: [UIRectCorner.TopLeft,UIRectCorner.TopRight], cornerRadii: CGSize( 15, height: 15))

            let shape:CAShapeLayer = CAShapeLayer()

            shape.path = beizer.CGPath

            btn5.layer.mask = shape

            

            //创建border按钮

            let btn9:UIButton = UIButton(frame: CGRect(x: 50, y: 500, 100, height: 35))

            btn9.backgroundColor = UIColor.whiteColor()

            btn9.setTitle("边框按钮", forState: UIControlState.Normal)

            btn9.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            btn9.layer.borderColor = UIColor.blackColor().CGColor

            btn9.layer.borderWidth = 1

            btn9.layer.cornerRadius = 5

            self.view.addSubview(btn9)

            

            

            

            

            

        }

        func press(sender:UIButton){

            print("你按下了按钮")

        }

        override func didReceiveMemoryWarning() {

            super.didReceiveMemoryWarning()

            // Dispose of any resources that can be recreated.

        }

    }

  • 相关阅读:
    不使用动态sql语句,正确书写case when中的null处理
    VC项目配置详解(转)
    JAXWS 访问SSL 的WebService 老是HTTP transport error: Connection refused错误的解决办法。
    [转]为什么开发人员工作10多年了还会迷茫?没有安全感?
    Tomcat 6.0.24 不兼容的APR版本问题
    WPF滚动条嵌套,响应鼠标滑轮事件的处理
    SqlServer无备份下误删数据恢复
    今天发现竟然有一个粉丝!!!
    好用的开源轻量级DHCP和DNS服务软件“Dual DHCP DNS Server”
    Windows下源码获取
  • 原文地址:https://www.cnblogs.com/tian-sun/p/5026396.html
Copyright © 2011-2022 走看看