弹出菜单

#import <UIKit/UIKit.h>

@interface DPopoverView : UIView

+ (void)showPopoverAtPoint:(CGPoint)point inView:(UIView *)view withContentView:(UIView *)cView;

- (void)showPopoverAtPoint:(CGPoint)point inView:(UIView *)view withContentView:(UIView *)cView;

- (void)dismiss;

@end

#import "DPopoverView.h"

@interface DPopoverView()

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, assign) CGRect boxFrame;

@end

@implementation DPopoverView

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        self.backgroundColor = [UIColor clearColor];

    }

    return self;

}

+ (void)showPopoverAtPoint:(CGPoint)point inView:(UIView *)view withContentView:(UIView *)cView

{

    DPopoverView *popView = [[DPopoverView alloc] initWithFrame:CGRectZero];

    [popView showPopoverAtPoint:point inView:view withContentView:cView];

}

- (void)showPopoverAtPoint:(CGPoint)point inView:(UIView *)view withContentView:(UIView *)cView

{

    self.boxFrame = cView.frame;

    self.contentView = cView;

    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    if(!window) {

        window = [[UIApplication sharedApplication].windows objectAtIndex:0];

    }

    UIView *topView = window;

    CGPoint topPoint = [topView convertPoint:point fromView:view];

    CGRect topViewBounds = topView.bounds;

    _contentView.frame = _boxFrame;

    _contentView.hidden = NO;

    [self addSubview:_contentView];

    

    self.layer.anchorPoint = CGPointMake(topPoint.x / topViewBounds.size.width, topPoint.y / topViewBounds.size.height);

    self.frame = topViewBounds;

    [self setNeedsDisplay];

    

    [topView addSubview:self];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];

    [self addGestureRecognizer:tap];

    self.userInteractionEnabled = YES;

    

    self.alpha = 0.f;

    self.transform = CGAffineTransformMakeScale(0.1f, 0.1f);

    

    [UIView animateWithDuration:0.2f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{

        self.alpha = 1.f;

        self.transform = CGAffineTransformMakeScale(1.05f, 1.05f);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.08f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{

            self.transform = CGAffineTransformIdentity;

        } completion:nil];

    }];

}

//#define kGradientTopColor [UIColor colorWithWhite:1.f alpha:0.95]

//#define kGradientBottomColor [UIColor colorWithWhite:0.98f alpha:0.95]

#define kGradientTopColor [UIColor clearColor]

#define kGradientBottomColor [UIColor clearColor]

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    CGRect frame = _boxFrame;

    

    float xMin = CGRectGetMinX(frame);

    float yMin = CGRectGetMinY(frame);

    

    float xMax = CGRectGetMaxX(frame);

    float yMax = CGRectGetMaxY(frame);

    

    float radius = 4.f;

    

    float cpOffset = 1.8f;

    

    UIBezierPath *popoverPath = [UIBezierPath bezierPath];

    [popoverPath moveToPoint:CGPointMake(CGRectGetMinX(frame), CGRectGetMinY(frame) + radius)];//

    [popoverPath addCurveToPoint:CGPointMake(xMin + radius, yMin) controlPoint1:CGPointMake(xMin, yMin + radius - cpOffset) controlPoint2:CGPointMake(xMin + radius - cpOffset, yMin)];//

    

    [popoverPath addLineToPoint:CGPointMake(xMax - radius, yMin)];

    [popoverPath addCurveToPoint:CGPointMake(xMax, yMin + radius) controlPoint1:CGPointMake(xMax - radius + cpOffset, yMin) controlPoint2:CGPointMake(xMax, yMin + radius - cpOffset)];//

    [popoverPath addLineToPoint:CGPointMake(xMax, yMax - radius)];

    [popoverPath addCurveToPoint:CGPointMake(xMax - radius, yMax) controlPoint1:CGPointMake(xMax, yMax - radius + cpOffset) controlPoint2:CGPointMake(xMax - radius + cpOffset, yMax)];//

    [popoverPath addLineToPoint:CGPointMake(xMin + radius, yMax)];

    [popoverPath addCurveToPoint:CGPointMake(xMin, yMax - radius) controlPoint1:CGPointMake(xMin + radius - cpOffset, yMax) controlPoint2:CGPointMake(xMin, yMax - radius + cpOffset)];//

    [popoverPath closePath];

    

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    UIColor* shadow = [UIColor colorWithWhite:0.f alpha:0.4f];

    CGSize shadowOffset = CGSizeMake(0, 1);

    CGFloat shadowBlurRadius = 10;

    

    NSArray* gradientColors = [NSArray arrayWithObjects:

                               (id)kGradientTopColor.CGColor,

                               (id)kGradientBottomColor.CGColor, nil];

    CGFloat gradientLocations[] = {0, 1};

    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)gradientColors, gradientLocations);

    

    float bottomOffset = 0.f;

    float topOffset = 0.f;

    

    CGContextSaveGState(context);

    CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow.CGColor);

    CGContextBeginTransparencyLayer(context, NULL);

    [popoverPath addClip];

    CGContextDrawLinearGradient(context, gradient, CGPointMake(CGRectGetMidX(frame), CGRectGetMinY(frame) - topOffset), CGPointMake(CGRectGetMidX(frame), CGRectGetMaxY(frame) + bottomOffset), 0);

    CGContextEndTransparencyLayer(context);

    CGContextRestoreGState(context);

    

    CGGradientRelease(gradient);

    CGColorSpaceRelease(colorSpace);

    

}

