1.先上效果图:
源码链接:https://gitee.com/xuanTestApp/TestActivityView.git
2.WHActivityView.h文件
//
// WHActivityView.h
// TestActivityView
//
// Created by weihong xuan on 2017/3/14.
// Copyright © 2017年 weihong xuan. All rights reserved.
//
#import <UIKit/UIKit.h>
@class ButtonView;
@class WHActivityView;
typedef void(^ButtonViewHandler)(ButtonView *buttonView);
@interface ButtonView : UIView
@property (nonatomic, strong) UILabel * textLabel;
@property (nonatomic, strong) UIButton * imageButton;
@property (nonatomic, weak) WHActivityView * activityView;
- (id)initWithText:(NSString *)text image:(UIImage *)image handler:(ButtonViewHandler)handler;
@end
@interface WHActivityView : UIView{
BOOL isNeed;
}
//背景颜色, 默认是透明度0.95的白色
@property (nonatomic, strong) UIColor *bgColor;
//标题
@property (nonatomic, strong) UILabel *titleLabel;
//取消按钮
@property (nonatomic, strong) UIButton *cancelButton;
//一行有多少个, 默认是4. iPhone竖屏不会多于4, 横屏不会多于6. ipad没试, 不建议ipad用这个.
@property (nonatomic, assign) int numberOfButtonPerLine;
//是否可以通过下滑手势关闭视图, 默认为YES
@property (nonatomic, assign) BOOL useGesturer;
//是否正在显示
@property (nonatomic, getter = isShowing) BOOL show;
//初始化
- (id)initWithTitle:(NSString *)title referView:(UIView *)referView isNeed:(BOOL)need;
//添加buttonView, WHActivityView会根据buttonView的数量自动变高, 但是没有对高度上限做过多处理,莫要丧心病狂的添加太多.
- (void)addButtonView:(ButtonView *)buttonView;
- (void)show;
- (void)hide;
@end
//
// WHActivityView.m
// TestActivityView
//
// Created by weihong xuan on 2017/3/14.
// Copyright © 2017年 weihong xuan. All rights reserved.
//
#import "WHActivityView.h"
#define BUTTON_VIEW_SIDE 80.f
#define BUTTON_VIEW_FONT_SIZE 12.f
#pragma mark - ButtonView
@interface ButtonView ()
@property (nonatomic, copy) NSString *text;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) ButtonViewHandler handler;
@end
@implementation ButtonView
- (id)initWithText:(NSString *)text image:(UIImage *)image handler:(ButtonViewHandler)handler
{
self = [super init];
if (self) {
self.text = text;
self.image = image;
if (handler) {
self.handler = handler;
}
[self setup];
}
return self;
}
- (void)setup
{
self.textLabel = [[UILabel alloc]init];
self.textLabel.numberOfLines=0;
self.textLabel.text = self.text;
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.font = [UIFont systemFontOfSize:BUTTON_VIEW_FONT_SIZE];
self.textLabel.textAlignment = NSTextAlignmentCenter;
self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.imageButton setImage:self.image forState:UIControlStateNormal];
[self.imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.textLabel];
[self addSubview:self.imageButton];
self.translatesAutoresizingMaskIntoConstraints = NO;
self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.imageButton.translatesAutoresizingMaskIntoConstraints = NO;
CGSize size=[self.text boundingRectWithSize:CGSizeMake(BUTTON_VIEW_SIDE, 50) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:BUTTON_VIEW_FONT_SIZE]} context:nil].size;
self.textLabel.frame=CGRectMake(0, 0, BUTTON_VIEW_SIDE, size.height);
// self.imageButton.frame=CGRectMake(0, 0, BUTTON_VIEW_SIDE, size.height);
NSLayoutConstraint *constraint = nil;
NSDictionary *views = @{@"textLabel": self.textLabel, @"imageButton": self.imageButton};
NSArray *constraints = nil;
//view的宽高为70
constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant