Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

Swift基础之实现选择图片时,出现类似于ActionSheet的样式

$
0
0

之前看到过有APP在选择图片时,调用手机相册时,将手机相册做成了左右滑动选择的效果,这次展示的就是这种样式,用OC语言已经有人实现过类似的代码,在这里写的仅仅是效果展示的代码调用,具体代码,可以自己研究一下。不喜勿喷~

代码如下:

func headBtnClick(btn:UIButton) {
        
        //创建授权状态
        let authorization = PHPhotoLibrary.authorizationStatus()
        
        //如果打开相册授权没有被允许
        if authorization == .notDetermined {
            PHPhotoLibrary.requestAuthorization({ (status) in
                DispatchQueue.main.async(execute: {
                    let alertV = UIAlertController.init(title: "温馨提示", message: "请打开此APP的相册权限", preferredStyle: .alert)
                    let okAction = UIAlertAction.init(title: "确定", style: .cancel, handler: nil)
                    alertV.addAction(okAction)
                    self.present(alertV, animated: true, completion: nil)
                })
            })
        }else if authorization == .authorized{
            //如果被授权了,弹出图片选择器
            let imgPSVC = ImagePickerSheetController()
            //操作过程就是:如果选择了某张图片,就会触发第二个按钮的显示,然后进行后续图片的显示操作
            //添加第一个操作按钮
            imgPSVC.addAction(ImageAction.init(title: "第一个操作", secondaryTitle: "确定", style: ImageActionStyle.default, handler: { (_) in
                //点击第一个进行的后续操作
                let alertV = UIAlertController.init(title: "温馨提示", message: "点击的是第一个操作", preferredStyle: .alert)
                let okAction = UIAlertAction.init(title: "确定", style: .cancel, handler: nil)
                alertV.addAction(okAction)
                self.present(alertV, animated: true, completion: nil)
                
            }, secondaryHandler: { (action, numberOfPhotos) in
                imgPSVC.getSelectedImagesWithCompletion({ (imageArr) in
                    btn.setImage(imageArr[0], for: .normal)
                })
            }))
            
            //添加第二个操作按钮
            imgPSVC.addAction(ImageAction.init(title: "取消", secondaryTitle: "取消", style: .cancel, handler: nil, secondaryHandler: nil))
            
            present(imgPSVC, animated: true, completion: nil)
        }
    }

效果图:(源代码:https://github.com/hbblzjy/SwiftSelectImageDemo)

     

作者:hbblzjy 发表于2017/1/12 17:32:02 原文链接
阅读:137 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>