前言:

适用场景:商城类的 App 。将自定义的购物车 view 设置为 navigationItem 的导航栏按钮。效果图如下:
图1、右上角的购物车即是我们定义的view
实现原理:
1、利用 navigationItem 可以将 UIView 设置为导航栏的按钮;
2、将一个 UIButton 和 一个 UILabel 添加到一个 UIView 上。然后将这个 UIView 设置为 navigationItem 的右侧按钮;3、UILabel 控件的动画效果。
具体实现代码如下:
1、ShopCarView.h 文件
#import @protocol ShopCarButtonDelegate <</span>NSObject> // 代理的方法,在此方法内,完成按钮的点击事件。 - (void)shopCarButtonClickAction; @end @interfaceShopCarView : UIView @property (nonatomic, assign)id<</span>ShopCarButtonDelegate> delegate; // 为购物车设置角标内数值 - (void)setShopCarCount:(NSString *)count; @end
2、ShopCarView.m 文件
#import "ShopCarView.h"
@interfaceShopCarView()
@property (nonatomic, strong)UIButton *carButton;
@property (nonatomic, strong)UILabel *countLabel;
@end
@implementation ShopCarView
- (instancetype)initWithFrame:(CGRect)frame{
CGRect myFrame = CGRectMake(0, 0, 40, 40);
self = [superinitWithFrame:myFrame];
if (self) {
[selfaddSubview:self.carButton];
}
returnself;
}
- (UIButton *)carButton{
if (!_carButton) {
_carButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
_carButton.frame = CGRectMake(0, 8, 32, 32);
[_carButtonsetImage:[UIImageimageNamed:@"购物1"] forState:UIControlStateNormal];
[_carButtonaddTarget:selfaction:@selector(shopCarButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
return_carButton;
}
- (UILabel *)countLabel{
if (!_countLabel) {
_countLabel = [[UILabelalloc] initWithFrame:CGRectMake(24, 5, 16, 16)];
_countLabel.backgroundColor = [UIColorredColor];
_countLabel.textAlignment = NSTextAlignmentCenter;
_countLabel.textColor = [UIColorwhiteColor];
_countLabel.layer.cornerRadius = 8;
_countLabel.font = [UIFontsystemFontOfSize:12];
_countLabel.layer.masksToBounds = YES;
[selfaddSubview:_countLabel];
}
return_countLabel;
}
// 为购物车设置角标内数值
- (void)setShopCarCount:(NSString *)count{
if ([count integerValue] == 0) {
if (_countLabel) {
[_countLabelremoveFromSuperview];
_countLabel = nil;
}
return;
}
if ([count integerValue] > 9) {
self.countLabel.text = @"9+";
}else{
self.countLabel.text = count;
}
[selfshakeView:_countLabel];
}
// 实现的代理方法
- (void)shopCarButtonAction{
[self.delegateshopCarButtonClickAction];
}
// 实现抖动效果
-(void)shakeView:(UIView*)viewToShake
{
CGFloat t =2.0;
CGAffineTransform translateRight =CGAffineTransformTranslate(CGAffineTransformIdentity, t,0.0);
CGAffineTransform translateLeft =CGAffineTransformTranslate(CGAffineTransformIdentity,-t,0.0);
viewToShake.transform = translateLeft;
[UIViewanimateWithDuration:0.07delay:0.0options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeatanimations:^{
[UIViewsetAnimationRepeatCount:2.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished){
if(finished){
[UIViewanimateWithDuration:0.05delay:0.0options:UIViewAnimationOptionBeginFromCurrentStateanimations:^{
viewToShake.transform =CGAffineTransformIdentity;
} completion:NULL];
}
}];
}
@end
代码很简单,逻辑也比较清晰。使用代理方法,将自定义的 View 的属性隐藏起来,打到很好的封装效果。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
# ios
# 购物车角标
# 角标
# iOS添加购物车动画效果示例
# IOS购物车界面实现效果示例
# iOS实现电商购物车界面示例
# iOS搭建简易购物车页面
# 购物车
# 设置为
# 自定义
# 很好
# 在此
# 很简单
# 打到
# 即是
# 角标内
# delegate
# id
# count
# NSString
# setShopCarCount
# shopCarButtonClickAction
# void
# NSObject
# interfaceShopCarView
# assign
# nonatomic
相关文章:
,怎么在广州志愿者网站注册?
如何续费美橙建站之星域名及服务?
如何注册花生壳免费域名并搭建个人网站?
建站之星伪静态规则如何正确配置?
网站制作公司,橙子建站是合法的吗?
大连网站制作公司哪家好一点,大连买房网站哪个好?
如何在Golang中使用encoding/gob序列化对象_存储和传输数据
C++时间戳转换成日期时间的步骤和示例代码
如何选择长沙网站建站模板?H5响应式与品牌定制哪个更优?
北京制作网站的公司排名,北京三快科技有限公司是做什么?北京三快科技?
建站之星如何配置系统实现高效建站?
江苏网站制作公司有哪些,江苏书法考级官方网站?
python的本地网站制作,如何创建本地站点?
如何在Windows服务器上快速搭建网站?
如何有效防御Web建站篡改攻击?
无锡制作网站公司有哪些,无锡优八网络科技有限公司介绍?
如何在云服务器上快速搭建个人网站?
MySQL查询结果复制到新表的方法(更新、插入)
网站网页制作电话怎么打,怎样安装和使用钉钉软件免费打电话?
如何使用Golang安装API文档生成工具_快速生成接口文档
一键网站制作软件,义乌购一件代发流程?
香港服务器建站指南:外贸独立站搭建与跨境电商配置流程
定制建站模板如何实现SEO优化与智能系统配置?18字教程
建站之星如何通过成品分离优化网站效率?
建站之星Pro快速搭建教程:模板选择与功能配置指南
如何通过西部建站助手安装IIS服务器?
已有域名能否直接搭建网站?
如何配置FTP站点权限与安全设置?
Python多线程使用规范_线程安全解析【教程】
深圳网站制作设计招聘,关于服装设计的流行趋势,哪里的资料比较全面?
jQuery 常见小例汇总
如何通过PHP快速构建高效问答网站功能?
已有域名如何快速搭建专属网站?
如何实现建站之星域名转发设置?
潮流网站制作头像软件下载,适合母子的网名有哪些?
陕西网站制作公司有哪些,陕西凌云电器有限公司官网?
如何快速搭建高效简练网站?
惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?
如何快速搭建高效WAP手机网站?
建站之星收费标准详解:套餐费用及年费价格表一览
如何快速搭建响应式可视化网站?
黑客如何通过漏洞一步步攻陷网站服务器?
网站制作公司排行榜,抖音怎样做个人官方网站
建站之星后台密码遗忘?如何快速找回?
如何用腾讯建站主机快速创建免费网站?
如何处理“XML格式不正确”错误 常见XML well-formed问题解决方法
已有域名建站全流程解析:网站搭建步骤与建站工具选择
建站之星导航配置指南:自助建站与SEO优化全解析
北京网站制作公司哪家好一点,北京租房网站有哪些?
沈阳个人网站制作公司,哪个网站能考到沈阳事业编招聘的信息?
*请认真填写需求信息,我们会在24小时内与您取得联系。