标签 ios控件 下的文章

swift版本的汤姆猫

//
//  ViewController.swift
//  汤姆猫
//
//  Created by admin on 15/12/25.
//  Copyright © 2015年 jin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    //定义 UIImageView 变量
    var imageView:UIImageView = UIImageView()
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置imageView坐标,尺寸
        imageView.frame = CGRect(origin: CGPoint(x: 20, y: 20), size: CGSize(width: 280, height: 450))
        //设置imageView默认图片
        imageView.image = UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource("cymbal_00", ofType: "jpg")!)!
        //把imageView添加进主视图
        self.view.addSubview(self.imageView)
        addButton("cymbal",actionName: "cymbal",x:50,y:400)
        addButton("pie",actionName: "pie",x:100,y:400)
        // Do any additional setup after loading the view, typically from a nib.
    }
    //添加按钮方法
    func addButton(backgroundImage:String,actionName:String,x:CGFloat,y:CGFloat)
    {
        //添加按钮
        let cymbalButton = UIButton()
        let buttonBackgroundImage = UIImage(imageLiteral: backgroundImage)
        //设置按钮背景图片
        cymbalButton.setBackgroundImage(buttonBackgroundImage, forState: UIControlState())
        //设置按钮位置坐标和宽度高度
        cymbalButton.frame = CGRect(origin: CGPoint(x: x, y: y), size: CGSize(width: buttonBackgroundImage.size.width, height: buttonBackgroundImage.size.height))
        //绑定事件方法,带参数的方法要加上冒号
        cymbalButton.addTarget(self, action: Selector("\(actionName):"), forControlEvents: UIControlEvents.TouchDown)
        //添加进主视图
        self.view.addSubview(cymbalButton)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func cymbal(sender: AnyObject) {
        //执行动画
        doAnimatingImage(self.getAnimatingImage("cymbal_",startNumber:0,endNumber:12))
    }
    @IBAction func pie(sender: AnyObject) {
        doAnimatingImage(self.getAnimatingImage("pie_",startNumber:0,endNumber:23))
    }
    //获得图片资源
    func getAnimatingImage(imagePrefix:String,startNumber:Int,endNumber:Int)->[UIImage]
    {
        var imageArr:[UIImage] = []
        for var i = startNumber;i <= endNumber;i++
        {
            //不足两位用0补齐
            let imagePath = String(format: "%02i", i)
            //使用这个方式(imageLiteral)创建的图片会缓存在内存中,系统内存占用会很大
            //imageArr.append(UIImage(imageLiteral: "\(imagePrefix)\(imagePath)"))
            //这个方式创建的图片不会缓存在内存中,但是要注意,图片不能放在.xcassets文件中,.xcassets中的文件都是经过处理的,不能像这样取得
            imageArr.append(UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource("\(imagePrefix)\(imagePath)", ofType: "jpg")!)!)
        }
        return imageArr
    }

    //播放动画
    func doAnimatingImage(imageArr:[UIImage])
    {
        //当前正在执行动画的时候,不响应动作
        if imageView.isAnimating() == false
        {
            //设置帧动画图片数组
            imageView.animationImages = imageArr
            //设置重复次数,默认是执行无数次,这里设置成1次
            imageView.animationRepeatCount = 1
            //设置动画执行时间
            imageView.animationDuration = NSTimeInterval(Double(imageArr.count) * 0.07)
            //开始执行动画
            imageView.startAnimating()
            //动画之行完毕之后释放内存,这个函数是延时执行的
            self.performSelector(Selector("clearAnimetingImage"), withObject: nil, afterDelay: imageView.animationDuration)
        }
    }
    //清除对UIImage数组的强引用,释放内存
    func clearAnimetingImage()
    {
        imageView.animationImages = nil
    }

}

图片浏览器

//
//  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
            }
        })
    }

}

ios键盘的弹出与收回

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

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