iOS-Masonry

視圖佈局:ide

(一)整個界面佈局採用masonry自適應佈局,也可供學習使用; 佈局

(二)項目中所須要的界面佈局,用於新手指引,此視圖用於分步顯示新手指引,如下的代碼是實現一具體的步驟,佈局界面中有operButton,用於執行按鈕操做,以此定義- (void)configViewWithBlock:(void (^)())excuteCode方法;配置button點擊事件執行操做;NTImageViewType標記圖片是在左仍是在右;學習

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger ,NTImageViewType) {
    
    NTImageViewTypeLeft = 0,
    NTImageViewTypeRight
};

@interface NTGuideView : UIView

@property (nonatomic, assign) CGFloat subLabelFontSize;
@property (nonatomic, assign) CGFloat operButtonTitleFontSize;
@property (nonatomic, assign) BOOL isTopImageViewCenter;

- (instancetype)initWithImgaeName:(NSString *)imageName topImageName:(NSString *)topImageName subTitle:(NSString *)subTitle labelTexts:(NSArray *)labelTexts buttonTitle:(NSString *)buttonTitle imageViewType:(NTImageViewType)imageViewType;

- (void)configViewWithBlock:(void (^)())excuteCode;

@end


#import "NTGuideView.h"
#import "UIImage+extension.h"

@interface NTGuideView () {
    
    UIImageView *topImgaeView;
    UIImageView *contentImageView;
    
    UIView *leftLayoutView;
    UIView *rightLayoutView;
    
    UILabel *subTitleLabel;
    UIButton *operButton;
    UIView *lastView;
}

@property (nonatomic, strong) NSMutableArray *subLabels;
@property (nonatomic, strong) NSMutableArray *pointAtLabels;
@property (nonatomic, copy) void(^excuteCode)();

@end

@implementation NTGuideView

- (instancetype)initWithImgaeName:(NSString *)imageName topImageName:(NSString *)topImageName subTitle:(NSString *)subTitle labelTexts:(NSArray *)labelTexts buttonTitle:(NSString *)buttonTitle imageViewType:(NTImageViewType)imageViewType {
    
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        _subLabels = [NSMutableArray array];
        _pointAtLabels = [NSMutableArray array];
        
        [self createViewWithImageName:imageName topImageName:topImageName labelTexts:labelTexts subTitle:subTitle buttonTitle:buttonTitle];
        [self configView:imageViewType];
    }
    
    return self;
}

- (void)createViewWithImageName:(NSString *)imageName topImageName:(NSString *)topImageName labelTexts:(NSArray *)labelTexts subTitle:(NSString *)subTitle buttonTitle:(NSString *)buttonTitle {
    
    topImgaeView = [UIImageView new];
    topImgaeView.image = [UIImage nt_imageNamed:topImageName];
    [self addSubview:topImgaeView];
    
    
    leftLayoutView = [UIView new];
    leftLayoutView.backgroundColor = [UIColor whiteColor];
    [self addSubview:leftLayoutView];
    
    
    rightLayoutView = [UIView new];
    rightLayoutView.backgroundColor = [UIColor whiteColor];
    [self addSubview:rightLayoutView];
    
    
    contentImageView = [UIImageView new];
    contentImageView.image = [UIImage nt_imageNamed:imageName];
    [leftLayoutView addSubview:contentImageView];
    
    if (subTitle) {
        
        subTitleLabel = [UILabel new];
        subTitleLabel.text = subTitle;
        subTitleLabel.backgroundColor = [UIColor whiteColor];
        subTitleLabel.textColor = [UIColor colorWithHex:0x0096ff];
        [rightLayoutView addSubview:subTitleLabel];
    }
    
    if (labelTexts) {
        
        [labelTexts enumerateObjectsUsingBlock:^(NSString *objSubTitle, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UILabel *subLabel = [UILabel new];
            subLabel.text = objSubTitle;
            subLabel.numberOfLines = 0;
            subLabel.backgroundColor = [UIColor whiteColor];
            [rightLayoutView addSubview:subLabel];
            
            [_subLabels addObject:subLabel];
            
            
            UIView *pointViewAtLabel = [UIView new];
            pointViewAtLabel.backgroundColor = [UIColor colorWithHex:0x0096ff];
            pointViewAtLabel.layer.cornerRadius = 2.0f;
            [rightLayoutView addSubview:pointViewAtLabel];
            
            [_pointAtLabels addObject:pointViewAtLabel];
        }];
    }
    
    if (buttonTitle) {
        
        operButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self configViewWithButtonTitle:buttonTitle];
        [rightLayoutView addSubview:operButton];
    }
}

