Skip to content

Instantly share code, notes, and snippets.

@xdream86
Created September 17, 2015 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdream86/8a82cb9f019d42c0c224 to your computer and use it in GitHub Desktop.
Save xdream86/8a82cb9f019d42c0c224 to your computer and use it in GitHub Desktop.
//
// VIPSaleView.m
// BSNBrowser
//
// Created by Jun Xia on 15/7/27.
// Copyright (c) 2015年 BlueSeaNetwork. All rights reserved.
//
#import "BSNVIPMembershipSuggestionView.h"
#import "UIView+AutoLayout.h"
#import <ILSApp/Color+Image.h>
#import "AppConstants.h"
#import "NSString+BSNSize.h"
@interface BSNVIPMembershipSuggestionView ()
@property (nonatomic, strong) UIImageView *logImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) UILabel *bodyLabel;
@property (nonatomic, strong) UIButton *submitButton;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *dontRemindMeButton;
@property (nonatomic, assign) BOOL didSetupContraints;
@property (nonatomic, copy) void (^submitButtonAction)();
@property (nonatomic, copy) void (^cancelButtonAction)();
@property (nonatomic, copy) void (^dontRemindMeButtonAction)();
@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) NSLayoutConstraint *viewHeightConstraint;
@property (nonatomic, strong) NSLayoutConstraint *subTitleTopConstraint;
@property (nonatomic, strong) NSLayoutConstraint *bodyTopConstraint;
@property (nonatomic, strong) NSLayoutConstraint *cancelButtonBottomConstraint;
@property (nonatomic, strong) NSLayoutConstraint *submitButtonBottomConstraint;
@end
@implementation BSNVIPMembershipSuggestionView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
[self addSubview:self.logImageView];
[self addSubview:self.titleLabel];
[self addSubview:self.subTitleLabel];
[self addSubview:self.bodyLabel];
[self addSubview:self.submitButton];
[self addSubview:self.cancelButton];
[self addSubview:self.dontRemindMeButton];
_isShowDontRemindMeButton = YES;
self.layer.cornerRadius = 3.0;
self.backgroundColor = [UIColor whiteColor];
self.translatesAutoresizingMaskIntoConstraints = NO;
return self;
}
- (void)show {
/**
*@see http://stackoverflow.com/questions/2508630/orientation-in-a-uiview-added-to-a-uiwindow
*/
UIWindow* window = [UIApplication sharedApplication].keyWindow ?:[[UIApplication sharedApplication].windows objectAtIndex:0];
UIView *baseView = [[window subviews] lastObject];
if (baseView.tag == kBrightnessAdjustViewTag) {
NSInteger count = window.subviews.count;
if (count > 1) {
baseView = [window.subviews objectAtIndex:count - 2];
} else {
baseView = [window.subviews firstObject];
}
}
[baseView addSubview:self.maskView];
[baseView bringSubviewToFront:self.maskView];
[self.maskView pinToSuperviewEdges:JRTViewPinAllEdges inset:0.0f];
[baseView addSubview:self];
[baseView bringSubviewToFront:self];
[self centerInView:self.superview];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
self.maskView.alpha = 0.5;
self.alpha = 0.7;
[UIView animateWithDuration:0.2 animations:^{
self.alpha = 1.0;
self.maskView.alpha = 0.5;
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.99, 0.99);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.08 animations:^{
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
}];
}];
}
- (void)dismiss {
[UIView animateWithDuration:0.08 animations:^{
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.95, 0.95);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
self.alpha = 0.0f;
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.3, 1.3);
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}];
[UIView animateWithDuration:0.4 animations:^{
self.maskView.alpha = 0.0f;
} completion:^(BOOL finished) {
[self.maskView removeFromSuperview];
}];
}
- (void)updateConstraints {
if (!self.didSetupContraints) {
self.didSetupContraints = YES;
if (kIsPhone) {
[self setupConstraintForiPhone];
} else {
[self setupConstraintForiPad];
}
}
if (kIsPhone) {
[self adjustConstraintsForiPhone];
} else {
[self adjustConstraintsForiPad];
}
[super updateConstraints];
}
- (void)setupConstraintForiPhone {
[_logImageView centerInContainerOnAxis:NSLayoutAttributeCenterX];
[_logImageView pinToSuperviewEdges:JRTViewPinTopEdge inset:30];
[_logImageView sizeToFit];
[_titleLabel pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:26];
[_titleLabel pinAttribute:NSLayoutAttributeTop toAttribute:NSLayoutAttributeBottom ofItem:_logImageView withConstant:30];
[_subTitleLabel pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:26];
self.subTitleTopConstraint = [_subTitleLabel pinAttribute:NSLayoutAttributeTop toAttribute:NSLayoutAttributeBottom ofItem:_logImageView withConstant:30];
[_bodyLabel pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:26];
self.bodyTopConstraint = [_bodyLabel pinAttribute:NSLayoutAttributeTop toAttribute:NSLayoutAttributeBottom ofItem:_logImageView withConstant:30];
[_dontRemindMeButton pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:15];
[_dontRemindMeButton constrainToHeight:14];
[_dontRemindMeButton pinToSuperviewEdges:JRTViewPinBottomEdge inset:20];
[_cancelButton pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:25];
[_cancelButton constrainToHeight:40];
_cancelButtonBottomConstraint = [_cancelButton pinAttribute:NSLayoutAttributeBottom toAttribute:NSLayoutAttributeBottom ofItem:self withConstant:-20];
[_submitButton pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:25];
[_submitButton constrainToHeight:40];
[_submitButton pinAttribute:NSLayoutAttributeBottom toAttribute:NSLayoutAttributeTop ofItem:_cancelButton withConstant:-10];
[self constrainToWidth:280];
_viewHeightConstraint = [self constrainToHeight:230];
}
- (void)setupConstraintForiPad {
[_logImageView centerInContainerOnAxis:NSLayoutAttributeCenterX];
[_logImageView pinToSuperviewEdges:JRTViewPinTopEdge inset:90];
[_logImageView sizeToFit];
[_titleLabel pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:26];
[_titleLabel pinAttribute:NSLayoutAttributeTop toAttribute:NSLayoutAttributeBottom ofItem:_logImageView withConstant:30];
[_subTitleLabel pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:26];
self.subTitleTopConstraint = [_subTitleLabel pinAttribute:NSLayoutAttributeTop toAttribute:NSLayoutAttributeBottom ofItem:_logImageView withConstant:30];
[_bodyLabel pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:100];
self.bodyTopConstraint = [_bodyLabel pinAttribute:NSLayoutAttributeTop toAttribute:NSLayoutAttributeBottom ofItem:_logImageView withConstant:30];
[_dontRemindMeButton pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:15];
[_dontRemindMeButton constrainToHeight:14];
[_dontRemindMeButton pinToSuperviewEdges:JRTViewPinBottomEdge inset:133];
[_cancelButton pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:120];
[_cancelButton constrainToHeight:40];
_cancelButtonBottomConstraint = [_cancelButton pinAttribute:NSLayoutAttributeBottom toAttribute:NSLayoutAttributeBottom ofItem:self withConstant:-133];
[_submitButton pinToSuperviewEdges:JRTViewPinLeftEdge | JRTViewPinRightEdge inset:120];
[_submitButton constrainToHeight:40];
[_submitButton pinAttribute:NSLayoutAttributeBottom toAttribute:NSLayoutAttributeTop ofItem:_cancelButton withConstant:-20];
[self constrainToWidth:545];
_viewHeightConstraint = [self constrainToHeight:411];
}
- (void)adjustConstraintsForiPhone {
if (self.isShowDontRemindMeButton) {
self.cancelButtonBottomConstraint.constant = -46;
} else {
self.cancelButtonBottomConstraint.constant = -20;
}
UIImage *logo = ILSImageDefault(@"premium_membership_popover_premium_star");
CGFloat height = 30 + logo.size.height + 30 + 20 + 40 + 10 + 40;
if (self.isShowDontRemindMeButton) {
height += 26;
}
if (self.title.length) {
height += 17;
}
if (self.subTitle.length) {
height += 14;
}
if (self.body.length) {
CGSize size = [self.body.string findHeightHavingWidth:240 andFont:[UIFont systemFontOfSize:13]];
height += size.height;
}
BOOL isTitleNotEmpty = self.title.length;
BOOL isSubtitleNotEmpty = self.subTitle.length;
BOOL isBodyNotEmpty = self.body.length;
if (!isTitleNotEmpty && !isSubtitleNotEmpty && !isBodyNotEmpty) {
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 30;
} else if (!isTitleNotEmpty && !isSubtitleNotEmpty && isBodyNotEmpty) {
height += 30;
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 30;
} else if (!isTitleNotEmpty && isSubtitleNotEmpty && !isBodyNotEmpty) {
height += 30;
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 30;
} else if (!isTitleNotEmpty && isSubtitleNotEmpty && isBodyNotEmpty) {
height += 50;
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 64;
} else if (isTitleNotEmpty && !isSubtitleNotEmpty && !isBodyNotEmpty) {
height += 30;
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 30;
} else if (isTitleNotEmpty && !isSubtitleNotEmpty && isBodyNotEmpty) {
height += 50;
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 67;
} else if (isTitleNotEmpty && isSubtitleNotEmpty && !isBodyNotEmpty) {
height += 36;
self.subTitleTopConstraint.constant = 53;
self.bodyTopConstraint.constant = 30;
} else if (isTitleNotEmpty && isSubtitleNotEmpty && isBodyNotEmpty) {
height += 56;
self.subTitleTopConstraint.constant = 53;
self.bodyTopConstraint.constant = 87;
}
self.viewHeightConstraint.constant = height;
}
- (void)adjustConstraintsForiPad {
if (self.isShowDontRemindMeButton) {
self.cancelButtonBottomConstraint.constant = -168;
} else {
self.cancelButtonBottomConstraint.constant = -131;
}
UIImage *logo = ILSImageDefault(@"premium_membership_popover_premium_star");
CGFloat height = 90 + logo.size.height + 30 + 132 + 40 + 20 + 40;
if (self.isShowDontRemindMeButton) {
height += 34;
}
if (self.title.length) {
height += 17;
}
if (self.subTitle.length) {
height += 14;
}
if (self.body.length) {
CGSize size = [self.body.string findHeightHavingWidth:336 andFont:[UIFont systemFontOfSize:13]];
height += size.height;
}
BOOL isTitleNotEmpty = self.title.length;
BOOL isSubtitleNotEmpty = self.subTitle.length;
BOOL isBodyNotEmpty = self.body.length;
if (!isTitleNotEmpty && !isSubtitleNotEmpty && !isBodyNotEmpty) {
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 30;
} else if (!isTitleNotEmpty && !isSubtitleNotEmpty && isBodyNotEmpty) {
height += 40;
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 30;
} else if (!isTitleNotEmpty && isSubtitleNotEmpty && !isBodyNotEmpty) {
height += 40;
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 30;
} else if (!isTitleNotEmpty && isSubtitleNotEmpty && isBodyNotEmpty) {
height += 80;
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 84;
} else if (isTitleNotEmpty && !isSubtitleNotEmpty && !isBodyNotEmpty) {
height += 40;
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 30;
} else if (isTitleNotEmpty && !isSubtitleNotEmpty && isBodyNotEmpty) {
height += 80;
self.subTitleTopConstraint.constant = 30;
self.bodyTopConstraint.constant = 87;
} else if (isTitleNotEmpty && isSubtitleNotEmpty && !isBodyNotEmpty) {
height += 50;
self.subTitleTopConstraint.constant = 57;
self.bodyTopConstraint.constant = 30;
} else if (isTitleNotEmpty && isSubtitleNotEmpty && isBodyNotEmpty) {
height += 90;
self.subTitleTopConstraint.constant = 57;
self.bodyTopConstraint.constant = 111;
}
self.viewHeightConstraint.constant = height;
}
- (void)setTitle:(NSString *)title {
_title = title;
self.titleLabel.text = _title;
[self layoutIfNeeded];
}
- (void)setSubTitle:(NSString *)subTitle {
_subTitle= subTitle;
self.subTitleLabel.text = _subTitle;
[self layoutIfNeeded];
}
- (void)setBody:(NSAttributedString *)body {
_body = body;
self.bodyLabel.attributedText = _body;
[self.bodyLabel sizeToFit];
[self layoutIfNeeded];
}
- (void)setIsShowDontRemindMeButton:(BOOL)isShowDontRemindMeButton {
_isShowDontRemindMeButton = isShowDontRemindMeButton;
_dontRemindMeButton.hidden = !_isShowDontRemindMeButton;
[self layoutIfNeeded];
}
- (void)setSubmitButtonTitle:(NSString *)title action:(void(^)())action {
self.submitButtonAction = [action copy];
[self.submitButton setTitle:title forState:UIControlStateNormal];
}
- (void)setCancelButtonTitle:(NSString *)title action:(void(^)())action {
self.cancelButtonAction = [action copy];
[self.cancelButton setTitle:title forState:UIControlStateNormal];
}
- (void)setDontRemindMeAgainTitle:(NSString *)title action:(void(^)())action {
self.dontRemindMeButtonAction = [action copy];
if (title.length) {
[self.dontRemindMeButton setTitle:title forState:UIControlStateNormal];
}
[self layoutIfNeeded];
}
- (void)submitButtonPressed:(id)sender {
if (self.submitButtonAction) {
self.submitButtonAction();
}
[self dismiss];
}
- (void)cancelButtonPressed:(id)sender {
self.cancelButton.layer.borderColor = [[UIColor colorWithRed:218 / 255.0 green:223 / 255.0 blue:225 / 255.0 alpha:1.0] CGColor];
if (self.cancelButtonAction) {
self.cancelButtonAction();
}
[self dismiss];
}
- (void)holdDown {
self.cancelButton.layer.borderColor = [[UIColor colorWithRed:138 / 255.0 green:152 / 255.0 blue:157 / 255.0 alpha:1.0] CGColor];
}
- (void)holdUpOutSide {
self.cancelButton.layer.borderColor = [[UIColor colorWithRed:218 / 255.0 green:223 / 255.0 blue:225 / 255.0 alpha:1.0] CGColor];
}
- (void)dontRemindMeButtonPressed:(id)sender {
if (self.dontRemindMeButtonAction) {
self.dontRemindMeButtonAction();
}
[self dismiss];
}
- (UIImageView *)logImageView {
if (!_logImageView) {
_logImageView = [UIImageView autoLayoutView];
_logImageView.image = ILSImageDefault(@"premium_membership_popover_premium_star");
}
return _logImageView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [UILabel autoLayoutView];
_titleLabel.textColor = [UIColor colorWithRed:73.0/ 255.0 green:77.0 / 255.0 blue:85.0 / 255.0 alpha:1.0];
_titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:17];
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
- (UILabel *)subTitleLabel {
if (!_subTitleLabel) {
_subTitleLabel = [UILabel autoLayoutView];
_subTitleLabel.textColor = [UIColor colorWithRed:73.0/ 255.0 green:77.0 / 255.0 blue:85.0 / 255.0 alpha:1.0];
_subTitleLabel.font = [UIFont systemFontOfSize:14];
_subTitleLabel.textAlignment = NSTextAlignmentCenter;
}
return _subTitleLabel;
}
- (UILabel *)bodyLabel {
if (!_bodyLabel) {
_bodyLabel = [UILabel autoLayoutView];
_bodyLabel.numberOfLines = 0;
_bodyLabel.textAlignment = NSTextAlignmentCenter;
_bodyLabel.font = [UIFont systemFontOfSize:13];
_bodyLabel.textColor = [UIColor colorWithRed:114 / 255.0 green:123 / 255.0 blue:142 / 255.0 alpha:1.0];
}
return _bodyLabel;
}
- (UIButton *)submitButton {
if (!_submitButton) {
_submitButton = [UIButton autoLayoutView];
_submitButton.layer.cornerRadius = 3;
_submitButton.layer.masksToBounds = YES;
UIColor *normalColorForSubmitButton = [UIColor colorWithRed: 87 / 255.0 green: 202 / 255.0 blue: 142 / 255.0 alpha:1.0];
UIImage *normalImageForSubmitButtoon = [UIImage resizableImageWithColor:normalColorForSubmitButton
size:CGSizeMake(1, 1)
caps:UIEdgeInsetsMake(0, 0, 0, 0)];
[_submitButton setBackgroundImage:normalImageForSubmitButtoon forState:UIControlStateNormal];
UIColor *hightlightColorForSubmitButton = [UIColor colorWithRed: 52 / 255.0 green: 171 / 255.0 blue: 109 / 255.0 alpha:1.0];
UIImage *hightlightImageForSubmitButtoon = [UIImage resizableImageWithColor:hightlightColorForSubmitButton
size:CGSizeMake(1, 1)
caps:UIEdgeInsetsMake(0, 0, 0, 0)];
[_submitButton setBackgroundImage:hightlightImageForSubmitButtoon forState:UIControlStateHighlighted];
[_submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_submitButton addTarget:self action:@selector(submitButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
_submitButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
}
return _submitButton;
}
- (UIButton *)cancelButton {
if (!_cancelButton) {
_cancelButton = [UIButton autoLayoutView];
_cancelButton.layer.cornerRadius = 3;
_cancelButton.layer.masksToBounds = YES;
UIImage *normalImageForCancelButton = [UIImage resizableImageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1) caps:UIEdgeInsetsMake(0, 0, 0, 0)];
[_cancelButton setBackgroundImage:normalImageForCancelButton forState:UIControlStateNormal];
UIImage *hightLightImageForCancelButton = [UIImage resizableImageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1) caps:UIEdgeInsetsMake(0, 0, 0, 0)];
[_cancelButton setBackgroundImage:hightLightImageForCancelButton forState:UIControlStateHighlighted];
_cancelButton.layer.borderColor = [[UIColor colorWithRed:218 / 255.0 green:223 / 255.0 blue:225 / 255.0 alpha:1.0] CGColor];
[_cancelButton setTitleColor:[UIColor colorWithRed:73 / 255.0 green:77 / 255.0 blue:85 / 255.0 alpha:1] forState:UIControlStateNormal];
_cancelButton.layer.borderWidth = 2;
[_cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[_cancelButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];
[_cancelButton addTarget:self action:@selector(holdUpOutSide) forControlEvents:UIControlEventTouchUpOutside];
[_cancelButton addTarget:self action:@selector(holdUpOutSide) forControlEvents:UIControlEventTouchDragExit];
_cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
}
return _cancelButton;
}
- (UIButton *)dontRemindMeButton {
if (!_dontRemindMeButton) {
_dontRemindMeButton = [UIButton autoLayoutView];
[_dontRemindMeButton setTitleColor:[UIColor colorWithRed:0 / 255.0 green:121 / 255.0 blue:255 / 255.0 alpha:1] forState:UIControlStateNormal];
_dontRemindMeButton.titleLabel.font = [UIFont systemFontOfSize:14];
[_dontRemindMeButton setTitle:NSLocalizedString(@"Don't Remind Me Again", nil) forState:UIControlStateNormal];
[_dontRemindMeButton addTarget:self action:@selector(dontRemindMeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
return _dontRemindMeButton;
}
- (UIView *)maskView {
if (!_maskView) {
_maskView = [UIView autoLayoutView];
_maskView.backgroundColor = [UIColor blackColor];
}
return _maskView;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment