天天看点

好书app中用到的常用第三方库的总结

在好书app中有很多常用的第三方库,自己总结一下在以后的项目中也可以应用

好书app中用到的常用第三方库的总结

shareSDK主要是一个分享功能,可以分享到微信,qq等,主要是要去看他的官网,注册app即可

HZPhotoBrower主要是图片放大的时候用到的 在点击图片的时候可以放大图片,可以选择放大图片时只有一张,或者轮播,有好几张 随后设置当前是在第几张

还要设置代理,和代理实现的方法

//实现图片点击时的放大
    func photoBrowser(sender:UITapGestureRecognizer){
        let photoBrowser = HZPhotoBrowser()
        //放大图片的个数
        photoBrowser.imageCount = 1
        //当前浏览的是第几张图
        photoBrowser.currentImageIndex = 0
        //代理
        photoBrowser.delegate = self
        photoBrowser.show()
    }
    //实现photoBroswer的回调
    func photoBrowser(browser: HZPhotoBrowser!, highQualityImageURLForIndex index: Int) -> NSURL! {
        //最终放大的图片
        let coverFile = self.bookObject!["cover"] as? AVFile
        return NSURL(string: coverFile!.url)
    }
    func photoBrowser(browser: HZPhotoBrowser!, placeholderImageForIndex index: Int) -> UIImage! {
        //缓冲图 在加载过程当中
        return self.bookTitleView?.cover?.image
    }
           

leftSwipeCell主要是在tableView中,当上一个左滑的时候,下一个就要把左滑的显示去掉 将自定义的tableViewCel继承自SWTableViewCell

import UIKit

class pushBookTableViewCell:SWTableViewCell{
//自定义cell
    var BookName:UILabel?
    var Editor:UILabel?
    var more:UILabel?
    //当前展开的是哪一个cell
    var swipCellindexPath:NSIndexPath?
    //封面
    var cover:UIImageView?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
//初始化的方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //移除所有的cell
        for view in self.contentView.subviews{
            view.removeFromSuperview()
            self.BookName = UILabel(frame: CGRectMake(70,8,242,25))
            self.Editor = UILabel(frame: CGRectMake(78,33,242,25))
            self.more = UILabel(frame: CGRectMake(78,66,242,25))
            self.BookName?.font = UIFont(name: MY_FONT, size: 15)
            self.Editor?.font = UIFont(name: MY_FONT, size: 15)
            self.more?.font = UIFont(name: MY_FONT, size: 15)
            self.contentView.addSubview(BookName!)
            self.contentView.addSubview(Editor!)
            self.contentView.addSubview(more!)
            self.cover = UIImageView(frame: CGRectMake(8, 9, 56, 70))
            self.contentView.addSubview(cover!)
                   }
    }
  
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
           
//记录上次左滑的按钮 
    var swipeCellIndexPath:NSIndexPath?
           

继承他的协议

SWTableViewCellDelegate
           
//删除和编辑的按钮只能在一个cell钟出现
    func swipeableTableViewCell(cell: SWTableViewCell!, scrollingToState state: SWCellState) {
    let indexPath = self.tableView?.indexPathForCell(cell)
        //当前有展开的按钮
        if state == .CellStateRight{
            if self.swipeCellIndexPath != nil && self.swipeCellIndexPath?.row != indexPath?.row{
                //遮盖住
                let swipeCell = self.tableView?.cellForRowAtIndexPath(self.swipeCellIndexPath!) as! pushBookTableViewCell
                swipeCell.hideUtilityButtonsAnimated(true)
            }
                //将当前的左滑的cell记录下来
                self.swipeCellIndexPath = indexPath
            //没有左滑的cell
        }else if state == .CellStateCenter{
            self.swipeCellIndexPath = nil
        }
        }
    //当点击右滑的两个按钮的时候
    
    func swipeableTableViewCell(cell: SWTableViewCell!, didTriggerRightUtilityButtonWithIndex index: Int) {
        //将点击的cell隐藏起来
     cell.hideUtilityButtonsAnimated(true)
        let indexPath = self.tableView?.indexPathForCell(cell)
        
//是编辑的这个按钮
    let object = self.dataArray[(indexPath?.row)!] as? AVObject
        if index == 0{
            let vc = pushNewBookController()
            vc.fixType = "fix"
            GeneralFactory.addTitleWithTile(vc, title1: "关闭", title2: "发布")
            
            vc.bookObject = object
            self.presentViewController(vc, animated: true, completion: nil)
            
        }else {
            //删除的操作
            ProgressHUD.show("")
            //删除讨论区和点赞区的所有有关这本书的信息
            let discussQuery = AVQuery(className: "discuss")
            discussQuery.whereKey("BookObject", equalTo: object)
            discussQuery.findObjectsInBackgroundWithBlock({ (results, error) -> Void in
                    for Book in results {
                        let BookObject = Book as? AVObject
                        BookObject?.deleteInBackground()
                    }
                })
                
                let loveQuery = AVQuery(className: "Love")
                loveQuery.whereKey("BookObject", equalTo: object)
                loveQuery.findObjectsInBackgroundWithBlock({ (results, error) -> Void in
                    for Book in results {
                        let BookObject = Book as? AVObject
                        BookObject?.deleteInBackground()
                    }
                })
                
                object?.deleteInBackgroundWithBlock({ (success, error) -> Void in
                    if success {
                        ProgressHUD.showSuccess("删除成功")
                        self.dataArray.removeObjectAtIndex((indexPath?.row)!)
                        self.tableView?.reloadData()
                        
                        
                    }else{
                        
                    }
                })
                
                
            }
    }
           

