#import <UIKit/UIKit.h>
@class ButtonView;
@class ShareActivityView;
typedef void (^ButtonViewHandler)(ButtonView *buttonView);
@interface ShareActivityView : UIView
//背景颜色, 默认是透明度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;
//添加buttonView, HYActivityView会根据buttonView的数量自动变高, 但是没有对高度上限做过多处理,莫要丧心病狂的添加太多.
- (void)addButtonView:(ButtonView *)buttonView;
- (void)show;
- (void)hide;
@end
@interface ButtonView : UIView
@property (nonatomic, retain) UILabel *textLabel;
@property (nonatomic, retain) UIButton *imageButton;
@property (nonatomic, retain) ShareActivityView *activityView;
- (id)initWithText:(NSString *)text image:(UIImage *)image handler:(ButtonViewHandler)handler;
@end
#import "ShareActivityView.h"
#define BUTTON_VIEW_SIDE 70.0f
#define BUTTON_VIEW_FONT_SIZE 13.0f
@interface ButtonView ()
@property (nonatomic, copy) ButtonViewHandler handler;
@end
@implementation ButtonView
- (id)initWithText:(NSString *)text image:(UIImage *)image handler:(ButtonViewHandler)handler
{
self = [super init];
if (self) {
_handler = handler;
[self setup:text image:image];
}
return self;
}
- (void)setup:(NSString *)text image:(UIImage *)image
{
_textLabel = [[UILabel alloc]init];
_textLabel.text = text;
_textLabel.backgroundColor = [UIColor clearColor];
_textLabel.font = [UIFont systemFontOfSize:BUTTON_VIEW_FONT_SIZE];
_textLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:_textLabel];
_imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_imageButton setImage:image forState:UIControlStateNormal];
[_imageButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_imageButton];
// don't know why ?
self.translatesAutoresizingMaskIntoConstraints = NO;
_textLabel.translatesAutoresizingMaskIntoConstraints = NO;
_imageButton.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *constraint = nil;
NSDictionary *views = @{@"textLabel":_textLabel, @"imageButton":_imageButton};
NSArray *constraints = nil;
//view height
constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:BUTTON_VIEW_SIDE];
[self addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:BUTTON_VIEW_SIDE];
[self addConstraint:constraint];
// label
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[textLabel]|" options:0 metrics:nil views:views];
[self addConstraints:constraints];
//imageView
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[imageButton(50)]-10-|" options:0 metrics:nil views:views];
[self addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageButton(50)][textLabel]|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views];
[self addConstraints:constraints];
}
- (void)dealloc
{
[_textLabel release];
[_handler release];
[super dealloc];
}
- (void)buttonClick:(UIButton *)button
{
if (_handler) {
_handler(self);
}
if (_activityView) {
[_activityView hide];
}
}
@end
#define ICON_VIEW_HEIGHT_SPACE 8
#pragma mark ShareActivityView
@interface ShareActivityView ()
@property (nonatomic, retain) NSString *title;
//将要显示在该视图上
@property (nonatomic, retain) UIView *referView;
//内容窗口
@property (nonatomic, retain) UIView *contentView;
//透明的关闭按钮
@property (nonatomic, retain) UIButton *closeButton;
//按钮加载的view
@property (nonatomic, retain) UIView *iconView;
//button数组
@property (nonatomic, retain) NSMutableArray *buttonArray;
//行数
@property (nonatomic, assign) int lines;
//目前正在生效的numberOfButtonPerLine
@property (nonatomic, assign) int workingNumberOfButtonPerLine;
//按钮间的间隔大小
@property (nonatomic, assign) CGFloat buttonSpace;
//消失的时候移除
@property (nonatomic, retain) NSLayoutConstraint *contentViewAndViewConstraint;
//iconView高度的constraint
@property (nonatomic, retain) NSLayoutConstraint *iconViewHeightConstraint;
//buttonView的constraints
@property (nonatomic, retain) NSMutableArray *buttonConstraintsArray;
@end
@implementation ShareActivityView
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[_referView release];
[_contentView release];
[_titleLabel release];
[_iconView release];
[super dealloc];
}
- (id)initWithTitle:(NSString *)title referView:(UIView *)referView
{
self = [super init];
if (self) {
self.title = title;
if (referView) {
self.referView = referView;
}
[self setup];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(deviceRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
return self;
}
- (void)calculateButtonSpaceWithNumberOfButtonPerLine:(int)number
{
self.buttonSpace = (self.referView.bounds.size.width - BUTTON_VIEW_SIDE * number) / (number + 1);
if (self.buttonSpace < 0) {
[self calculateButtonSpaceWithNumberOfButtonPerLine:4];
} else {
self.workingNumberOfButtonPerLine = number;
}
}
- (void)setup
{
self.buttonArray = [NSMutableArray array];
self.buttonConstraintsArray = [NSMutableArray array];
self.lines = 0;
self.numberOfButtonPerLine = 4;
self.useGesturer = YES;
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f];
self.contentView = [[UIView alloc]init];
self.bgColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.95f];
[self addSubview:self.contentView];
self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.closeButton addTarget:self action:@selector(closeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.closeButton];
self.titleLabel = [[UILabel alloc]init];
self.titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font = [UIFont systemFontOfSize:17.f];
self.titleLabel.text = self.title;
[self.contentView addSubview:self.titleLabel];
self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.cancelButton setTitle:@"取 消" forState:UIControlStateNormal];
[self.cancelButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[self.cancelButton addTarget:self action:@selector(closeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.cancelButton];
self.iconView = [[UIView alloc]init];
[self.contentView addSubview:self.iconView];
self.translatesAutoresizingMaskIntoConstraints = NO;
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
self.closeButton.translatesAutoresizingMaskIntoConstraints = NO;
self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.cancelButton.translatesAutoresizingMaskIntoConstraints = NO;
self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
[self setNeedsUpdateConstraints];
//添加下滑关闭手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandler:)];
swipe.direction = UISwipeGestureRecognizerDirectionDown;
[self addGestureRecognizer:swipe];
}
- (void)updateConstraints
{
[super updateConstraints];
NSArray *constraints = nil;
NSLayoutConstraint *constraint = nil;
NSDictionary *views = @{@"contentView": self.contentView, @"closeButton": self.closeButton, @"titleLabel": self.titleLabel, @"cancelButton": self.cancelButton, @"iconView": self.iconView, @"view": self, @"referView": self.referView};
//view跟referView的宽高相等
constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.referView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
[self.referView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.referView attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
[self.referView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.referView attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
[self.referView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.referView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
[self.referView addConstraint:constraint];
//closeButton跟view的左右挨着
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[closeButton]|" options:0 metrics:nil views:views];
[self addConstraints:constraints];
//contentView跟view的左右挨着
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:views];
[self addConstraints:constraints];
//垂直方向closeButton挨着contentView
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[closeButton(==view@999)][contentView]" options:0 metrics:nil views:views];
[self addConstraints:constraints];
//titleLabel跟contentView左右挨着
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[titleLabel]|" options:0 metrics:nil views:views];
[self.contentView addConstraints:constraints];
//cancelButton跟contentView左右挨着
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[cancelButton]|" options:0 metrics:nil views:views];
[self.contentView addConstraints:constraints];
//iconView跟contentView左右挨着
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[iconView]|" options:0 metrics:nil views:views];
[self.contentView addConstraints:constraints];
//iconView的高度
if (self.iconViewHeightConstraint) {
[self.iconView removeConstraint:self.iconViewHeightConstraint];
}
self.iconViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:self.lines * BUTTON_VIEW_SIDE + (self.lines + 1) * ICON_VIEW_HEIGHT_SPACE];
[self.iconView addConstraint:self.iconViewHeightConstraint];
//垂直方向titleLabel挨着iconView挨着cancelButton
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[titleLabel(==30)]-[iconView]-[cancelButton(==30)]-8-|" options:0 metrics:nil views:views];
[self.contentView addConstraints:constraints];
}
- (void)prepareForShow
{
//计算行数
int count = [self.buttonArray count];
self.lines = count / self.workingNumberOfButtonPerLine;
if (count % self.workingNumberOfButtonPerLine != 0) {
self.lines++;
}
for (int i = 0; i < [self.buttonArray count]; i++) {
ButtonView *buttonView = [self.buttonArray objectAtIndex:i];
[self.iconView addSubview:buttonView];
int y = i / self.workingNumberOfButtonPerLine;
int x = i % self.workingNumberOfButtonPerLine;
//排列buttonView的位置
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeTop multiplier:1 constant:(y + 1) * ICON_VIEW_HEIGHT_SPACE + y * BUTTON_VIEW_SIDE];
[self.iconView addConstraint:constraint];
[self.buttonConstraintsArray addObject:constraint];
constraint = [NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeLeading multiplier:1 constant:(x + 1) * self.buttonSpace + x * BUTTON_VIEW_SIDE];
[self.iconView addConstraint:constraint];
[self.buttonConstraintsArray addObject:constraint];
}
[self layoutIfNeeded];
}
- (void)show
{
if (self.isShowing) {
return;
}
[self.referView addSubview:self];
[self setNeedsUpdateConstraints];
self.alpha = 0;
[self prepareForShow];
self.contentViewAndViewConstraint = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
[self addConstraint:self.contentViewAndViewConstraint];
[UIView animateWithDuration:0.25f animations:^{
self.alpha = 1;
[self layoutIfNeeded];
self.show = YES;
}];
}
- (void)hide
{
if (!self.isShowing) {
return;
}
[UIView animateWithDuration:0.25f animations:^{
self.alpha = 0;
[self removeConstraint:self.contentViewAndViewConstraint];
self.contentViewAndViewConstraint = nil;
[self layoutIfNeeded];
} completion:^(BOOL finished) {
self.show = NO;
[self removeFromSuperview];
}];
}
- (void)deviceRotate:(NSNotification *)notification
{
[self.iconView removeConstraints:self.buttonConstraintsArray];
[self.buttonConstraintsArray removeAllObjects];
[self calculateButtonSpaceWithNumberOfButtonPerLine:self.numberOfButtonPerLine];
[self prepareForShow];
[self.iconView removeConstraint:self.iconViewHeightConstraint];
self.iconViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:self.lines * BUTTON_VIEW_SIDE + (self.lines + 1) * ICON_VIEW_HEIGHT_SPACE];
[self.iconView addConstraint:self.iconViewHeightConstraint];
}
- (void)setNumberOfButtonPerLine:(int)numberOfButtonPerLine
{
_numberOfButtonPerLine = numberOfButtonPerLine;
[self calculateButtonSpaceWithNumberOfButtonPerLine:numberOfButtonPerLine];
}
- (void)setBgColor:(UIColor *)bgColor
{
_bgColor = bgColor;
self.contentView.backgroundColor = bgColor;
}
- (void)addButtonView:(ButtonView *)buttonView
{
[self.buttonArray addObject:buttonView];
buttonView.activityView = self;
}
- (void)closeButtonClicked:(UIButton *)button
{
[self hide];
}
- (void)swipeHandler:(UISwipeGestureRecognizer *)swipe
{
if (self.useGesturer) {
[self hide];
}
}
@end
- (void)addButtonClicked:(UIButton *)button
{
ButtonView *bv = [[ButtonView alloc]initWithText:@"新浪微博" image:[UIImage imageNamed:@"share_platform_sina"] handler:^(ButtonView *buttonView){
NSLog(@"点击新增的新浪微博");
}];
[self.activityView addButtonView:bv];
}
//链接
- (void)linkButton:(UIButton *)button
{
if (!self.activityView) {
self.activityView = [[ShareActivityView alloc]initWithTitle:@"分享到哪里" referView:self.view];
//横屏会变成一行6个, 竖屏无法一行同时显示6个, 会自动使用默认一行4个的设置.
self.activityView.numberOfButtonPerLine = 6;
ButtonView *bv = [[ButtonView alloc]initWithText:@"新浪微博" image:[UIImage imageNamed:@"share_platform_sina"] handler:^(ButtonView *buttonView){
NSLog(@"点击新浪微博");
}];
[self.activityView addButtonView:bv];
bv = [[ButtonView alloc]initWithText:@"Email" image:[UIImage imageNamed:@"share_platform_email"] handler:^(ButtonView *buttonView){
NSLog(@"点击Email");
}];
[self.activityView addButtonView:bv];
bv = [[ButtonView alloc]initWithText:@"印象笔记" image:[UIImage imageNamed:@"share_platform_evernote"] handler:^(ButtonView *buttonView){
NSLog(@"点击印象笔记");
}];
[self.activityView addButtonView:bv];
bv = [[ButtonView alloc]initWithText:@"QQ" image:[UIImage imageNamed:@"share_platform_qqfriends"] handler:^(ButtonView *buttonView){
NSLog(@"点击QQ");
}];
[self.activityView addButtonView:bv];
bv = [[ButtonView alloc]initWithText:@"微信" image:[UIImage imageNamed:@"share_platform_wechat"] handler:^(ButtonView *buttonView){
NSLog(@"点击微信");
}];
[self.activityView addButtonView:bv];
bv = [[ButtonView alloc]initWithText:@"微信圈" image:[UIImage imageNamed:@"share_platform_wechattimeline"] handler:^(ButtonView *buttonView){
// NSLog(@"添加");
// [self addButtonClicked:buttonView.imageButton];
}];
[self.activityView addButtonView:bv];
}
[self.activityView show];
}