IOS 弹出式 POPMenuView

//MenuView.h

 

//

//  MenuView.h

//  RockPopMenu

//

//  Created by zhuang chaoxiao on 14-6-26.

//  Copyright (c) 2014年 zhuang chaoxiao. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

 

@protocol MenuViewDelegate

 

@optional

 

 

@end

 

 

@interface MenuView : UIView

{

    NSArray * _itemArray;

    UIView * _itemBackView;

}

 

@property(nonatomic,weak) id<MenuViewDelegate> delegate;

 

 

+(id)shareMenuView;

 

 

+(void)showPopView:(UIView *)parentView withRect:(CGRect)rect withMenuItem:(NSArray*)array;

 

@end

 

/////////////////////////////////////////////////////////////////////////////////////////////////

 

@interface MyMenuItem : NSObject

{

    //NSString * _strTitle;

    //UIImage * _iconImg;

}

 

-(void)performAction;

 

+(id)initMenuItemWithTitle:(NSString*)title withIcon:(UIImage*)img withTarget:(id)target withSelector:(SEL)selector;

 

@property(nonatomic,strongNSString * strTitle;

@property(nonatomic,strongUIImage * iconImg;

@property(nonatomic,weak) id target;

@property(nonatomic) SEL action;

 

@end

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//MenuView.m
 

//

//  MenuView.m

//  RockPopMenu

//

//  Created by zhuang chaoxiao on 14-6-26.

//  Copyright (c) 2014年 zhuang chaoxiao. All rights reserved.

//

 

#import "MenuView.h"

 

#define ITEM_MARGIN_X 5.0f

#define ITEM_MARGIN_Y 5.0f

#define ITEM_IMAGE_WIDTH 28.0f

#define ITEM_IMAGE_HEIGHT 28.0f

#define ITEM_TEXT_WIDHT 100.0f

#define ITEM_TEXT_HEIGHT 25.0f

#define ITEM_WIDTH 145.0f

#define ITEM_HEIGHT 40.0f

 

 

 

 

 

@implementation MyMenuItem

 

 

+(id)initMenuItemWithTitle:(NSString*)title withIcon:(UIImage*)img withTarget:(id)target withSelector:(SEL)selector

{

    return [[MyMenuItem alloc]initWithTitle:title withIcon:img withTarget:target withSelector:selector];

}

 

-(void)performAction

{

    if( _target && [_target respondsToSelector:_action] )

    {

        [_target performSelectorOnMainThread:_action withObject:self waitUntilDone:YES];

    }

}

 

-(id)initWithTitle:(NSString*)title withIcon:(UIImage*)img withTarget:(id)target withSelector:(SEL)selector

{

    self = [super init];

    

    if(self)

    {

        _strTitle = title;

        _iconImg = img;

        _target = target;

        _action = selector;

    }

    

    return self;

}

 

 

@end

 

 

 

////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 

@implementation MenuView

 

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        

        self.frame = [[UIScreen mainScreen] bounds];

        

    }

    return self;

}

 

 

-(CGRect)layoutMenuItem:(UIView * )parentView

{

    CGRect rect;

    CGFloat yPos = 0;

    int btnTag = 0;

    

    UIView * contentView = [[UIView alloc]initWithFrame:CGRectZero];

    

    for( MyMenuItem * item in _itemArray )

    {

        UIButton * btnView = [[UIButton alloc]initWithFrame:CGRectZero];

        btnView.frame = CGRectMake(0, yPos, ITEM_WIDTH, ITEM_HEIGHT);

        [contentView addSubview:btnView];

        

        if( item.iconImg )

        {

            UIImageView * imgView = [[UIImageView alloc]initWithImage:item.iconImg];

            imgView.frame = CGRectMake(ITEM_MARGIN_X, ITEM_MARGIN_Y, ITEM_IMAGE_WIDTH, ITEM_IMAGE_HEIGHT);

            [btnView addSubview:imgView];

        }

        

        UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(ITEM_MARGIN_X*2+ITEM_IMAGE_WIDTH, ITEM_MARGIN_Y, ITEM_TEXT_WIDHT, ITEM_TEXT_HEIGHT)];

        lab.text = item.strTitle;

        lab.textColor = [UIColor greenColor];

        [btnView addSubview:lab];

        

        btnView.tag = btnTag;

        [btnView addTarget:self action:@selector(performAction:) forControlEvents:UIControlEventTouchUpInside];

        

        ++ btnTag;

        yPos += ITEM_HEIGHT;

    }

    

    rect = CGRectMake(0, 0, ITEM_WIDTH, yPos);

    contentView.frame = rect;

    

    [parentView addSubview:contentView];

    

    

    return rect;

}

 

 

-(void)performAction:(id)sender

{

    UIButton *btn = (UIButton*)sender;

    

    MyMenuItem * item = _itemArray[btn.tag];

    [item performAction];

    

    [self dismissPopView];

}

 

 

-(void)dismissPopView

{

    [UIView animateWithDuration:0.5f animations:^(void){

        self.alpha = 0.0f;

        

    }completion:^(BOOL finished){

        self.alpha = 1.0f;

        [self removeFromSuperview];

    }];

    

}

 

-(void)showPopView:(UIView *)parentView withRect:(CGRect)rect withMenuItem:(NSArray*)array

{

    self.backgroundColor = [UIColor clearColor];

 

    [parentView addSubview:self];

    

    _itemArray = array;

    

    _itemBackView = [[UIView alloc]initWithFrame:CGRectMake(100100100100)];

    _itemBackView.backgroundColor = [UIColor grayColor];

     [self addSubview:_itemBackView];

    

    

    CGRect frame = [self layoutMenuItem:_itemBackView];

    _itemBackView.frame = CGRectMake(_itemBackView.frame.origin.x_itemBackView.frame.origin.y, frame.size.width, frame.size.height);

    _itemBackView.layer.cornerRadius = 10;

}

 

 

+(void)showPopView:(UIView *)parentView withRect:(CGRect)rect withMenuItem:(NSArray*)array

{

    [[self shareMenuView] showPopView:parentView withRect:rect withMenuItem:array];

}

 

 

+(id)shareMenuView

{

    static MenuView * menu;

    static dispatch_once_t once;

    

    dispatch_once(&once,^{

        

        menu = [[MenuView alloc]init];

        

    });

    

    return menu;

}

 

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/

@end

 

////////////////////////////////////////////////////////////////////////////////////////////

//调用

-(void)click

{

    NSLog(@"click");

    

    NSArray * arr= @[

                     [MyMenuItem initMenuItemWithTitle:@"点击查看详情" withIcon:[UIImage imageNamed:@"home_icon"] withTarget:self withSelector:(@selector(action1))],

                     [MyMenuItem initMenuItemWithTitle:@"明日天气预报" withIcon:[UIImage imageNamed:@"home_icon"] withTarget:self withSelector:(@selector(action2))],

                     [MyMenuItem initMenuItemWithTitle:@"中国人民银行" withIcon:[UIImage imageNamed:@"home_icon"] withTarget:self withSelector:(@selector(action1))],

                     ];

    

    [MenuView showPopView:self.view withRect:CGRectZero withMenuItem:arr];

}

 

-(void)action1

{

    NSLog(@"action1");

}

 

-(void)action2

{

    NSLog(@"action2");

 

}

本文转自  张江论坛 http://www.999dh.net/home.php?mod=space&uid=1&do=blog&id=205 转自请标注,谢谢~~~

IOS 弹出式 POPMenuView,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。