         let index = btn.tag
         if(false == btn.curSelected){
             // 把其他菜单按钮复位
-            for btn in self.subviews{
-                if let b = btn as? GalleryMenuButtton{
-                    b.curSelected = false
-                }
-            }
-            for v in self.subItemContainer!.subviews{
-                v.removeFromSuperview()
-            }
-            
+            self.resetMenu()
             // 生成自己的二级菜单
             sender.curSelected = true;
             if let dic = self.menuConfig[index] as? [NSObject:AnyObject]{
                 self.isExpanded = true
                 // 生成所有二级菜单
                 if let subItems = dic["items"] as? [AnyObject]{
-                    let w:CGFloat = self.bounds.width / 4
-                    let h:CGFloat = GalleryMenuItem_H
-                    for (var i = 0; i < subItems.count; i++){
-                        if let one = subItems[i] as? [NSObject:AnyObject]{
-                            let row:Int = i/4
-                            let col:Int = i%4
-                            let rect = CGRect(x: w * CGFloat(col), y: h * CGFloat(row), width: w, height: h)
-                            let btn = GalleryMenuItem(frame: rect)
-                            btn.tag = i
-                            btn.addTarget(self, action: Selector("onTapItem:"), forControlEvents: UIControlEvents.TouchUpInside)
-                            if let t = one["title"] as? String{
-                                btn.setTitle(t, forState: UIControlState.Normal)
-                            }
-                            self.subItemContainer!.addSubview(btn)
-                        }
-                    }
-                    let rowCount:Int = (subItems.count-1)/4 + 1
-                    let offh = GalleryMenuItem_H*CGFloat(rowCount) + GalleryMenuItem_H*50.0/80.0/2.0
-                    self.subItemContainer?.frame = CGRectMake(0, GalleryMenuBar_H, self.frame.size.width, offh)
+                    self.generateMenuItems(subItems)
                 }
             }
         }else{

            }
        }
    }
//    var menuBtnContainer:UIView?
    var subItemContainer:UIView?
    var menuConfig = [AnyObject](){
        didSet{

        }
    }
    
    // 生成所有二级菜单
    func generateMenuItems(data:[AnyObject]){
        let w:CGFloat = self.bounds.width / 4
        let h:CGFloat = GalleryMenuItem_H
        for (var i = 0; i < data.count; i++){
            if let one = data[i] as? [NSObject:AnyObject]{
                let row:Int = i/4
                let col:Int = i%4
                let rect = CGRect(x: w * CGFloat(col), y: h * CGFloat(row), width: w, height: h)
                let btn = GalleryMenuItem(frame: rect)
                btn.tag = i
                btn.addTarget(self, action: Selector("onTapItem:"), forControlEvents: UIControlEvents.TouchUpInside)
                if let t = one["title"] as? String{
                    btn.setTitle(t, forState: UIControlState.Normal)
                }
                self.subItemContainer!.addSubview(btn)
            }
        }
        let rowCount:Int = (data.count-1)/4 + 1
        let offh = GalleryMenuItem_H*CGFloat(rowCount) + GalleryMenuItem_H*50.0/80.0/2.0
        self.subItemContainer?.frame = CGRectMake(0, GalleryMenuBar_H, self.frame.size.width, offh)
    }
    
    // 点击二级菜单项
    func onTapItem(item:GalleryMenuItem) {
        print("----------sub menu items title:", item.titleLabel?.text)
        self.resetMenu()
        self.isExpanded = false
    }
    
    // 复位所有菜单状态
    func resetMenu(){
        for v in self.subviews{
            if let b = v as? GalleryMenuButtton{
                b.curSelected = false
            }
        }
        for v in self.subItemContainer!.subviews{
            if let b = v as? GalleryMenuItem{
                b.curSelected = false
            }
            v.removeFromSuperview()
        }
    }
}