swift UIButton的使用

来源:互联网 发布:nginx 修改root路径 编辑:程序博客网 时间:2024/04/28 18:59

代码

[objc] view plaincopy在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. }  

效果

0 0
原创粉丝点击