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

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

本人视频教程系类   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;主要由阻性元器件内部产…

图像增强-图像锐化

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

[译]git revert

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

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;欢迎大家的关注。如想获取更多计算机二级考试的备考资料&#…

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

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

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

不畏酷暑送清风&#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麻烦的话&…

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…

Xcode 5.1 编译模拟器以及真机都能使用的静态库

Xcode 5.1.dmg 下载地址 http://pan.baidu.com/s/1jGJpKm6 1.新建 Framework & Library 工程 我起名叫ShowInfo,下面为其源码 showInfo.h #import <Foundation/Foundation.h> interface ShowInfo : NSObject (void)showInfo; end showInfo.m #import "ShowI…

UVALive 6511 Term Project

Term Project Time Limit: 3000msMemory Limit: 131072KBThis problem will be judged on UVALive. Original ID: 651164-bit integer IO format: %lld Java class name: Main解题&#xff1a;强连通分量 1 #include <bits/stdc.h>2 using namespace std;3 const in…

MATLAB混合编程视频教程下载 SIMULINK系统仿真视频

下载链接&#xff1a; http://item.taobao.com/item.htm?id43401674106 精通MATLAB混合编程视频讲解 MATLAB各类函数视频讲解 基于MATLAB的高等数学问题求解 MATLAB函数速查视频讲解 面向对象C视频教程 五朵金花&#xff0c;带你轻松搞定MATLAB 金花详情&#xff1a; 精通MA…

css z-index

为什么80%的码农都做不了架构师&#xff1f;>>> css z-index 最近在发现z-index在position"relative"的元素中会失效 去掉position属性就正常&#xff1a;z-index1 出现在 z-index999之上 记录一下供以后查看 转载于:https://my.oschina.net/livend/blog…

用c++写的一个词典工具

使用的QT图形界面&#xff0c;用libcurl获取的网页&#xff0c;在之中遇见了很多问题&#xff0c;一直想用c类封装一下libcurl,发现c很不到家啊。索性用了友元函数。 先贴上代码吧 main.cpp #include <stdio.h> #include <stdlib.h> #include <unistd.h>#in…

html5证书,1+X证书Web前端开发HTML专项练习

1 . HTML5 之前的 HTML 版本是&#xff1f; A.HTML 4.01 B.HTML 4 C.HTML 4.1 D.HTML 4.9 2 . HTML5 的正确 doctype 是&#xff1f; A.B.C.3 . 在 HTML5 中&#xff0c;哪个元素用于组合标题元素&#xff1f;A.B.C.D.4 . HTML5 中不再支持下面哪个元素&#xff1f;A.B.C.D.5 .…

PHP中封装mysql数据库链接(简单版)

为什么80%的码农都做不了架构师&#xff1f;>>> 1&#xff0c;封装mysql数据链接需要哪些属性 主机地址&#xff0c;数据库名&#xff0c;数据库密码&#xff0c;数据库名&#xff0c;字符集 2&#xff0c;利用构造方法在实例化类时&#xff0c;php底层会自动执行的…

家用台式计算机的额定功率,台式机的功率(台式电脑电源功率多大才合适?)...

台式机的功率(台式电脑电源功率多大才合适&#xff1f;)组装一台电脑&#xff0c;我们需要先挑选好硬件&#xff0c;搭配硬件最关键点就是CPU和主板兼容性&#xff0c;硬件之间的均衡性、电源功率等&#xff0c;均需要考虑周到。那么台式电脑电源功率多大才合适&#xff1f;下面…