山寨“饿了么”应用中添加菜品数量按钮效果

山寨“饿了么”应用中添加菜品数量按钮效果

本人视频教程系类   iOS中CALayer的使用

最终效果:

山寨源头:

源码:(此源码解决了重用问题,可以放心的放在cell中使用

AddAndDeleteButton.h 与 AddAndDeleteButton.m

//
//  AddAndDeleteButton.h
//  LabelControll
//
//  Created by YouXianMing on 14/12/11.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//#import <UIKit/UIKit.h>typedef enum : NSUInteger {CNY, // 人民币GBP, // 英镑JPY, // 日元USD, // 美元
} EnumMoneyType;@protocol AddAndDeleteButtonDelegate <NSObject>
@optional
- (void)currentCount:(NSNumber *)count;
@end@interface AddAndDeleteButton : UIView@property (nonatomic, weak)    id<AddAndDeleteButtonDelegate> delegate;/***  数目(数目为0就会隐藏)*/
@property (nonatomic, strong) NSNumber *count;/***  单价(商品单价)*/
@property (nonatomic, strong) NSNumber *price;/***  设置数目**  @param count   数目*  @param animted 时候执行动画*/
- (void)setCount:(NSNumber *)count animated:(BOOL)animted;/***  起始值**  @param count 值*/
- (void)startValue:(NSNumber *)count;@end


