九宫格的修改

之前的九宫格的控件都是用代码加的,这里改用xib,http://jinblog.com/archives/285.html

1.新建xib,这个直接在在项目里面新建一个User Interface->Empty就好了
2.拖控件
3.设置好xib的Custom Class
4.设置好控件和Custom Class类的关联,Custom Class代码如下

//
//  AppBlockView.swift
//  九宫格
//
//  Created by admin on 15/12/28.
//  Copyright © 2015年 jin. All rights reserved.
//

import UIKit

class AppBlockView: UIView {


    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var lable: UILabel!
    //类方法,用来方便的实例话xib
    class func instance()->UIView
    {
        //加载xib的方法
        return NSBundle.mainBundle().loadNibNamed("AppBlockView", owner: nil, options: nil).last as! AppBlockView
    }
}

4.使用,这里碰到一个问题,xib里面的imageview没有办法去修改大小,修改成功了,但是运行效果确还是那么大的

//加载xib
var blockView = AppBlockView.instance() as! AppBlockView
//xib里面有个 imageview ,设置下图片
blockView.imageView.image = UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource("icon_00", ofType: "png")!)!
//输出宽肚,输出结果是: 214.0
print(blockView.imageView.frame.width)
//设置 fram
blockView.imageView.frame = CGRectMake(100,100, 10, 10)
blockView.imageView.backgroundColor = UIColor.blackColor()
//输出宽 输出结果是  10.0,设置输出的结果就是修改之后的结果,但是运行的话,看上去还是那么大
print(blockView.imageView.frame.width)
blockView.button.addTarget(self, action: "showInfo:", forControlEvents: UIControlEvents.TouchDown)

动画函数的用法

UIView.animateWithDuration(3, animations: {
    self.lable.alpha = 0
}, completion:
{
     (status) in
     //设置当前按钮的图片和文字,禁用点击
     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()
     //允许用户和页面交互,这个值在点击事件发生之后会被设置成`false`,用户就不能点击界面的任何按钮了,所以这里需要复原
     self.view.userInteractionEnabled = true
})

标签: swift, ios控件

添加新评论