//
// ViewController.swift
// 九宫格
//
// Created by admin on 15/12/26.
// Copyright © 2015年 jin. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
//存放通知消息的lable
lazy var lable:UILabel =
{
var lable = UILabel()
//设置宽度
lable.frame.size.width = 100
//设置高度
lable.frame.size.height = 20
//设置位置
lable.transform = CGAffineTransformMakeTranslation(UIScreen.mainScreen().bounds.size.width/2 - lable.frame.size.width/2, UIScreen.mainScreen().bounds.size.height/2)
//设置背景颜色
lable.backgroundColor = UIColor(red: 0.483992,green:0.638788,blue: 1, alpha: 1)
//设置文字
lable.text = "正在下载中........"
//设置圆角,button可以设置,lable没有效果啊
lable.layer.cornerRadius = 50
return lable
}()
//当前最后一个插入主视图在数组中的下标
var dataIndex:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
//获得图片信息
let data = NSArray.init(contentsOfFile: NSBundle.mainBundle().pathForResource("app", ofType: "plist")!)
//循环添加控件
for (var (index,blockInfo)) in EnumerateSequence(data!)
{
//获得图片
let image = UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource(String(blockInfo["icon"]!!), ofType: "png")!)
//获得图片描述
let descreption = String(blockInfo["name"]!!)
//设置最后插入视图的数据的在数据数组中的下标
self.dataIndex = index
//执行插入
addChildBlok(image!,description: descreption,dataIndex: self.dataIndex)
}
}
//UIImage好像没有直接缩放的办法,就用这个完成缩放
func scalImage(image:UIImage,scal:CGFloat)->UIImage
{
//创建 CGSize 对象记录下缩放改变比例之后的宽高,方便之后的使用
let imageSize = CGSize(width: CGFloat(image.size.width*scal),height: CGFloat(image.size.height*scal))
//创建一个 bitmap 大小的 context
UIGraphicsBeginImageContext(imageSize)
//绘制改变大小的图片
image.drawInRect(CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height))
//从当前context中创建一个改变大小后的图片
let imageTest = UIGraphicsGetImageFromCurrentImageContext()
//使当前 context 出栈
UIGraphicsEndImageContext()
return imageTest
}
func addChildBlok(image:UIImage,description:String,dataIndex:Int)
{
//列数是计算而成的,初始化是散列
var column = 3
//这里可以通过屏幕的宽度来选择显示多少条
if UIScreen.mainScreen().bounds.size.width > 0
{
column = 4
}
//获得屏幕当前 view 的左边距
var marginLeft = Int(UIScreen.mainScreen().bounds.size.width * 0.05)
//上边距
let marginTop = 20
//获得 view 的宽度
let blockViewWidth = Int(UIScreen.mainScreen().bounds.size.width * 0.8 / CGFloat(column))
//重新设置左边距,以达到居中的效果
marginLeft = (Int(UIScreen.mainScreen().bounds.size.width) - column * blockViewWidth) / 2
//创建 view
let blockView = UIView(frame: CGRect(x: 0,y: 0,width: 0,height: 0))
//设置blockview颜色,用以调试
//blockView.backgroundColor = UIColor(red:0.483992,green:0.638788,blue: 1,alpha: 1)
self.view.addSubview(blockView)
//获得图片信息
var image = image
let descreption = description
//缩放图片
image = scalImage(image, scal: (CGFloat(blockViewWidth) * 0.8)/image.size.width)
//创建 UIImageView 承载图片
let imageView = UIImageView(image: image)
//改变UIImageView位置
let imageMarginLeft = CGFloat((Double(blockViewWidth) * 0.1))
let imageMarginTop = CGFloat((Double(blockViewWidth) * 0.1))
imageView.transform = CGAffineTransformTranslate(imageView.transform, imageMarginLeft, imageMarginTop)
//加入当前view中
blockView.addSubview(imageView)
//加入 UIlable ,并设定好坐标和大小
let lableMarginLeft = imageMarginLeft
let lableMarginTop = imageMarginTop + imageView.frame.height
let lable = UILabel(frame: CGRect(x: lableMarginLeft,y: lableMarginTop,width: CGFloat((Double(blockViewWidth) * 0.8)),height: 10))
//像这样直接创建一个,设置字体没有效果
//lable.font = UIFont(name: "System", size: CGFloat(10))
//改变字体的大小
lable.font = lable.font.fontWithSize(8)
//改变字体对齐方式
lable.textAlignment = NSTextAlignment.Center
//设置lable显示的文字
lable.text = descreption
blockView.addSubview(lable)
//加入button并设定好位置信息
let buttonMarginLeft = imageMarginLeft
let buttonMarginTop = imageMarginTop + lable.frame.height + imageView.frame.height
let button = UIButton(frame: CGRect(x: buttonMarginLeft,y: buttonMarginTop,width: CGFloat((Double(blockViewWidth) * 0.8)),height: 10))
//设置背景图片
button.setBackgroundImage(UIImage(imageLiteral: "buttongreen"), forState: UIControlState.Normal)
button.setBackgroundImage(UIImage(imageLiteral: "buttongreen_highlighted"), forState: UIControlState.Highlighted)
//设置title
button.setTitle("点击下载", forState: UIControlState.Normal)
button.setTitle("正在下载", forState: UIControlState.Highlighted)
//设置字体大小
button.titleLabel?.font = button.titleLabel?.font.fontWithSize(7)
//绑定事件
button.addTarget(self, action: "showInfo:", forControlEvents: UIControlEvents.TouchDown)
//设定当前view的坐标和大小
let blockViewHeight = imageMarginTop + imageView.frame.height + lable.frame.height + button.frame.height
let blockViewX = (dataIndex%column) * blockViewWidth + marginLeft
let blovkViewY = (dataIndex/column) * Int(blockViewHeight) + marginTop
blockView.frame = CGRectMake(CGFloat(blockViewX), CGFloat(blovkViewY), CGFloat(blockViewWidth), CGFloat(blockViewHeight))
blockView.addSubview(button)
// self.view.bringSubviewToFront(button)
//出现一次情况,按钮点击之后没有任何反应,搞了半天,后来想到用这个办法调试,发现原来是button的位置不在父控件的view之内
// blockView.backgroundColor = UIColor(red:0.483992,green:0.638788,blue: 1,alpha: 1)
// imageView.backgroundColor = UIColor(red:0.12323,green:0.123123,blue: 1,alpha: 1)
}
//按钮点击事件,显示一个lable
func showInfo(sender:UIButton)
{
//改变当前点击按钮的图片和文字,禁用点击
sender.setBackgroundImage(UIImage(imageLiteral: "buttongreen_highlighted"), forState: UIControlState.Normal)
sender.setTitle("正在下载", forState: UIControlState.Normal)
sender.enabled = false
//显示lable按钮
self.view.addSubview(self.lable)
//延时执行函数
self.performSelector("hideInfo:", withObject: sender, afterDelay: 1)
}
//执行完毕,隐藏lable
func hideInfo(button:AnyObject)
{
//设置lable透明度
UIView.animateWithDuration(3, animations: {
self.lable.alpha = 0
})
//延时执行函数,初始化执行完毕的信息
self.performSelector("endDownload:", withObject: button, afterDelay: 3)
}
//进行下载完成之后的初始化
func endDownload(button:UIButton)
{
//设置当前按钮的图片和文字,禁用点击
button.setBackgroundImage(UIImage(imageLiteral: "buttongreen"), forState: UIControlState.Normal)
button.setBackgroundImage(UIImage(imageLiteral: "buttongreen_highlighted"), forState: UIControlState.Highlighted)
button.setTitle("下载完成", forState: UIControlState.Normal)
button.enabled = false
//设置lable透明度
self.lable.alpha = 1
//移除lable
self.lable.removeFromSuperview()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}