swift之应用程序管理

前端之家收集整理的这篇文章主要介绍了swift之应用程序管理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、效果


2、控件

UIButton、UILabel、UIView、UIImageView

3、代码

import UIKit

class 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文件

原文链接:https://www.f2er.com/swift/324468.html

猜你在找的Swift相关文章