1. 程式人生 > >分享彈出介面

分享彈出介面

1.先上效果圖:

                                                                                     

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>

@classButtonView;

@classWHActivityView;

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

3.WHActivityView.m檔案

//

//  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

@interfaceButtonView ()

@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 = [[UILabelalloc]init];

self.textLabel.numberOfLines=0;

self.textLabel.text = self.text;

self.textLabel.backgroundColor = [UIColorclearColor];

self.textLabel.font = [UIFontsystemFontOfSize:BUTTON_VIEW_FONT_SIZE];

self.textLabel.textAlignment = NSTextAlignmentCenter;

self.imageButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [self.imageButtonsetImage:self.imageforState:UIControlStateNormal];

    [self.imageButtonaddTarget:selfaction:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [selfaddSubview:self.textLabel];

    [selfaddSubview:self.imageButton];

self.translatesAutoresizingMaskIntoConstraints = NO;

self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;

self.imageButton.translatesAutoresizingMaskIntoConstraints = NO;

CGSize size=[self.textboundingRectWithSize:CGSizeMake(BUTTON_VIEW_SIDE, 50) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize: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 = [NSLayoutConstraintconstraintWithItem:selfattribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:1constant:90];

    [self addConstraint:constraint];

    constraint = [NSLayoutConstraintconstraintWithItem:selfattribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:1constant:BUTTON_VIEW_SIDE];

    [self addConstraint:constraint];

//label緊貼view的左右

    constraints = [NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[textLabel]|"options:0metrics:nilviews:views];

    [self addConstraints:constraints];

//imageView距離view左右各10, imageView的寬為50

    constraints = [NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-13-[imageButton(50)]-7-|"options:0metrics:nilviews:views];

    [self addConstraints:constraints];

//豎直方向imageView和textLabel在一條直線上, 並且挨著, imageView的高為50

    constraints = [NSLayoutConstraintconstraintsWithVisualFormat:@"V:|[imageButton(50)]-0-[textLabel]|"options:NSLayoutFormatAlignAllCenterXmetrics:nilviews:views];

    [self addConstraints:constraints];

}

- (void)buttonClicked:(UIButton *)button

{

    if (self.handler) {

        self.handler(self);

    }

    else

    {

        return;

    }

if (self.activityView) {

        [self.activityViewhide];

    }

}

@end

#define ICON_VIEW_HEIGHT_SPACE 8

#pragma mark - HYActivityView

@interface WHActivityView ()

@property (nonatomic, copy) NSString *title;

//將要顯示在該檢視上

@property (nonatomic, weak) UIView *referView;

//內容視窗

@property (nonatomic, strong) UIView *contentView;

//透明的關閉按鈕

@property (nonatomic, strong) UIButton *closeButton;

//按鈕載入的view

@property (nonatomic, strong) UIView *iconView;

//button陣列

@property (nonatomic, strong) NSMutableArray *buttonArray;

//行數

@property (nonatomic, assign) int lines;

//目前正在生效的numberOfButtonPerLine

@property (nonatomic, assign) int workingNumberOfButtonPerLine;

//按鈕間的間隔大小

@property (nonatomic, assign) CGFloat buttonSpace;

//消失的時候移除

@property (nonatomic, strong) NSLayoutConstraint *contentViewAndViewConstraint;

//iconView高度的constraint

@property (nonatomic, strong) NSLayoutConstraint *iconViewHeightConstraint;

//buttonView的constraints

@property (nonatomic, strong) NSMutableArray *buttonConstraintsArray;

@end

@implementation WHActivityView

- (void)dealloc

{

    [[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIDeviceOrientationDidChangeNotificationobject:nil];

}

- (id)initWithTitle:(NSString *)title referView:(UIView *)referView isNeed:(BOOL)need

{

    self = [super init];

    if (self) {

        self.title = title;

        if (referView) {

            self.referView = referView;

        }

        isNeed=need;

        [self setup];

        [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(deviceRotate:) name:UIDeviceOrientationDidChangeNotificationobject:nil];

    }

return self;

}

- (void)calculateButtonSpaceWithNumberOfButtonPerLine:(int)number

{

    self.buttonSpace = (self.referView.bounds.size.width - BUTTON_VIEW_SIDE * number) / (number + 1);

    if (self.buttonSpace < 0) {

        [selfcalculateButtonSpaceWithNumberOfButtonPerLine:4];

    } else {

self.workingNumberOfButtonPerLine = number;

    }

}

- (void)setup

{

self.buttonArray = [NSMutableArrayarray];

self.buttonConstraintsArray = [NSMutableArrayarray];

    self.lines = 0;

self.numberOfButtonPerLine = 4;

self.useGesturer = YES;

//遮蓋的背景顏色

self.backgroundColor = [UIColorcolorWithRed:0green:0blue:0alpha:0.6f];

self.contentView = [[UIViewalloc]init];

//容器的背景顏色

self.bgColor = [UIColorcolorWithRed:242/255.0green:242/255.0blue:242/255.0alpha:1.0f];

    [selfaddSubview:self.contentView];

self.closeButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [self.closeButtonaddTarget:selfaction:@selector(closeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [selfaddSubview:self.closeButton];

    //標題

self.titleLabel = [[UILabelalloc]init];

self.titleLabel.backgroundColor = [UIColorclearColor];

self.titleLabel.textAlignment = NSTextAlignmentCenter;

self.titleLabel.font = [UIFontsystemFontOfSize:17.f];

self.titleLabel.text = self.title;

    [self.contentViewaddSubview:self.titleLabel];

//取消按鈕

self.cancelButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

self.cancelButton.backgroundColor=[UIColorwhiteColor];

    [self.cancelButtonsetTitle:@"取消"forState:UIControlStateNormal];

    [self.cancelButtonsetTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    [self.cancelButtonaddTarget:selfaction:@selector(closeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.contentViewaddSubview:self.cancelButton];

self.iconView = [[UIViewalloc]init