登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

swift 仿新浪微博顶部导航栏popView

[复制链接]
来自: 攻小楷o 分类: iOS精品源码 上传时间: 2016-6-21 16:40:08

项目介绍:

因为自己也要用到,参照其他oc的思路,用swift写了一个通用类,效果跟新浪微博顶部导航的弹出框差不多最近在看布局的问题,所用项目里用到了cocoapods,安装了snapKit用来设置简单的布局跟屏幕适配.
主要代码:
[Objective-C] 查看源文件 复制代码
import Foundation
import UIKit
let  CurrentScreenBounds = UIScreen.mainScreen().bounds

//枚举,popView出现的位置
enum PopViewPosition : Int {
    case left
    case center
    case right
}


//纯粹练习protocol
//兼容objc做一个可选方法,popView消失前做一些事
@objc protocol PopViewDelegate {
    
    optional func popViewWillDismiss(popView popView:CustomPopView)

}

class CustomPopView: UIView{
    var meunImageView: UIImageView?
    var cover :UIButton?
    var contentView:UIView?
    var delegate: PopViewDelegate?
    //获得arrowPosition时定义background的背景图片(箭头不同)
    var _arrowPosition:PopViewPosition?
    var arrowPosition:PopViewPosition?{
        willSet{
            self.arrowPosition = _arrowPosition
        }
        //类似oc的重写set方法
        didSet{
            switch arrowPosition! {
            case PopViewPosition.left:
                self.meunImageView?.image = resizedImage("popover_background_left")
            case PopViewPosition.center:
                self.meunImageView?.image = resizedImage("popover_background")
            case PopViewPosition.right:
                self.meunImageView?.image = resizedImage("popover_background_right")
            }

        }
    }

    //重写父类初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.frame = CurrentScreenBounds;
        let cover = UIButton.init()
        cover.backgroundColor = UIColor.clearColor()
        cover.addTarget(self, action: #selector(CustomPopView.coverClick), forControlEvents: .TouchUpInside)
        self.addSubview(cover)
        self.cover = cover;
        let meunImageView = UIImageView.init()
        meunImageView.userInteractionEnabled = true
        self.addSubview(meunImageView)
        self.meunImageView = meunImageView;
        self.cover?.frame = self.bounds
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //菜单view
    func menuWithContentView(contentView:UIView)->AnyObject{
        return initWithContentView(contentView)
    }
    
    func initWithContentView(contentView:UIView)->AnyObject{
        self.contentView = contentView
        return self
    }

//    //设置背景图片
//    func setBackgroundImage(backgroundImag:UIImage){
//        meunImageView?.image = backgroundImag
//    }

    //点击消失
    func dismiss(){
        
        //消失前进行的一些操作
//        self.delegate?.popViewWillDismiss!(popView: self)

        //添加消失动画,延时remove
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0.5)
        self.alpha = 0
        UIView.commitAnimations()
        let time: NSTimeInterval = 0.5
        let delay = dispatch_time(DISPATCH_TIME_NOW,
                                  Int64(time * Double(NSEC_PER_SEC)))
        dispatch_after(delay, dispatch_get_main_queue()) {
           self.removeFromSuperview()
        }
    }
    
    //点击背景消失
    func coverClick()  {
        dismiss()
    }
    
    //设置image大小,拉伸图片
    func resizedImage(name:String) -> UIImage {
        let image = UIImage.init(named: name)
        return (image?.stretchableImageWithLeftCapWidth(Int(image!.size.width * 0.5), topCapHeight: Int(image!.size.height * 0.5)))!
        
    }
    
    //在指定区域显示菜单
    func showInRect(){
        let window:UIWindow = UIApplication.sharedApplication().windows.last!
        window.addSubview(self)
        self.alpha = 0
        meunImageView?.addSubview(contentView!)
        
        //淡出动画
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0.5)
        self.alpha = 1
        UIView.commitAnimations()
    }

    func setArrowPosition(arrowPosition:PopViewPosition) {
        self.arrowPosition = arrowPosition
        switch arrowPosition {
        case .left:
            self.meunImageView?.image = resizedImage("popover_background_left")
        case .center:
            self.meunImageView?.image = resizedImage("popover_background")
        case .right:
            self.meunImageView?.image = resizedImage("popover_background_right")
        }
    }
    
}


代码下载:
CustomPopView-Swift.zip (101.73 KB, 下载次数: 38, 售价: 5 金钱)
1300 0 0
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542


代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表