- (void)tapped:(UITapGestureRecognizer *)tap

{

    [self dismiss];

}

- (void)dismiss {

    [UIView animateWithDuration:0.3f animations:^{

        self.alpha = 0.1f;

        self.transform = CGAffineTransformMakeScale(0.1f, 0.1f);

    } completion:^(BOOL finished) {

        [self removeFromSuperview];

        

    }];

}

@end

使用示例:

{

ButtonSelectControl *buttonSelectControl_= [[ButtonSelectControl alloc] initWithFrame:CGRectMake(ScreenWidth - 80, [Utility getNavBarHight],80, 80) direction:YES];

buttonSelectControl_.delegate = self;

buttonSelectControl_.normalColor = [UIColor colorWithRed:103/255.0f green:103/255.0f blue:103/255.0f alpha:1.0f];

buttonSelectControl_.selectColor = [UIColor whiteColor];

buttonSelectControl_.fSelFontSize = 12;

[buttonSelectControl_ setBackgroundColor:MainTitleBgColor];

CGFloat width, hight;

width = buttonSelectControl_.frame.size.width;

hight = buttonSelectControl_.frame.size.height /3;

UIEdgeInsets edgeinset =UIEdgeInsetsMake(0,0,0,15);

UIButton *btn0 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];

btn0.imageEdgeInsets = edgeinset;

[btn0 setImage:[UIImage imageNamed:@"二维码-up"] forState:UIControlStateNormal];

[btn0 setImage:[UIImage imageNamed:@"二维码-down"] forState:UIControlStateHighlighted];

[btn0 setTitle:@"扫一扫" forState:UIControlStateNormal];

[btn0 setTitle:@"扫一扫" forState:UIControlStateHighlighted];

[btn0.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];

btn0.tag = MV_Btn_QRCode_tag;

[buttonSelectControl_ addButton:btn0];

UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0,40,

CGRectGetWidth(btn0.frame),

CGRectGetHeight(btn0.frame))];

[btn1 setImage:[UIImage imageNamed:@"ic_setting_up"] forState:UIControlStateNormal];

btn1.imageEdgeInsets = edgeinset;

[btn1 setTitle:@"设置" forState:UIControlStateNormal];

[btn1 setTitle:@"设置" forState:UIControlStateHighlighted];

[btn1.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];

btn1.tag =MV_Btn_setting_tag;

[buttonSelectControl_ addButton:btn1];

DPopoverView *popoverView1 = [[DPopoverView alloc] initWithFrame:CGRectZero];

popoverView = popoverView1;

[popoverView1 showPopoverAtPoint:CGPointMake(ScreenWidth - 20, [Utility getNavBarHight]/2) inView:self.view withContentView:buttonSelectControl_];

} 

原文地址:https://www.cnblogs.com/ldc529/p/3862360.html