inputView主要是在讨论区中,他可以自带的键盘和文本框,并且有评论这个按钮 可以将文本加入到tableView中

继承他的协议InputViewDelegate 从xib进行加载 在加入inputView的时候 最好下面覆盖一层灰色的背景layView

self.input = NSBundle.mainBundle().loadNibNamed("InputView", owner: self, options: nil).last as? InputView
           
func connect(){
        //发表评论的时候 从xib加载发表评论的文件框,因此加载方法和纯代码创建的时候不同
        if self.input == nil{
            //从xib 加载的 只有一个对象
            self.input = NSBundle.mainBundle().loadNibNamed("InputView", owner: self, options: nil).last as? InputView
            self.input?.frame = CGRectMake(0,SCREEN_HEIGHT - 44,SCREEN_WIDTH,44)
            //所有的动作都在delegate中
            self.input?.delegate = self
            self.view.addSubview(self.input!)
            //当发表联系时,因马上出现键盘
            self.input?.inputTextView?.becomeFirstResponder()
        }
        if self.layView == nil{
            self.layView = UIView(frame: self.view.frame)
            self.layView?.backgroundColor = UIColor.grayColor()
            self.layView?.alpha = 0
            let tap = UITapGestureRecognizer(target: self, action: "tapInputView")
             self.layView?.addGestureRecognizer(tap)
            //将某个界面插入到另外一个界面下
           self.view.insertSubview(self.layView!, belowSubview: self.input!)
        }
        self.layView?.hidden = false
    }
           

为layView添加点击事件 当一点击时 键盘消失

func tapInputView() {
        //当点击这个layView的时候 键盘就消失
        self.input?.inputTextView?.resignFirstResponder()
    }
           

当加入inputView的时候 都会触发键盘的出现和消失 所以这个第三方库已经为我们添加了这方面的通知

//实现inputView的delegate
    //当文本高度发生变化时,整个键盘向上移动
    func textViewHeightDidChange(height: CGFloat) {
        //输入框的高度加10 并且输入框向上已 改变bottom 
       self.input?.height = height + 10
       self.input?.bottom = SCREEN_HEIGHT - CGFloat(self.keyBoardHeight)
    }
     //在inputView中,点击评论的时候 触发的动作
    func publishButtonDidClick(button: UIButton!) {
        //加上进度的提示
        ProgressHUD.show("")
        //传到leanCloud的后台
        let object = AVObject(className: "discuss")
        object.setObject(self.input?.inputTextView?.text, forKey: "text")
        object.setObject(AVUser.currentUser(), forKey: "user")
        object.setObject(self.bookObject, forKey: "BookObject")
        object.saveInBackgroundWithBlock { (success, error) -> Void in
            if success{
                ProgressHUD.showSuccess("评论成功")
            }else{
                ProgressHUD.showError("评论失败")
            }
        }
    }
    func keyboardWillHide(inputView: InputView!, keyboardHeight: CGFloat, animationDuration duration: NSTimeInterval, animationCurve: UIViewAnimationCurve) {
        //当键盘消失时,设置键盘底部的高度为屏幕高度再加上Input的高度
UIView.animateWithDuration(duration, delay: 0, options: .BeginFromCurrentState, animations: { () -> Void in
    self.input?.bottom = SCREEN_HEIGHT + (self.input?.height)!
    self.layView?.alpha = 0.0
    }, completion: {(finish) -> Void in
    //当键盘消失时,将layView再次隐藏起来
        self.layView?.hidden = true
})
    }
    func keyboardWillShow(inputView: InputView!, keyboardHeight: CGFloat, animationDuration duration: NSTimeInterval, animationCurve: UIViewAnimationCurve) {
        //当键盘出现的时候 设置Input的键盘底部的高度离屏幕最上方的距离即设置bottom属性
        //用一个动画来进行设置
        self.keyBoardHeight = Double(keyboardHeight)
        UIView.animateWithDuration(duration, delay: 0, options: .BeginFromCurrentState, animations: { () -> Void in
            self.input?.bottom = SCREEN_HEIGHT - keyboardHeight
            //背景界面的透明度
            self.layView?.alpha = 0.2
            }) { (finish) -> Void in
                
        }
    }
           