//
//  AddAndDeleteButton.m
//  LabelControll
//
//  Created by YouXianMing on 14/12/11.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//#import "AddAndDeleteButton.h"typedef enum : NSUInteger {UIBUTTON_ADD = 10,UIBUTTON_DELETE,
} EnumAddAndDeleteButton;// 控件总体的宽度
static CGFloat width          = 150;
// 控件总体的高度
static CGFloat height         = 28;
// 添加按钮的宽度
static CGFloat addButtonWidth = 60;
// 控件之间的间隙
static CGFloat gap             = 7;static CGFloat label_10_99       = 5;
static CGFloat label_100_999     = 10;// 隐藏位置的frame值(后面要用)
static CGRect  hidenRect;  // 0static CGRect  labelRect;  // 1 - 9
static CGRect  deleteRect; // 1 - 9static CGRect  labelRect_10_99;  // 10 - 99
static CGRect  deleteRect_10_99; // 10 - 99static CGRect  labelRect_100_999;  // 100 - 999
static CGRect  deleteRect_100_999; // 100 - 999@interface AddAndDeleteButton ()@property (nonatomic, strong) UIView    *backedView;@property (nonatomic, strong) UIButton  *addButton;    // 添加的按钮@property (nonatomic, strong) UILabel   *countLabel;   // 计数的标签@property (nonatomic, strong) UIButton  *deleteButton; // 删除的按钮@end@implementation AddAndDeleteButton+ (void)initialize {if (self == [AddAndDeleteButton class]) {// 0时候的frame值hidenRect  = CGRectMake(width - height, 0, height, height);// 1到9的frame值labelRect  = CGRectMake(width - addButtonWidth - gap - height, 0, height, height);deleteRect = CGRectMake(width - addButtonWidth - (gap + height)*2, 0, height, height);// 10到99的frame值labelRect_10_99 = CGRectMake(width - addButtonWidth - gap - (height + label_10_99), 0,height + label_10_99, height);deleteRect_10_99 = CGRectMake(width - addButtonWidth - (gap + height) - (gap + height + label_10_99), 0,height, height);// 100到999的frame值labelRect_100_999 = CGRectMake(width - addButtonWidth - gap - (height + label_100_999), 0,height + label_100_999, height);deleteRect_100_999 = CGRectMake(width - addButtonWidth - (gap + height) - (gap + height + label_100_999), 0,height, height);}
}- (instancetype)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {// 添加背景图层_backedView                   = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];[self addSubview:_backedView];// 计数的标签_countLabel = [[UILabel alloc] initWithFrame:CGRectMake(width - addButtonWidth - gap - height, 0, height, height)];_countLabel.backgroundColor     = [UIColor whiteColor];_countLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;_countLabel.layer.borderWidth   = 1.f;_countLabel.layer.cornerRadius  = 4.f;_countLabel.layer.masksToBounds = YES;_countLabel.layer.borderColor   = [UIColor colorWithRed:0.898 green:0.898 blue:0.902 alpha:1].CGColor;_countLabel.text                = @"0";_countLabel.textAlignment       = NSTextAlignmentCenter;_countLabel.textColor           = [UIColor colorWithRed:0.945 green:0.102 blue:0.325 alpha:1];[self addSubview:_countLabel];// 删除按钮_deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(width - addButtonWidth - (gap + height)*2, 0, height, height)];_deleteButton.backgroundColor     = [UIColor colorWithRed:0.792 green:0.796 blue:0.800 alpha:1];_deleteButton.tag                 = UIBUTTON_DELETE;[_deleteButton addTarget:selfaction:@selector(buttonsEvent:)forControlEvents:UIControlEventTouchUpInside];_deleteButton.layer.cornerRadius  = 4.f;_deleteButton.layer.masksToBounds = YES;[self addSubview:_deleteButton];// 添加按钮_addButton = [[UIButton alloc] initWithFrame:CGRectMake(width - addButtonWidth, 0, addButtonWidth, height)];[_addButton setTitle:@"$10.00" forState:UIControlStateNormal];[_addButton addTarget:selfaction:@selector(buttonsEvent:)forControlEvents:UIControlEventTouchUpInside];_addButton.tag = UIBUTTON_ADD;[_addButton setTitleColor:[UIColor colorWithRed:0.957 green:0.984 blue:0.949 alpha:1]forState:UIControlStateNormal];_addButton.titleLabel.font    = [UIFont systemFontOfSize:16.f];_addButton.layer.cornerRadius = 4.f;_addButton.backgroundColor    = [UIColor colorWithRed:0.475 green:0.796 blue:0.329 alpha:1];[self addSubview:_addButton];}return self;
}- (void)buttonsEvent:(UIButton *)button {if (button.tag == UIBUTTON_ADD) {[self setCount:@(self.count.intValue + 1) animated:YES];} else if (button.tag == UIBUTTON_DELETE) {[self setCount:@(self.count.intValue - 1) animated:YES];}if (_delegate && [_delegate respondsToSelector:@selector(currentCount:)]) {[_delegate currentCount:self.count];}
}- (void)startValue:(NSNumber *)count {if (count.integerValue == 0) {self.count = count;_countLabel.frame = hidenRect;_countLabel.alpha = 0.f;_countLabel.text  = @"0";_deleteButton.frame = hidenRect;_deleteButton.alpha = 0.f;return;}if (count.integerValue >= 1 && count.integerValue <= 9) {self.count = count;_countLabel.frame = labelRect;_countLabel.alpha = 1.f;_countLabel.text  = count.stringValue;_deleteButton.frame = deleteRect;_deleteButton.alpha = 1.f;return;}if (count.integerValue >= 10 && count.integerValue <= 99) {self.count = count;_countLabel.frame = labelRect_10_99;_countLabel.alpha = 1.f;_countLabel.text  = count.stringValue;_deleteButton.frame = deleteRect_10_99;_deleteButton.alpha = 1.f;return;}if (count.integerValue >= 100 && count.integerValue <= 999) {self.count = count;_countLabel.frame = labelRect_100_999;_countLabel.alpha = 1.f;_countLabel.text  = count.stringValue;_deleteButton.frame = deleteRect_100_999;_deleteButton.alpha = 1.f;return;}
}- (void)setCount:(NSNumber *)count animated:(BOOL)animted {if (count.intValue == 1000) {return;}_count = count;// 设置数为0而且标签上的值为1时(从1减到0的情况)   1 --> 0if (count.intValue == 0 && _countLabel.text.intValue == 1) {if (animted) {[UIView animateWithDuration:0.35f animations:^{_countLabel.frame = hidenRect;_countLabel.alpha = 0.f;_countLabel.text  = @"0";_deleteButton.frame = hidenRect;_deleteButton.alpha = 0.f;}];} else {_countLabel.frame = hidenRect;_countLabel.alpha = 0.f;_countLabel.text  = @"0";_deleteButton.frame = hidenRect;_deleteButton.alpha = 0.f;}return;}// 设置数目为1而且标签上的值为0时(从0加到1的情况) 0 --> 1if (count.intValue == 1 && _countLabel.text.intValue == 0) {if (animted) {[UIView animateWithDuration:0.35f animations:^{_countLabel.frame   = labelRect;_countLabel.alpha   = 1.f;_countLabel.text    = @"1";_deleteButton.frame = deleteRect;_deleteButton.alpha = 1.f;}];} else {_countLabel.frame   = labelRect;_countLabel.alpha   = 1.f;_countLabel.text    = @"1";_deleteButton.frame = deleteRect;_deleteButton.alpha = 1.f;}return;}// 设置数目从9到10时候的动画   9 --> 10if (count.intValue == 10 && _countLabel.text.intValue == 9) {if (animted) {[UIView animateWithDuration:0.35f animations:^{_countLabel.frame   = labelRect_10_99;_countLabel.alpha   = 1.f;_countLabel.text    = @"10";_deleteButton.frame = deleteRect_10_99;_deleteButton.alpha = 1.f;}];} else {_countLabel.frame   = labelRect_10_99;_countLabel.alpha   = 1.f;_countLabel.text    = @"10";_deleteButton.frame = deleteRect_10_99;_deleteButton.alpha = 1.f;}return;}// 设置数目从9到10时候的动画  10 --> 9if (count.intValue == 9 && _countLabel.text.intValue == 10) {if (animted) {[UIView animateWithDuration:0.35f animations:^{_countLabel.frame   = labelRect;_countLabel.alpha   = 1.f;_countLabel.text    = @"9";_deleteButton.frame = deleteRect;_deleteButton.alpha = 1.f;}];} else {_countLabel.frame   = labelRect;_countLabel.alpha   = 1.f;_countLabel.text    = @"9";_deleteButton.frame = deleteRect;_deleteButton.alpha = 1.f;}return;}// 99 --> 100if (count.intValue == 100 && _countLabel.text.intValue == 99) {if (animted) {[UIView animateWithDuration:0.35f animations:^{_countLabel.frame   = labelRect_100_999;_countLabel.alpha   = 1.f;_countLabel.text    = @"100";_deleteButton.frame = deleteRect_100_999;_deleteButton.alpha = 1.f;}];} else {_countLabel.frame   = labelRect_100_999;_countLabel.alpha   = 1.f;_countLabel.text    = @"100";_deleteButton.frame = deleteRect_100_999;_deleteButton.alpha = 1.f;}return;}// 100 --> 99if (count.intValue == 99 && _countLabel.text.intValue == 100) {if (animted) {[UIView animateWithDuration:0.35f animations:^{_countLabel.frame   = labelRect_10_99;_countLabel.alpha   = 1.f;_countLabel.text    = @"99";_deleteButton.frame = deleteRect_10_99;_deleteButton.alpha = 1.f;}];} else {_countLabel.frame   = labelRect_10_99;_countLabel.alpha   = 1.f;_countLabel.text    = @"99";_deleteButton.frame = deleteRect_10_99;_deleteButton.alpha = 1.f;}return;}// 11 - 98if (count.intValue >= 10 && count.intValue <= 99) {_countLabel.frame   = labelRect_10_99;_countLabel.text    = count.stringValue;_deleteButton.frame = deleteRect_10_99;return;}// 2 --> 8if (count.intValue >= 1 && count.intValue <= 9) {_countLabel.frame   = labelRect;_countLabel.text    = count.stringValue;_deleteButton.frame = deleteRect;return;}if (count.intValue >= 100 && count.intValue <= 999) {_countLabel.frame   = labelRect_100_999;_countLabel.text    = count.stringValue;_deleteButton.frame = deleteRect_100_999;return;}
}@end

