swift UIButton的使用

2024-05-31 06:32
文章标签 使用 swift uibutton

本文主要是介绍swift UIButton的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

代码

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  AppDelegate.swift  
  3. //  UIButtonDemo  
  4. //  
  5. //  Created by 赵超 on 14-6-18.  
  6. //  Copyright (c) 2014年 赵超. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11. @UIApplicationMain  
  12. class AppDelegate: UIResponder, UIApplicationDelegate {  
  13.                               
  14.     var window: UIWindow?  
  15.     var btn:UIButton!  
  16.     //响应事件  
  17.     func buttonOnClick() {  
  18.         println("点击了铵键")  
  19.        // self.btn.backgroundColor=UIColor.greenColor()  
  20.     }  
  21.   
  22.     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {  
  23.         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)  
  24.         // Override point for customization after application launch.  
  25.         self.window!.backgroundColor = UIColor.whiteColor()  
  26.         self.window!.makeKeyAndVisible()  
  27.         var obj = UIButton.buttonWithType(UIButtonType.Custom)  
  28.         self.btn = obj as UIButton  
  29.         self.btn.frame = CGRect(x:50, y:100, width:200, height:100)  
  30.         self.btn.setTitle("点我",forState:.Normal)  
  31.         self.btn.setTitle("按键被点击",forState:.Highlighted)  
  32.         self.btn.setTitleColor(UIColor.greenColor(),forState:.Normal)  
  33.         self.btn.setTitleColor(UIColor.blueColor(),forState:.Highlighted)  
  34.         self.btn.backgroundColor=UIColor.grayColor()  
  35.         self.btn.adjustsImageWhenHighlighted=true  
  36.         //添加背影图片  
  37.         self.btn.setBackgroundImage(UIImage(named:"button.png"),forState:UIControlState.Normal)  
  38.         self.btn.setBackgroundImage(UIImage(named:"button1.png"),forState:UIControlState.Highlighted)  
  39.           
  40.         //添加事件  
  41.         btn.addTarget(self, action: Selector("buttonOnClick"), forControlEvents: UIControlEvents.TouchUpInside)  
  42.    
  43.           
  44.         self.window!.addSubview(btn)  
  45.         return true  
  46.     }  
  47.   
  48.     func applicationWillResignActive(application: UIApplication) {  
  49.         // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.  
  50.         // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.  
  51.     }  
  52.   
  53.     func applicationDidEnterBackground(application: UIApplication) {  
  54.         // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.  
  55.         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.  
  56.     }  
  57.   
  58.     func applicationWillEnterForeground(application: UIApplication) {  
  59.         // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.  
  60.     }  
  61.   
  62.     func applicationDidBecomeActive(application: UIApplication) {  
  63.         // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.  
  64.     }  
  65.   
  66.     func applicationWillTerminate(application: UIApplication) {  
  67.         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.  
  68.     }  
  69.   
  70.   
  71. }  

效果

这篇关于swift UIButton的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1017505

相关文章

windows和Linux安装Jmeter与简单使用方式

《windows和Linux安装Jmeter与简单使用方式》:本文主要介绍windows和Linux安装Jmeter与简单使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows和linux安装Jmeter与简单使用一、下载安装包二、JDK安装1.windows设

Spring 缓存在项目中的使用详解

《Spring缓存在项目中的使用详解》Spring缓存机制,Cache接口为缓存的组件规范定义,包扩缓存的各种操作(添加缓存、删除缓存、修改缓存等),本文给大家介绍Spring缓存在项目中的使用... 目录1.Spring 缓存机制介绍2.Spring 缓存用到的概念Ⅰ.两个接口Ⅱ.三个注解(方法层次)Ⅲ.

PyTorch中cdist和sum函数使用示例详解

《PyTorch中cdist和sum函数使用示例详解》torch.cdist是PyTorch中用于计算**两个张量之间的成对距离(pairwisedistance)**的函数,常用于点云处理、图神经网... 目录基本语法输出示例1. 简单的 2D 欧几里得距离2. 批量形式(3D Tensor)3. 使用不

C#使用MQTTnet实现服务端与客户端的通讯的示例

《C#使用MQTTnet实现服务端与客户端的通讯的示例》本文主要介绍了C#使用MQTTnet实现服务端与客户端的通讯的示例,包括协议特性、连接管理、QoS机制和安全策略,具有一定的参考价值,感兴趣的可... 目录一、MQTT 协议简介二、MQTT 协议核心特性三、MQTTNET 库的核心功能四、服务端(BR

使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案

《使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案》在SpringBoot应用中,我们经常使用​​@Cacheable​​注解来缓存数据,以提高应用的性能... 目录@Cacheable注解Redis时,Redis宕机或其他原因连不上,继续调用原方法的解决方案1

java中XML的使用全过程

《java中XML的使用全过程》:本文主要介绍java中XML的使用全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录什么是XML特点XML作用XML的编写语法基本语法特殊字符编写约束XML的书写格式DTD文档schema文档解析XML的方法​​DOM解析XM

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

使用Nginx配置文件服务器方式

《使用Nginx配置文件服务器方式》:本文主要介绍使用Nginx配置文件服务器方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 为什么选择 Nginx 作为文件服务器?2. 环境准备3. 配置 Nginx 文件服务器4. 将文件放入服务器目录5. 启动 N

使用nohup和--remove-source-files在后台运行rsync并记录日志方式

《使用nohup和--remove-source-files在后台运行rsync并记录日志方式》:本文主要介绍使用nohup和--remove-source-files在后台运行rsync并记录日... 目录一、什么是 --remove-source-files?二、示例命令三、命令详解1. nohup2.

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言