swift之应用程序管理
来源:互联网 发布:卸载软件找不到程序 编辑:程序博客网 时间:2024/05/29 04:16
1、效果图
2、控件
UIButton、UILabel、UIView、UIImageView
3、代码
import UIKitclass ViewController: UIViewController { ///---保存plist文件中的数组数据 var m_Array:NSArray!; ///--加载plist文件数据 func loadPlistInfo() { ///---获取路径 let path:String = NSBundle.mainBundle().pathForResource("game.plist", ofType: nil)!; m_Array = NSArray(contentsOfFile:path); } override func viewDidLoad() { super.viewDidLoad(); ///---每行应用的个数 let appCount:CGFloat = 3; ///---每个应用的宽和高 let appw:CGFloat = 80; let apph:CGFloat = 100; ///--第一行距离顶部的距离 let margintop:CGFloat = 90; ///---行之间的巨鹿 let marginx:CGFloat = (view.frame.size.width - appCount * appw) / (appCount + 1); ///---列之间的距离 let marginy:CGFloat = marginx + 70; loadPlistInfo(); ///--循环添加view for i in 0...Int(appCount) { ///---计算每个单元格所在列索引 let colIndex:Int = i % Int(appCount); ///---计算每个单元格所在行索引 let rowIndex = i / Int(appCount); let appx:CGFloat = CGFloat(marginx + CGFloat(colIndex) * (appw + marginx)); let appy:CGFloat = CGFloat(margintop + CGFloat(rowIndex) * (appw + marginy)); ///---创建每一个应用 let appview = UIView(frame: CGRectMake(appx, appy, appw,apph)); ///--设置每个应用的背景色 ///appview.backgroundColor = .None; ///---添加应用 view.addSubview(appview); ////---向每一个view中添加view,label,btn ///----1、添加view(图片框) let appViewIcon = UIImageView(); ///appViewIcon.backgroundColor = UIColor.yellowColor(); ///--设置frame let iconw = CGFloat(45); let iconh = CGFloat(50); let iconx = CGFloat(appview.frame.size.width - iconw) * 0.5; let icony = CGFloat(0); appViewIcon.frame = CGRectMake(iconx, icony, iconw, iconh); ///---设置图片数据 ///---获取当前应用的字典 let dic = m_Array[i]; let str:String = String(dic["icon"]); ///---字符串截取 var iconName:String!; var iconIndex_i = 0; for ch in str.characters { if(9 == iconIndex_i++) { iconName = String(ch); } } appViewIcon.image = UIImage(named:iconName); ///---在appView中显示添加的appviewicon appview.addSubview(appViewIcon); ///---2、添加label ///--计算frame let labelw = appview.frame.size.width; let labelh = CGFloat(20); let labelx = CGFloat(0); let labely = iconh; let label = UILabel(frame: CGRectMake(labelx, labely, labelw, labelh)); ///label.backgroundColor = UIColor.blueColor(); ///---label设置数据 let nameStr:String = String(dic["name"]); ///---字符串截取 var Name:String = ""; var nameindex_i = 0; for ch in nameStr.characters { nameindex_i++; if(10 == nameindex_i || 11 == nameindex_i) { Name += String(ch); } } label.text = Name; label.textAlignment = .Center; ///--显示label appview.addSubview(label); ///---添加按钮 ///---设置frame let btnw = iconw; let btnh = CGFloat(20); let btnx = iconx; let btny = labely + labelh; let btn = UIButton(frame: CGRectMake(btnx, btny, btnw, btnh)); ///btn.backgroundColor = UIColor.grayColor(); btn.setTitle("下载", forState: .Normal); btn.setTitleColor(UIColor.greenColor(), forState: .Normal); btn.setTitle("进入", forState: .Highlighted); btn.setTitleColor(UIColor.redColor(), forState: .Highlighted); ///---显示按钮 appview.addSubview(btn); ///---为按钮添加单击事件 btn.addTarget(self, action: "btnClick", forControlEvents:UIControlEvents.TouchUpInside); } } ///---按钮的单击事件 func btnClick() { let alert = UIAlertController(title: "提示", message: "下载中", preferredStyle: UIAlertControllerStyle.Alert); ///----创建按钮 let sureAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: {(action)-> Void in }); ///---添加按钮 alert.addAction(sureAction); ///---调用 self.presentViewController(alert, animated:true, completion:nil); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}
4、plist文件
1 0
- swift之应用程序管理
- Swift之文件管理
- The Swift Code之应用程序的启动过程
- Swift内存管理(ARC)之循环引用
- linux应用程序开发之Makefile文件的管理
- ASP.NET状态管理之八(应用程序Application)
- swift 第一个IOS应用程序
- swift 第一个IOS应用程序
- Swift编写Android应用程序入门
- Swift-内存管理
- swift 内存管理
- Swift中的内存管理
- Swift中的内存管理
- swift - 内存管理
- swift - 内存管理
- Swift OC 内存管理
- Swift--15内存管理
- Swift中的内存管理
- Java Collection
- 容器的emplace成员函数
- 技术团队负责人应该具备怎样的能力
- ArrayAdapter和SimpleAdapter详解
- iOS网络数据安全
- swift之应用程序管理
- 随笔
- c++实验1-求两个数的和差
- 堆栈与程序指针
- Objective-C学习笔记(四):OC之分类、类对象、description方法和SEL
- 杭电1237简单计算器
- android 发送长短信失败的原因
- 哈理工oj 1739 sort problem【思维、水题】
- FFmpeg给mp4视频加上旋转90度的代码