swift图片多选,取消选择模型


以下代码未完全实现多选功能,仅仅为中间过程,作为自己复习使用。因为经过优化的代码,最终会忘记中间的过程。
读取本地图片,然后列出,然后可以多选,可以取消选择

//
//  ViewController.swift
//  imageTest
//
//  Created by  on 15/11/30.
//  Copyright (c) 2015年 JS. All rights reserved.
//
 
import UIKit
import AssetsLibrary
 
class ViewController: UIViewController {
    //资源库管理类
    var assetsLibrary =  ALAssetsLibrary()
    //保存照片集合
    var assets = [ALAsset]()
 
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var countOne = 0
 
        var qix = 0;
 
 
        let registerBtn : UIButton = UIButton()
        registerBtn.frame = CGRectMake(10, 200, 300, 40)
        registerBtn.backgroundColor = UIColor.lightGrayColor()
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.addTarget(self, action: "goRegister:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(registerBtn)
 
 
        //被读取的数据, 当前函数内无法读取到asset的数据,必须有另外一个动作触发,所以弄了个按钮,下一步动作
        //ALAssetsGroupSavedPhotos表示只读取相机胶卷(ALAssetsGroupAll则读取全部相簿)
        assetsLibrary.enumerateGroupsWithTypes(ALAssetsGroupSavedPhotos, usingBlock: {
            (group: ALAssetsGroup!, stop) in
            print("is goin:")
            if group != nil
            {
                let assetBlock : ALAssetsGroupEnumerationResultsBlock = {
                    (result: ALAsset!, index: Int, stop) in
                    if result != nil
                    {
 
                        self.assets.append(result)//装载图片
                        qix = countOne%6;
 
                        var pos_x:CGFloat = CGFloat(10 + Int(qix*55) );
                        var pos_y:CGFloat = CGFloat(40 + Int(countOne/6 * 55));
 
                        //取缩略图
                        let image = UIImage(CGImage:result.thumbnail().takeUnretainedValue())
                        var imageView = UIImageView(frame: CGRectMake(pos_x, pos_y, 50, 50))
                        imageView.image = image
                        imageView.userInteractionEnabled = true;//点击手势
                        var singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageViewTouch:")
                        imageView .addGestureRecognizer(singleTap)
                        imageView.tag = countOne
 
                        //边框颜色设置
                        imageView.layer.borderColor = UIColor.grayColor().CGColor
                        //边框的宽度设置
                        imageView.layer.borderWidth = 1
                        // 最主要的一句
                        imageView.layer.masksToBounds = true
                        //把uiimageview加载到父控件上,也就是self.view
                        self.view.addSubview(imageView)
 
 
                        qix++
                        countOne++
 
                    }
                }
 
 
                group.enumerateAssetsUsingBlock(assetBlock)
                print("assets:\(countOne)")
                //collectionView网格重载数据
                //self.collectionView?.reloadData()
                println("======");
            }
 
            }, failureBlock: { (fail) in
                print(fail)
        })
 
    }
 
    func imageViewTouch(gestur:UITapGestureRecognizer){
        println("我被摸了!!");
        let x = gestur.view?.tag
 
        print(assets[x!])
 
        let countOne = x //还原countOne
        var qix = countOne!%6;
 
        var pos_x:CGFloat = CGFloat(10 + Int(qix*55) );
        var pos_y:CGFloat = CGFloat(40 + Int(countOne!/6 * 55));
 
        let image = UIImage(CGImage:assets[x!].thumbnail().takeUnretainedValue())
        var imageView = UIImageView(frame: CGRectMake(pos_x, pos_y, 50, 50))
        imageView.image = image
 
        imageView.userInteractionEnabled = true;//点击手势
        var singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageViewTouch2:")
        imageView .addGestureRecognizer(singleTap)
        imageView.tag = countOne!
 
        //边框颜色设置
        imageView.layer.borderColor = UIColor.redColor().CGColor
        //边框的宽度设置
        imageView.layer.borderWidth = 1
        // 最主要的一句
        imageView.layer.masksToBounds = true
        //把uiimageview加载到父控件上,也就是self.view
        self.view.addSubview(imageView)
 
    }
 
    //取消摸
    func imageViewTouch2(gestur:UITapGestureRecognizer){
        println("我被摸了!!");
        let x = gestur.view?.tag
 
        print(assets[x!])
 
        let countOne = x //还原countOne
        var qix = countOne!%6;
 
        var pos_x:CGFloat = CGFloat(10 + Int(qix*55) );
        var pos_y:CGFloat = CGFloat(40 + Int(countOne!/6 * 55));
 
        let image = UIImage(CGImage:assets[x!].thumbnail().takeUnretainedValue())
        var imageView = UIImageView(frame: CGRectMake(pos_x, pos_y, 50, 50))
        imageView.image = image
 
        imageView.userInteractionEnabled = true;//点击手势
        var singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageViewTouch:")
        imageView .addGestureRecognizer(singleTap)
        imageView.tag = countOne!
 
        //边框颜色设置
        imageView.layer.borderColor = UIColor.grayColor().CGColor
        //边框的宽度设置
        imageView.layer.borderWidth = 1
        // 最主要的一句
        imageView.layer.masksToBounds = true
        //把uiimageview加载到父控件上,也就是self.view
        self.view.addSubview(imageView)
 
    }
 
 
 
    @IBAction func goRegister(sender: AnyObject) {
 
        //展示图片列表
        var count = assets.count;
 
        for var index = 0; index < count; ++index {
            println("index is \(index)")
        }
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
 
 
}

更加简洁的办法是我们将imageView存入一个数组,然后利用这个数组来改变图片选择情况
var imgs = [UIImageView]()

//点击,边框变红色
    func imageViewTouchArr(gestur:UITapGestureRecognizer){
        let x = gestur.view?.tag
        var imageView = imgs[x!]
        var singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageViewTouchArrCancel:")
        imageView .addGestureRecognizer(singleTap)
        imageView.layer.borderColor = UIColor.redColor().CGColor
    }
    //点击取消边框
    func imageViewTouchArrCancel(gestur:UITapGestureRecognizer){
        let x = gestur.view?.tag
        var imageView = imgs[x!]
        var singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageViewTouchArr:")
        imageView .addGestureRecognizer(singleTap)
        imageView.layer.borderColor = UIColor.grayColor().CGColor
    }
如果你是一名技术人员可加我QQ 2651-0442-02,如果你是java技术人还可以加入QQ群 1784-9136-0
你将得到的不仅仅是技术的交流,还有职业机会,人生解惑.
首发地址:月小升博客http://java-er.com/blog/swift-select-many-cacel/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
此条目发表在 Xcode 分类目录,贴了 , , 标签。将固定链接加入收藏夹。
既然来了,就评论一下,不会怀孕的

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">