- (void)configView:(NTImageViewType)imageViewType {
    
    [topImgaeView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(@20);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
        make.height.mas_equalTo(@40);
    }];
    
    
    if (imageViewType == NTImageViewTypeLeft) {
        
        [leftLayoutView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(topImgaeView.mas_bottom).offset(20);
            make.left.equalTo(self);
            make.width.equalTo(self).multipliedBy(0.5);
        }];
        
        [rightLayoutView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(leftLayoutView.mas_right);
            make.centerY.equalTo(leftLayoutView);
            make.width.equalTo(self).multipliedBy(0.5);
        }];
        
    } else if (imageViewType == NTImageViewTypeRight) {
        
        [leftLayoutView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(topImgaeView.mas_bottom).offset(20);
            make.right.equalTo(self);
            make.width.equalTo(self).multipliedBy(0.5);
        }];
        
        [rightLayoutView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(leftLayoutView.mas_left);
            make.centerY.equalTo(leftLayoutView);
            make.width.equalTo(self).multipliedBy(0.5);
        }];
    }
    
    [contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(@140);
        make.height.mas_equalTo(@220);
        make.center.equalTo(leftLayoutView);
    }];
    
    [leftLayoutView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(contentImageView.mas_bottom);
    }];
    
    
    if (subTitleLabel) {
        
        [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(rightLayoutView).offset(10);
            make.left.equalTo(rightLayoutView).offset(24);
            make.right.equalTo(rightLayoutView).offset(-10);
        }];
    }
    
    if (_subLabels.count > 0) {
        
        [_subLabels enumerateObjectsUsingBlock:^(UILabel *objSubLabel, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UIView *pointViewAtLabel = _pointAtLabels[idx];
            if (pointViewAtLabel) {
                
                [pointViewAtLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    
                    make.left.equalTo(rightLayoutView).offset(10);
                    make.width.and.height.mas_equalTo(@4);
                    
                    if (lastView) {
                        
                        make.top.equalTo(lastView.mas_bottom).offset(6);
                        
                    } else {
                        
                        if (subTitleLabel) {
                            
                            make.top.equalTo(subTitleLabel.mas_bottom).offset(10);
                            
                        } else {
                            
                            make.top.equalTo(rightLayoutView).offset(10);
                        }
                    }
                }];
            }
            
            [objSubLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.left.equalTo(pointViewAtLabel.mas_right).offset(10);
                make.right.equalTo(rightLayoutView).offset(-10);
                
                if (lastView) {
                    
                    make.top.equalTo(lastView.mas_bottom).offset(2);
                    
                } else {
                    
                    make.top.equalTo(pointViewAtLabel).offset(-4);
                }
                
                lastView = objSubLabel;
            }];
        }];
    }
    
    if (operButton) {
        
        [operButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(lastView.mas_bottom).offset(10);
            make.left.equalTo(lastView);
            make.right.equalTo(rightLayoutView).offset(-10);
            make.height.mas_equalTo(@35);
        }];
        
        [rightLayoutView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(operButton.mas_bottom).offset(10);
        }];
        
    } else {
        
        [rightLayoutView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(lastView.mas_bottom).offset(10);
        }];
    }
    
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(leftLayoutView.mas_bottom);
    }];
}

- (void)configViewWithButtonTitle:(NSString *)buttonTitle {
    
    [operButton setTitle:buttonTitle forState:UIControlStateNormal];
    operButton.backgroundColor = [UIColor colorWithHex:0x0096ff];
    [operButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.operButtonTitleFontSize = 16.0f;
    [operButton addTarget:self action:@selector(clickOperButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setSubLabelFontSize:(CGFloat)subLabelFontSize {
    
    if (subLabelFontSize > 0) {
        
        [_subLabels enumerateObjectsUsingBlock:^(UILabel *objSubLabel, NSUInteger idx, BOOL * _Nonnull stop) {
            
            objSubLabel.font = [UIFont systemFontOfSize:subLabelFontSize];
        }];
    }
}

- (void)setOperButtonTitleFontSize:(CGFloat)operButtonTitleFontSize {
    
    if (operButtonTitleFontSize) {
        
        operButton.titleLabel.font = [UIFont systemFontOfSize:operButtonTitleFontSize];
    }
}

- (void)clickOperButton:(UIButton *)sender {
    
    if (_excuteCode) {
        
        _excuteCode();
    }
}

- (void)configViewWithBlock:(void (^)())excuteCode {
    
    self.excuteCode = excuteCode;
}

- (void)setIsTopImageViewCenter:(BOOL)isTopImageViewCenter {
    
    if (isTopImageViewCenter) {
        
        [topImgaeView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(@20);
            make.centerX.equalTo(self);
            make.width.mas_equalTo(@38);
            make.height.mas_equalTo(@52);
        }];
    }
}
@end
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息