CustomDropMenuView.m 4.83 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
//
//  CustomDropMenuView.m
//  redstar
//
//  Created by admin on 15/10/25.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "CustomDropMenuView.h"

@interface CustomDropMenuView ()
/**
 *  将来用来显示具体内容的容器
 */
@property (nonatomic, weak) UIView *showView;
@end

@implementation CustomDropMenuView

// 便利构造器
+ (instancetype)defaultMenuView
{
    return [[self alloc] init];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 清除颜色
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

/**
 *  重写setter方法
 */
- (void)setContent:(UIView *)content
{
    _content = content;
    
    // 调整内容的位置
    content.x = 10;
    content.y = 15;

    // 添加内容到灰色图片中
    [self.showView addSubview:content];
    
    // 顶端
    NSLayoutConstraint *contentTop = [NSLayoutConstraint constraintWithItem:content attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.showView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    [self.showView addConstraint:contentTop];
    
    // 左边
    NSLayoutConstraint *contentLeft = [NSLayoutConstraint constraintWithItem:content attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.showView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    [self.showView addConstraint:contentLeft];
    
    // 右边
    NSLayoutConstraint *contentRight = [NSLayoutConstraint constraintWithItem:content attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.showView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    [self.showView addConstraint:contentRight];
    
    // 高度
    NSLayoutConstraint *contentBottom = [NSLayoutConstraint constraintWithItem:content attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.showView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self.showView addConstraint:contentBottom];
    
}

- (void)setContentController:(UIViewController *)contentController
{
    _contentController = contentController;
    self.content = contentController.view;
}

/**
 *  显示
 */
- (void)showFrom:(UIView *)from
{
    // 1.获得最上面的窗口
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    
    // 2.添加自己到窗口上
    [window addSubview:self];
    
    // 3.设置尺寸
    self.frame = window.bounds;
    
    // 通知外界,自己显示了
    if ([self.delegate respondsToSelector:@selector(dropdownMenuDidShow:)]) {
        [self.delegate dropdownMenuDidShow:self];
    }
}

/**
 *  销毁
 */
- (void)dismiss
{
    [self removeFromSuperview];
    
    // 通知外界,自己被销毁了
    if ([self.delegate respondsToSelector:@selector(dropdownMenuDidDismiss:)]) {
        [self.delegate dropdownMenuDidDismiss:self];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}

- (UIView *)showView
{
    if (!_showView) {
        // 添加一个灰色图片控件
        UIView *showView = [[UIView alloc] init];
        showView.backgroundColor = [UIColor clearColor];
        showView.userInteractionEnabled = YES; // 开启交互
        showView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:showView];
        
        
        // 顶端
124
        NSLayoutConstraint *showTop = [NSLayoutConstraint constraintWithItem:showView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.showTop];
admin's avatar
admin committed
125 126 127
        [self addConstraint:showTop];
        
        // 左边
128
        NSLayoutConstraint *showViewLeft = [NSLayoutConstraint constraintWithItem:showView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:self.showLeft];
admin's avatar
admin committed
129 130 131
        [self addConstraint:showViewLeft];
        
        // 右边
132
        NSLayoutConstraint *showViewRight = [NSLayoutConstraint constraintWithItem:showView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:self.showRight];
admin's avatar
admin committed
133 134 135
        [self addConstraint:showViewRight];
        
        // 高度
136
        NSLayoutConstraint *showViewHeight = [NSLayoutConstraint constraintWithItem:showView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:self.showHeight];
admin's avatar
admin committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        [showView addConstraint:showViewHeight];
        
        self.showView = showView;

    }
    
    
    return _showView;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end