使用源码:
    AddAndDeleteButton *button = [[AddAndDeleteButton alloc] initWithFrame:CGRectMake(100, 0, 150, 28)];[button startValue:@(0)];button.transform           = CGAffineTransformScale(button.transform, 1.5, 1.5);button.center              = self.view.center;[self.view addSubview:button];

控制器源码:
//
//  ViewController.m
//  LabelControll
//
//  Created by YouXianMing on 14/12/11.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//#import "ViewController.h"
#import "AddAndDeleteButton.h"
#import "YXCell.h"static NSString *test = @"YouXianMing";@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, YXCellDelegate>@property (nonatomic, strong) UITableView    *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];_dataArray = [[NSMutableArray alloc] init];for (int i = 0; i < 20; i++) {[_dataArray addObject:@(i)];}_tableView            = [[UITableView alloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];_tableView.delegate   = self;_tableView.dataSource = self;[self.view addSubview:_tableView];[_tableView registerClass:[YXCell class] forCellReuseIdentifier:test];
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return 20;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {YXCell *cell = [tableView dequeueReusableCellWithIdentifier:test];cell.selectionStyle = UITableViewCellSelectionStyleNone;cell.delegate = self;[cell.button startValue:_dataArray[indexPath.row]];return cell;
}- (void)currentCount:(NSNumber *)count cell:(YXCell *)cell {NSIndexPath *path = [_tableView indexPathForCell:cell];[_dataArray replaceObjectAtIndex:path.row withObject:count];
}@end


