@@ -6,16 +6,260 @@
//
# import "KBShopHeadView.h"
@ interface KBShopHeadView ( )
@ interface KBShopHeadView ( )
// 容 器 视 图 ( 留 出 与 屏 幕 的 左 右 边 距 )
@ property ( nonatomic , strong ) UIView * containerView ;
// 顶 部 左 侧 标 题 与 点 缀
@ property ( nonatomic , strong ) UILabel * titleLabel ; // "Points\nMall"
@ property ( nonatomic , strong ) UIImageView * yellowStarView ; // 左 侧 黄 色 装 饰 星 : shop_yellowxx _icon
@ property ( nonatomic , strong ) UIImageView * greenStarView ; // 右 上 角 绿 色 装 饰 星 : shop_greenxx _icon
// 右 侧 大 金 币
@ property ( nonatomic , strong ) UIImageView * bigCoinView ; // shop_headbigBg _icon
// 底 部 信 息 卡 片
@ property ( nonatomic , strong ) UIView * infoCard ; // 渐 变 浅 绿 背 景
@ property ( nonatomic , strong ) UILabel * myPointsLabel ; // "My Points"
@ property ( nonatomic , strong ) UIImageView * smallCoinView ; // 左 侧 小 金 币 : shop_jb _icon
@ property ( nonatomic , strong ) UILabel * amountLabel ; // 数 字 88.00
@ property ( nonatomic , strong ) UIButton * rechargeButton ; // 充 值 按 钮 : recharge_btn _bg
@ end
@ implementation KBShopHeadView
- ( instancetype ) initWithFrame : ( CGRect ) frame {
- ( instancetype ) initWithFrame : ( CGRect ) frame {
if ( self = [ super initWithFrame : frame ] ) {
self . backgroundColor = [ UIColor clearColor ] ;
// 组 装 视 图 层 级
[ self addSubview : self . containerView ] ;
[ self . containerView addSubview : self . titleLabel ] ;
[ self . containerView addSubview : self . yellowStarView ] ;
[ self . containerView addSubview : self . greenStarView ] ;
[ self . containerView addSubview : self . bigCoinView ] ;
[ self . containerView addSubview : self . infoCard ] ;
[ self . infoCard addSubview : self . myPointsLabel ] ;
[ self . infoCard addSubview : self . smallCoinView ] ;
[ self . infoCard addSubview : self . amountLabel ] ;
[ self . infoCard addSubview : self . rechargeButton ] ;
// 布 局
[ self . containerView mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . left . equalTo ( self ) . offset ( 16 ) ;
make . right . equalTo ( self ) . offset ( -16 ) ;
make . top . equalTo ( self ) ;
make . bottom . equalTo ( self ) ;
} ] ;
// 左 侧 标 题
[ self . titleLabel mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . right . left . equalTo ( self . containerView ) . offset ( 0 ) ;
make . bottom . equalTo ( self . containerView ) . offset ( 0 ) ;
} ] ;
// 左 侧 黄 色 装 饰 星
[ self . yellowStarView mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . centerY . equalTo ( self . titleLabel . mas_centerY ) . offset ( 6 ) ;
make . left . equalTo ( self . containerView ) . offset ( 6 ) ;
make . width . height . mas_equalTo ( 16 ) ;
} ] ;
// 右 上 角 绿 色 装 饰 星
[ self . greenStarView mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . top . equalTo ( self . containerView ) . offset ( 10 ) ;
make . right . equalTo ( self . containerView ) . offset ( -6 ) ;
make . width . height . mas_equalTo ( 14 ) ;
} ] ;
// 右 侧 大 金 币 ( 装 饰 )
[ self . bigCoinView mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . bottom . equalTo ( self . containerView ) ;
make . left . right . equalTo ( self . containerView ) ;
make . height . mas_equalTo ( KBFit ( 238 ) ) ;
} ] ;
// 底 部 信 息 卡 片
[ self . infoCard mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . left . right . equalTo ( self . containerView ) ;
make . bottom . equalTo ( self . containerView ) ;
make . height . mas_equalTo ( KBFit ( 140 ) ) ;
} ] ;
[ self . myPointsLabel mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . left . equalTo ( self . infoCard ) . offset ( 16 ) ;
make . top . equalTo ( self . infoCard ) . offset ( 16 ) ;
} ] ;
[ self . smallCoinView mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . left . equalTo ( self . infoCard ) . offset ( 16 ) ;
make . top . equalTo ( self . myPointsLabel . mas_bottom ) . offset ( 12 ) ;
make . width . height . mas_equalTo ( 36 ) ;
} ] ;
[ self . amountLabel mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . left . equalTo ( self . smallCoinView . mas_right ) . offset ( 10 ) ;
make . centerY . equalTo ( self . smallCoinView ) ;
} ] ;
[ self . rechargeButton mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . centerY . equalTo ( self . amountLabel ) ;
make . right . equalTo ( self . infoCard ) . offset ( -16 ) ;
make . width . mas_equalTo ( 120 ) ;
make . height . mas_equalTo ( 40 ) ;
} ] ;
}
return self ;
}
- ( void ) layoutSubviews {
[ super layoutSubviews ] ;
// 圆 角 ; 头 图 容 器 做 裁 剪 , 避 免 内 容 溢 出
// self . containerView . layer . cornerRadius = 16.0 ;
// self . containerView . layer . masksToBounds = YES ;
//
// self . infoCard . layer . cornerRadius = 16.0 ;
// self . infoCard . layer . masksToBounds = YES ;
//
// // 卡 片 底 色 做 一 个 很 浅 的 左 → 右 渐 变 , 贴 近 设 计 微 绿 色
// // 先 移 除 已 有 渐 变 , 避 免 多 次 叠 加
// NSMutableArray < CALayer * > * remove = [ NSMutableArray array ] ;
// for ( CALayer * l in self . infoCard . layer . sublayers ) {
// if ( [ l isKindOfClass : [ CAGradientLayer class ] ] ) { [ remove addObject : l ] ; }
// }
// for ( CALayer * l in remove ) { [ l removeFromSuperlayer ] ; }
//
// CAGradientLayer * g = [ CAGradientLayer layer ] ;
// g . colors = @ [ ( __bridge id ) [ UIColor colorWithHex : 0 xF4FFFE ] . CGColor ,
// ( __bridge id ) [ UIColor colorWithHex : 0 xE8FFF6 ] . CGColor ] ;
// g . startPoint = CGPointMake ( 0 , 0.5 ) ;
// g . endPoint = CGPointMake ( 1 , 0.5 ) ;
// g . frame = self . infoCard . bounds ;
// [ self . infoCard . layer insertSublayer : g atIndex : 0 ] ;
}
# pragma mark - Public
- ( void ) updatePoints : ( NSString * ) points {
if ( points . length = = 0 ) { points = @ "0" ; }
self . amountLabel . text = points ;
}
# pragma mark - Actions
- ( void ) onRechargeTappedAction {
if ( self . onRechargeTapped ) { self . onRechargeTapped ( ) ; }
}
# pragma mark - Lazy UI
- ( UIView * ) containerView {
if ( ! _containerView ) {
_containerView = [ UIView new ] ;
// _containerView . backgroundColor = [ UIColor whiteColor ] ;
}
return _containerView ;
}
- ( UILabel * ) titleLabel {
if ( ! _titleLabel ) {
_titleLabel = [ UILabel new ] ;
_titleLabel . numberOfLines = 0 ;
_titleLabel . text = @ "Points\nMall" ; // 两 行 展 示
_titleLabel . font = [ UIFont systemFontOfSize : 30 weight : UIFontWeightBold ] ;
_titleLabel . textColor = [ UIColor colorWithHex : 0 x1B1F1A ] ;
}
return _titleLabel ;
}
- ( UIImageView * ) yellowStarView {
if ( ! _yellowStarView ) {
_yellowStarView = [ [ UIImageView alloc ] initWithImage : [ UIImage imageNamed : @ "shop_yellowxx_icon" ] ] ;
_yellowStarView . contentMode = UIViewContentModeScaleAspectFit ;
}
return _yellowStarView ;
}
- ( UIImageView * ) greenStarView {
if ( ! _greenStarView ) {
_greenStarView = [ [ UIImageView alloc ] initWithImage : [ UIImage imageNamed : @ "shop_greenxx_icon" ] ] ;
_greenStarView . contentMode = UIViewContentModeScaleAspectFit ;
}
return _greenStarView ;
}
- ( UIImageView * ) bigCoinView {
if ( ! _bigCoinView ) {
_bigCoinView = [ [ UIImageView alloc ] initWithImage : [ UIImage imageNamed : @ "shop_headbigBg_icon" ] ] ;
_bigCoinView . contentMode = UIViewContentModeScaleAspectFit ;
}
return _bigCoinView ;
}
- ( UIView * ) infoCard {
if ( ! _infoCard ) {
_infoCard = [ UIView new ] ;
}
return _infoCard ;
}
- ( UILabel * ) myPointsLabel {
if ( ! _myPointsLabel ) {
_myPointsLabel = [ UILabel new ] ;
_myPointsLabel . text = @ "My Points" ;
_myPointsLabel . textColor = [ UIColor colorWithHex : 0 x1B1F1A ] ;
_myPointsLabel . font = [ UIFont systemFontOfSize : 18 weight : UIFontWeightSemibold ] ;
}
return _myPointsLabel ;
}
- ( UIImageView * ) smallCoinView {
if ( ! _smallCoinView ) {
_smallCoinView = [ [ UIImageView alloc ] initWithImage : [ UIImage imageNamed : @ "shop_jb_icon" ] ] ;
_smallCoinView . contentMode = UIViewContentModeScaleAspectFit ;
}
return _smallCoinView ;
}
- ( UILabel * ) amountLabel {
if ( ! _amountLabel ) {
_amountLabel = [ UILabel new ] ;
_amountLabel . text = @ "88.00" ; // 默 认 文 案
_amountLabel . textColor = [ UIColor colorWithHex : KBColorValue ] ;
_amountLabel . font = [ UIFont systemFontOfSize : 38 weight : UIFontWeightBold ] ;
_amountLabel . adjustsFontSizeToFitWidth = YES ;
_amountLabel . minimumScaleFactor = 0.7 ;
}
return _amountLabel ;
}
- ( UIButton * ) rechargeButton {
if ( ! _rechargeButton ) {
_rechargeButton = [ UIButton buttonWithType : UIButtonTypeCustom ] ;
[ _rechargeButton setTitle : @ "Recharge" forState : UIControlStateNormal ] ;
[ _rechargeButton setTitleColor : [ UIColor colorWithHex : 0 x1B1F1A ] forState : UIControlStateNormal ] ;
_rechargeButton . titleLabel . font = [ UIFont systemFontOfSize : 16 weight : UIFontWeightSemibold ] ;
// 切 图 背 景 图 ( 可 拉 伸 )
UIImage * bg = [ UIImage imageNamed : @ "recharge_btn_bg" ] ;
if ( bg ) {
CGFloat cap = 20 ; // 圆 角 拉 伸 区 域
bg = [ bg resizableImageWithCapInsets : UIEdgeInsetsMake ( cap , cap , cap , cap ) resizingMode : UIImageResizingModeStretch ] ;
[ _rechargeButton setBackgroundImage : bg forState : UIControlStateNormal ] ;
} else {
// 兜 底 : 用 绿 色 渐 变 生 成
UIImage * fallback = [ UIImage kb_gradientImageWithColors : @ [ [ UIColor colorWithHex : 0 xA6F6E9 ] , [ UIColor colorWithHex : 0 xD6FFF4 ] ]
size : CGSizeMake ( 120 , 40 )
direction : KBGradientDirectionLeftToRight ] ;
[ _rechargeButton setBackgroundImage : fallback forState : UIControlStateNormal ] ;
}
_rechargeButton . layer . cornerRadius = 20 ;
_rechargeButton . layer . masksToBounds = YES ;
[ _rechargeButton addTarget : self action : @ selector ( onRechargeTappedAction ) forControlEvents : UIControlEventTouchUpInside ] ;
}
return _rechargeButton ;
}
@ end