SDWebImage主要是imageView的扩展,他支持可以从url进行缓存,多线程的加载,不会阻塞主线程 不需要继承相关的协议

//图片加载 还有默认的图片
        self.bookTitleView?.cover?.sd_setImageWithURL(NSURL(string: (coverFile?.url)!), placeholderImage: UIImage(named: "Cover"))
           

MJRefresh主要是tableView的上拉加载和下拉刷新的时候可以使用

注册初始化

//上拉加载 下拉刷新
        self.tableView?.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: "headerReFresh")
        self.tableView?.mj_footer = MJRefreshBackFooter(refreshingTarget: self, refreshingAction: "footerReFresh")
           
func headerReFresh() {
        //查询条件
        let quary = AVQuery(className: "discuss")
        //倒序排序
        quary.orderByAscending("createdAt")
        //表头加载的时候只需20个即可 不需要跳过已有的
        quary.limit = 20
        quary.skip = 0
        quary.whereKey("user", equalTo: AVUser.currentUser())
        quary.whereKey("BookObject", equalTo: self.bookObject)
        //将指针传回
       quary.includeKey("user")
       quary.includeKey("BookObject")
       quary.findObjectsInBackgroundWithBlock { (results, error) -> Void in
        self.tableView?.mj_header.endRefreshing()
        self.dataArray.removeAllObjects()
        self.dataArray.addObject(results)
        self.tableView?.reloadData()
        }
    }
    func footerReFresh() {
        //查询条件
        let quary = AVQuery(className: "discuss")
        //倒序排序
        quary.orderByAscending("createdAt")
        //表尾加载的时候也需要20个 但需要跳过已经加载的
        quary.limit = 20
        quary.skip = self.dataArray.count
        quary.whereKey("user", equalTo: AVUser.currentUser())
        quary.whereKey("BookObject", equalTo: self.bookObject)
        //将指针传回
        quary.includeKey("user")
        quary.includeKey("BookObject")
        quary.findObjectsInBackgroundWithBlock { (results, error) -> Void in
            self.tableView?.mj_footer.endRefreshing()
            //self.dataArray.removeAllObjects()
            self.dataArray.addObject(results)
            self.tableView?.reloadData()
        
    }
           

progressHUD类似于警告框的作用,感觉以后自己也不用那么麻烦协警告框了

ProgressHUD.show("", interaction:false)
            ProgressHUD.dismiss()
            self.dismissViewControllerAnimated(true, completion: nil)
        }else {
            if error.code == 210{
                ProgressHUD.showError("用户名或密码错误")
            }else if error.code == 211{
                ProgressHUD.showError("不存在该用户")
            }else if error.code == 216{
                ProgressHUD.showError("未验证邮箱")
            }else if error.code == 1{
                ProgressHUD.showError("操作频繁")
            }else {
                ProgressHUD.showError("登陆失败")
           

XKeyBoard主要是用在一些当问文字处理的时候 要出现键盘 随后键盘的遮挡问题

其实也是一个通知的机制 首先注册通知

//注册键盘出现和消失
        XKeyBoard.registerKeyBoardHide(self)
        XKeyBoard.registerKeyBoardShow(self)
           

随后实现通知当键盘出现的时候和键盘消失的时候

//键盘遮挡
    func keyboardWillHideNotification(notifacition:NSNotification) {
        
    self.textView?.contentInset = UIEdgeInsetsMake(0, 0,0, 0)
    }
    func keyboardWillShowNotification(notifacition:NSNotification) {
           //返回键盘的大小
        let rect = XKeyBoard.returnKeyBoardWindow(notifacition)
        self.textView?.contentInset = UIEdgeInsetsMake(0, 0, rect.size.height, 0)
    }

}
           
func keyboardWillHideNotification(notifacition:NSNotification) {
        UIView.animateWithDuration(0.3) { () -> Void in
            self.topLayout.constant = 0
            //加载新的约束
            self.view.layoutIfNeeded()
            
        }
    }
    func keyboardWillShowNotification(notifacition:NSNotification) {
        //做一个动画
        UIView.animateWithDuration(0.3) { () -> Void in
            self.topLayout.constant = -200
            //加载新的约束
            self.view.layoutIfNeeded()
        }
        
    }
           

IGLDropDownMenu主要是下滑的时候的一个动画的效果 实现协议IGLDropDownMenuDelegate

定义两个menu

var dropDownMenu1:IGLDropDownMenu?
    var dropDownMenu2:IGLDropDownMenu?
           
</pre><pre code_snippet_id="1589511" snippet_file_name="blog_20160226_18_9938298" name="code" class="plain">  self.initDropArray()
        self.createDropMenu(self.literatureArray1, array2: self.literatureArray2)
           

将数组初始化为这个menu的元素

func createDropMenu(array1:Array<NSDictionary>,array2:Array<NSDictionary>){
        let dropDownItem1 = NSMutableArray()
        for var i = 0;i<array1.count;i++ {
            let dict = array1[i]
            let item = IGLDropDownItem()
            item.text = dict["title"] as? String
            dropDownItem1.addObject(item)
        }
        
        let dropDownItem2 = NSMutableArray()
        for var i = 0;i<array2.count;i++ {
            let dict = array2[i]
            let item = IGLDropDownItem()
            item.text = dict["title"] as? String
            dropDownItem2.addObject(item)
        }
        //重绘的操作
        self.dropDownMenu1?.removeFromSuperview()
        self.dropDownMenu1 = IGLDropDownMenu()
        self.dropDownMenu1?.menuText = "点我,展开详细列表"
        self.dropDownMenu1?.menuButton.textLabel.adjustsFontSizeToFitWidth = true
        self.dropDownMenu1?.menuButton.textLabel.textColor = setColor(38, g: 82, b: 67)
        self.dropDownMenu1?.paddingLeft = 15
        self.dropDownMenu1?.delegate = self
        //动画
        self.dropDownMenu1?.type = .Stack
        self.dropDownMenu1?.itemAnimationDelay = 0.1
        self.dropDownMenu1?.gutterY = 5
        self.dropDownMenu1?.dropDownItems = dropDownItem1 as [AnyObject]
        self.dropDownMenu1?.frame = CGRectMake(20, 150, SCREEN_WIDTH/2-30, (SCREEN_HEIGHT-200)/7)
        self.view.addSubview(self.dropDownMenu1!)
        self.dropDownMenu1?.reloadView()
        self.dropDownMenu2?.removeFromSuperview()
        self.dropDownMenu2 = IGLDropDownMenu()
        self.dropDownMenu2?.menuText = "点我,展开详细列表"
        self.dropDownMenu2?.menuButton.textLabel.adjustsFontSizeToFitWidth = true
        self.dropDownMenu2?.menuButton.textLabel.textColor = setColor(38, g: 82, b: 67)
        self.dropDownMenu2?.paddingLeft = 15
        self.dropDownMenu2?.delegate = self
        self.dropDownMenu2?.type = .Stack
        self.dropDownMenu2?.itemAnimationDelay = 0.1
        self.dropDownMenu2?.gutterY = 5
        self.dropDownMenu2?.dropDownItems = dropDownItem2 as [AnyObject]
        self.dropDownMenu2?.frame = CGRectMake(SCREEN_WIDTH/2+10, 150, SCREEN_WIDTH/2-30, (SCREEN_HEIGHT-200)/7)
        self.view.addSubview(self.dropDownMenu2!)
        self.dropDownMenu2?.reloadView()
        
    }
    //dropDownMenu的delegate
    func dropDownMenu(dropDownMenu: IGLDropDownMenu!, selectedItemAtIndex index: Int) {
        if dropDownMenu == self.dropDownMenu1 {
            let item = self.dropDownMenu1?.dropDownItems[index] as? IGLDropDownItem
            self.detailType = (item?.text)!
            self.dropDownMenu2?.menuButton.text = self.detailType
        }else{
            let item = self.dropDownMenu2?.dropDownItems[index] as? IGLDropDownItem
            self.detailType = (item?.text)!
            self.dropDownMenu1?.menuButton.text = self.detailType
        }
    }
    
    
}
           