//
//  YXCell.h
//  LabelControll
//
//  Created by YouXianMing on 14/12/11.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//#import <UIKit/UIKit.h>
#import "AddAndDeleteButton.h"@class YXCell;@protocol YXCellDelegate <NSObject>
@optional
- (void)currentCount:(NSNumber *)count cell:(YXCell *)cell;
@end@interface YXCell : UITableViewCell@property (nonatomic, weak)    id<YXCellDelegate>  delegate;@property (nonatomic, strong)  AddAndDeleteButton *button;@end


//
//  YXCell.m
//  LabelControll
//
//  Created by YouXianMing on 14/12/11.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//#import "YXCell.h"@interface YXCell ()<AddAndDeleteButtonDelegate>@end@implementation YXCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {_button = [[AddAndDeleteButton alloc] initWithFrame:CGRectMake(100, 0, 150, 28)];_button.delegate = self;[_button startValue:@(0)];[self addSubview:_button];}return self;
}- (void)currentCount:(NSNumber *)count {if (_delegate && [_delegate respondsToSelector:@selector(currentCount:cell:)]) {[_delegate currentCount:count cell:self];}
}@end

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/458826.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html间数据传送,Express框架与html之间如何进行数据传递(示例代码)

关于Node.js 的Express框架介绍&#xff0c;推荐看菜鸟教程的Express框架&#xff0c;很适合入门&#xff0c;这里不再赘述&#xff0c;这里主要讲一下Express框架与html之间如何进行数据传递我采用的是JQuery的Ajax()向后台传参方式(url传参)1、Type属性为Get时&#xff1a;(1…

数字图像去噪典型算法及matlab实现

图像去噪是数字图像处理中的重要环节和步骤。去噪效果的好坏直接影响到后续的图像处理工作如图像分割、边缘检测等。图像信号在产生、传输过程中都可能会受到噪声的污染&#xff0c;一般数字图像系统中的常见噪声主要有&#xff1a;高斯噪声&#xff08;主要由阻性元器件内部产…

pat1100. Mars Numbers (20)

1100. Mars Numbers (20) 时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard 作者CHEN, YuePeople on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars.The numbers 1 to 12 on Earch is called "jan, feb, …

【USACO1.1】Broken Necklace

题意 一个环形项链&#xff0c;有rbw三种珠子&#xff0c;r代表red&#xff0c;b代表blue&#xff0c;w代表white&#xff0c;从任意一个位置断开&#xff0c;两端分别取珠子&#xff0c;同一端取的珠子要相同颜色&#xff0c;w可以染成想要的颜色&#xff0c;即既可当作r也可以…

html+注释格式化,使用xml注释来生成格式化的html输出

我试图从我在xml文件中的注释中生成一个格式良好的html文档。目前我有一个xml文件&#xff0c;用于生成xml表格的html列表。为了让我添加有关表格的评论&#xff0c;我手动将注释添加到输出html文件中。使用xml注释来生成格式化的html输出我想如果可能将html代码放在xml文件中作…

图像增强-图像锐化

图像锐化主要影响图像中的低频分量&#xff0c;不影响图像中的高频分量。 图像锐化的主要目的有两个&#xff1a; 1.增强图像边缘&#xff0c;使模糊的图像变得更加清晰&#xff0c;颜色变得鲜明突出&#xff0c;图像的质量有所改善&#xff0c;产生更适合人眼观察和识别的图像…

[译]git revert

git revert git revert用来撤销一个已经提交了的快照. 但不是从项目历史中移除这个commit, 而是生成一个新的commit, 老的commit还是保留在历史项目里面的. 这样做的好处是防止了项目丢失历史. 用法 git revert <commit>生成一个新的commit, 撤销老的<commit>的所有…

图像二值化算法总结

****************************************************************************************************************************************** 红&#xff1a;数字图像处理视频教程&#xff08;两部&#xff09; {中科院版36讲视频教程 电子科大版70讲视频教程&#xff…

html 替换反斜杠,在URL直接替换反斜杠反斜杠

我们有一个系统&#xff0c;基于Moodle的平台&#xff0c;在这里的文件是这样引用&#xff1a;在URL直接替换反斜杠反斜杠的http&#xff1a;// [服务器] /file.php/3/LR4/info/ index.html的现在&#xff0c;这个伟大的工程&#xff0c;但是我们的一些老师错误地使用落后的斜杠…

