2015年12月

图片浏览器

//
//  ViewController.swift
//  图片浏览器
//
//  Created by admin on 15/12/24.
//  Copyright © 2015年 jin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var preButton: UIButton!
    @IBOutlet weak var nextButton: UIButton!
    @IBOutlet weak var titleLable: UILabel!
    @IBOutlet weak var imageDescription: UILabel!
    //图片名称数组,
    lazy var imageArr:[NSDictionary] =
    {
        //获取 plist文件路径
        var filePath:String = (NSBundle.mainBundle().pathForResource("imageData", ofType: "plist"))!
        //从文件中读取配置信息
        var imageArr:NSArray! = NSArray(contentsOfFile: filePath)!
        //转换类型并返回
        return imageArr as! [NSDictionary]
    }()

    //当前显示图片的下标
    static var imageIndex = 0
    override func viewDidLoad() {
        super.viewDidLoad()

//        print(self.imageArr[0]["image"]!)
        updateImageView()
//        imageView.image = UIImage(imageLiteral: "2")
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //上一张图片
    @IBAction func preImage(sender: AnyObject) {
        ViewController.imageIndex--
        updateImageView()
    }
    //下一张图片
    @IBAction func nextImage(sender: AnyObject) {
        ViewController.imageIndex++
        updateImageView()
    }
    //更新页面
    func updateImageView()
    {
        //更新标题
        titleLable.text = "\(ViewController.imageIndex+1)/\(imageArr.count)"
        //设置上一页下一页按钮的状态
        preButton.enabled = ViewController.imageIndex != 0
        nextButton.enabled = ViewController.imageIndex < (imageArr.count - 1)
        //做个动画效果
        UIView.animateWithDuration(1, animations: {
            //
            if self.imageArr.count > ViewController.imageIndex
            {
                //设置 imageView 透明度,为了动画效果
                self.imageView.alpha = 0
                //设置 imageView 的图片
                self.imageView.image = UIImage(imageLiteral: self.imageArr[ViewController.imageIndex]["image"] as! String)
                //设置 当前图片的描述
                self.imageDescription.text = self.imageArr[ViewController.imageIndex]["description"] as? String
                self.imageView.alpha = 1
            }
        })
    }

}

按钮的基本使用以及控件的基本属性

//
//  ViewController.swift
//  按钮基本使用
//
//  Created by admin on 15/12/24.
//  Copyright © 2015年 jin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imageButton: UIButton!
    @IBOutlet var viewparent: UIView!
    @IBOutlet weak var lable: UILabel!
    @IBOutlet weak var viewA: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
//控件在相对于父控件左上角为坐标原点的位置坐标,类似 (32.0, 65.0, 139.0, 162.0),分别对应控件的 (横坐标,纵坐标,宽度,长度)
        print(lable.frame)
        //控件在相对于自己左上角为坐标原点的位置坐标,因为是相对于自己,所以横纵坐标为0,类似 (0.0, 0.0, 139.0, 162.0),分别对应控件的 (横坐标,纵坐标,宽度,长度)
        print(lable.bounds)
        //控件中点相对于父控件左上角为坐标原点的位置坐标,类似 (101.5, 146.0),分别对应控件的 (横坐标,纵坐标)
        print(lable.center)
        //控件的父控件,会是一个控件对象
        print(lable.superview)
        //控件的子对象,会是一个控件对象数组
        print(self.view.subviews)
        //控件的tag标示,父控件可以通过tag来找到对应的子控件
        print(lable.tag)
        //控件的旋转角度,比例缩放,平移等属性,类似 CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0)
        print(lable.transform)
        //删除自己
//        viewA.removeFromSuperview()
        //添加一个控件
        viewA.addSubview(lable)
        //查找子控件中的tag值为指定值的控件
        print(viewparent.viewWithTag(3))
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //向上
    @IBAction func imageTop(sender: AnyObject) {
        imageButton.center.y -= 1
    }
    //向下
    @IBAction func imageButtom(sender: AnyObject) {
        imageButton.center.y += 1
    }
    //向右
    @IBAction func imageRight(sender: AnyObject) {
        imageButton.center.x -= 1
    }
    //向左
    @IBAction func imageLeft(sender: AnyObject) {
        imageButton.center.x += 1
    }
    //移动 x轴移动,y轴移动yua
    static var moveInt:CGFloat = 20
    @IBAction func moveImage(sender: AnyObject) {
        //这里如果写一个定值的话只在第一次点击的时候生效,所以需要借助一个变量
//        imageButton.transform = CGAffineTransformMakeTranslation(ViewController.moveInt, ViewController.moveInt)
//        ViewController.moveInt += 20
        //这里的平移是以第一个参数的基准进行偏移,不要额外的变量
        imageButton.transform = CGAffineTransformTranslate(imageButton.transform, 20, 20)
    }
    @IBAction func imageScale(sender: AnyObject) {
        //当这个值大于1的时候是方法,小于1的时候是缩小
        var scale:CGFloat = 0
        if sender.titleLabel!!.text! == "放大"
        {
            scale = 2
        }
        else if sender.titleLabel!!.text! == "缩小"
        {
            scale = 0.5
        }
        imageButton.transform = CGAffineTransformScale(imageButton.transform, scale, scale)
    }
    //旋转
    @IBAction func imageRotate(sender: AnyObject) {
        //animateWithDuration是实现动画效果,第一个参数是动画持续时间,第二个参数是一个闭包函数
        UIView.animateWithDuration(2.0, animations: {
            //这里的旋转单位是以圆周率为单位,这里是四分之一圆周率(45度)
            self.imageButton.transform = CGAffineTransformRotate(self.imageButton.transform, CGFloat(M_PI_4))
        })


    }
}


ios键盘的弹出与收回

1.弹出
这里以UITextField为例,其它控件应该也是一样的
选中控件,在右侧的属性面板(show the attributes inspector)找到Keyboard Type设置成自己想要的键盘形式
2.收回

//只有呼出键盘的对象才能收回键盘,所以这里调用了两次
        textFieldA.resignFirstResponder()
        textFieldB.resignFirstResponder()
//上面的例子只有两个设置了弹出键盘的控件,要是有很多的话可以用下面的代码,一句搞定
        self.view.endEditing(true)

ios应用启动流程

程序启动过程如下:
UIApplicationMain 函数调用创建一个 UIApplication 对象及程序代理对象(本例为 AppDelegate)
UIApplication 对象扫描 Info.plist 文件,将其中 Mainstoryboard file base name 所指定的 Storyboard 文件装入(通常为:MainStoryboard.storyboard)
UIApplication 对象从程序代理对象中获取窗口对象UIWindow(或创建一个UIWindow 新实例并将其与程序代理对象相关联)
将Storyboard 文件中 initial view controller 属性所指定的UIViewController 实例化,并将它赋予为 UIWindow 的root view controller
向程序代理对象发送 application:didFinishLaunchingWithOptions: 消息,以便程序员做自己的初始化工作

UIViewController的实例化过程中初始化了View和View下的其它控件对象,

swift中控件和代码绑定

1.事件绑定
如果是手写的话,代码要遵照这种格式

@IBAction func buttonClick(sender:AnyObject)
{

}

直接在控件上面右键,在右键菜单中把要绑定的时间拖动到编写的函数上面,如果没有编写代码,直接拖动到类当中,填一下方法名,xcode就会自动创建方法的代码
2.把控件绑定到方法的变量上
在控件上面右键,在右键菜单中找到New Reference Outlet拖动到类中,填好变量名即可,会生成如下代码

@IBOutlet weak var button: UIButton!