AKSegmentedControl主要是选择栏的加强版 个人认为,这里是和上面的dropMenu联合使用的 是做一个动画,所以在有些工程中,可以不用的

var segmentController1:AKSegmentedControl?
    var segmentController2:AKSegmentedControl?
           
//初始化segment
    func initSegment() {
        let buttonArray1 = [
            ["image":"ledger","title":"文学","font":MY_FONT],
            ["image":"drama masks","title":"人文社科","font":MY_FONT],["image":"aperture","title":"生活","font":MY_FONT],
        ]
        let buttonArray2 = [
            ["image":"atom","title":"经管","font":MY_FONT],
            ["image":"alien","title":"科技","font":MY_FONT],
            ["image":"fire element","title":"网络流行","font":MY_FONT],
        ]
        self.segmentController1 = AKSegmentedControl(frame: CGRectMake(10,50,SCREEN_WIDTH - 20,37))
        self.segmentController1?.initButtonWithTitleandImage(buttonArray1)
        self.view.addSubview(segmentController1!)
        self.segmentController2 = AKSegmentedControl(frame: CGRectMake(10,110,SCREEN_WIDTH - 20,37))
        self.segmentController2?.initButtonWithTitleandImage(buttonArray2)
        self.view.addSubview(self.segmentController2!)
        segmentController1?.addTarget(self, action: "segmentControllerAction:", forControlEvents: .ValueChanged)
        segmentController2?.addTarget(self, action: "segmentControllerAction:", forControlEvents: .ValueChanged)
        
    }
    func segmentControllerAction(segment:AKSegmentedControl){
        let index = segment.selectedIndexes.firstIndex
        if segment == self.segmentController1{
            //把2的点击效果取消掉
            self.segmentController2?.setSelectedIndex(3)
        }else{
            self.segmentController1?.setSelectedIndex(3)
            
        }
        self.type = ((segment.buttonsArray[index]) as! UIButton).currentTitle!
        if self.dropDownMenu1 != nil {
            self.dropDownMenu1?.resetParams()
        }
        if self.dropDownMenu2 != nil {
            self.dropDownMenu2?.resetParams()
        }
        
        switch (index){
        case 0:
            self.createDropMenu(self.literatureArray1, array2: self.literatureArray2)
            break
        case 1:
            self.createDropMenu(self.humanitiesArray1, array2: self.humanitiesArray2)
            break
        case 2:
            self.createDropMenu(self.livelihoodArray1, array2: self.livelihoodArray2)
            break
        case 3:
            self.createDropMenu(self.economiesArray1, array2: self.economiesArray2)
            break
        case 4:
            self.createDropMenu(self.technologyArray1, array2: self.technologyArray2)
            break
        case 5:
            self.createDropMenu(self.NetworkArray1, array2: self.NetworkArray2)
            break
        default:
            break
        }
        
    }
           