VMware桥接模式无法连网

2019独角兽企业重金招聘Python工程师标准>>> #VMware桥接模式无法连网 在VMware上装了个CentOS7&#xff0c;使用桥接模式连网&#xff0c;开始使用的时候没有问题&#xff0c;可以正常上网。最近打开的时候发现上不了网了&#xff0c; 使用ifconfig查看也没有分配到…

Java 7 中 NIO.2 的使用——第四节 文件和目录

Files类提供了很多方法用于检查在于你真正实际去操作一个文件或目录。这些方法强烈推荐&#xff0c;也非常有用&#xff0c;也能避免很多异常的发生。例如&#xff0c;一个很好的习惯就是在你试着移动一个文件从一个地方到另一个地方的时候&#xff0c;先检查文件是否存在。 检…

计算机二级access知识点6,2019年计算机二级ACCESS考试知识点:关系数据模型

【导语】2019年计算机二级考试备考正在进行中&#xff0c;为了方便考生及时有效的备考&#xff0c;那么&#xff0c;无忧考网为您精心整理了2019年计算机二级ACCESS考试知识点&#xff1a;关系数据模型&#xff0c;欢迎大家的关注。如想获取更多计算机二级考试的备考资料&#…

乘方取模计算(模幂计算)

乘方取模计算也称为模幂计算&#xff0c;在密码系统中经常使用&#xff0c;是不可缺少的。 使用本程序可以解HDU2035&#xff0c;只需要考虑输入和输出。 /** 乘方取模** 已知给定的正整数a、n和m&#xff0c;计算x的值&#xff0c;a^n x (mod m)。** 二分法用在这里也很有效果…

Moldflow中文版注塑流动分析案例导航视频教程

http://item.taobao.com/item.htm?spma1z10.5.w4002-9510581626.18.30lDTO&id43054534418 QQ&#xff1a;2911984429 http://aidem.lingw.net/

Jaxb annotation使用

JAXB&#xff08;Java Architecture for XML Binding) 是一个业界的标准&#xff0c;是一项可以根据XML Schema产生Java类的技术。该过程中&#xff0c;JAXB也提供了将XML实例文档反向生成Java对象树的方法&#xff0c;并能将Java对象树的内容重新写到XML实例文档。从另一方面来…

湖北大学计算机袁云,暑期走访不停歇 远赴异地送关怀——学校慰问离退休教职工和校友...

不畏酷暑送清风&#xff0c;心常为老怀关爱。7月至8月&#xff0c;正值高温时节&#xff0c;校领导和各单位负责人根据学校党委的安排&#xff0c;赴深圳、广州、北京、上海等地走访慰问70岁以上离退休教职工和部分校友&#xff0c;把学校的问候和祝福送到他们身边。“对老同志…

MATLAB各类函数详细讲解 simulike系统仿真分析

http://item.taobao.com/item.htm?spma230r.1.14.40.yWjJFw&id43113292964&ns1&abbucket2&_uk10ekfuf6120#detail Matlab基本操作函数 SIMULINK仿真函数 插值与拟合函数视频教程 符号运算函数视频教程 概率统计函数视频教程 级数与微积分函数视频教程 矩阵运…

Github Coding Developer Book For LiuGuiLinAndroid

Github Coding Developer Book For LiuGuiLinAndroid 收集了这么多开源的PDF&#xff0c;也许会帮到一些人&#xff0c;现在里面的书籍还不是很多&#xff0c;我也在一点点的上传&#xff0c;才上传不到一半&#xff0c;没办法&#xff0c;库存太多了 觉得全部pull麻烦的话&…

#个人博客作业week2——结对编程伙伴代码复审

General 1.程序能够顺利地运行。程序通过命令行输入&#xff0c;能够向对应的文件中输出符合要求的题目和答案。程序能够根据用户的不同选择&#xff0c;进行题目的生产或答案的校验&#xff0c;生成出的题目符合参数要求和项目的查重等各种要求&#xff0c;答案校验准确迅速。…

Linux设备驱动程序(第三版)/深入理解计算机系统(原书第2版)/[Android系统原理及开发要点详解].(韩超,梁泉)百度云盘下载

文档下载云盘连接&#xff1a;http://pan.baidu.com/s/1dDD2sgT 更多其他资料&#xff0c;请关注淘宝&#xff1a;http://shop115376623.taobao.com/ http://item.taobao.com/item.htm?spma230r.1.14.3.ArS64K&id43025290175&ns1&abbucket2&_uk10ekfuf6187#d…