JVFloatTextField主要应该是能够使textField更加美观吧 个人认为

var BookName:JVFloatLabeledTextField?
    
    var BookEditor:JVFloatLabeledTextField?
           
self.BookName = JVFloatLabeledTextField(frame: CGRectMake(128,8+40,SCREEN_WIDTH-128-15,30))
        self.BookEditor = JVFloatLabeledTextField(frame: CGRectMake(128,8+70+40,SCREEN_WIDTH-128-15,30))
        
        self.BookName?.placeholder = "书名"
        self.BookEditor?.placeholder = "作者"
        
        self.BookName?.floatingLabelFont = UIFont(name: MY_FONT, size: 14)
        self.BookEditor?.floatingLabelFont = UIFont(name: MY_FONT, size: 14)
        
           

LDXScore主要是一个加星星的一个评分的view吧

self.Score = LDXScore(frame: CGRectMake(100,10,100,22))
        //是否可以编辑
        self.Score?.isSelect = true
        self.Score?.normalImg = UIImage(named:"btn_star_evaluation_normal")
        self.Score?.highlightImg = UIImage(named: "btn_star_evaluation_press")
        self.Score?.max_star = 5
        //一出来就是5颗
        self.Score?.show_star = 5
           

可以把score读出来 应show_Star

self.Score?.show_star
           

VPimagePicker主要是图片的裁剪 继承协议VPImageCropperDelegate

func getImageFromPicker(image: UIImage) {
        let CropVC = VPImageCropperViewController(image: image, cropFrame: CGRectMake(0,100,SCREEN_WIDTH,SCREEN_WIDTH * 1.273), limitScaleRatio: 3)
        CropVC.delegate = self
        self.presentViewController(CropVC, animated: true, completion: nil)
    
    }
           
func imageCropper(cropperViewController: VPImageCropperViewController!, didFinished editedImage: UIImage!) {
        self.BookTitle?.BookCover?.setImage(editedImage, forState: .Normal)
        cropperViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    func imageCropperDidCancel(cropperViewController: VPImageCropperViewController!) {
       cropperViewController.dismissViewControllerAnimated(true, completion: nil)
    }
           

主要就是这么一些 有补足以后再加,就这么多了