iOS仿美团外卖饿了吗App点餐动画

效果图

前言: 在这篇文章中你可以学到什么? Masonry布局, Block 以及动画, 俗称的懒加载. (想了解的看一看). 0.-

tableViewCell布局篇--为方便大家查看, 我会尽量贴出全部代码

/**< typedef block >*/
typedef void(^btnPulsBlock)(NSInteger count, BOOL animated);
@interface XTFoodCell : UITableViewCell
@property (nonatomic, strong) UIImageView *foodImage;   // cyan
@property (nonatomic, strong) UILabel *nameLabel;       // orange
@property (nonatomic, strong) UILabel *priceLabel;      // gray
@property (nonatomic, strong) UIButton *btnMinus;       // black
@property (nonatomic, strong) UIButton *btnPlus;        // black
@property (nonatomic, strong) UILabel *orderCount;      // red
@property (nonatomic, copy)   btnPulsBlock block;       // block
@property (nonatomic, strong) UIImageView *animateView; // 购物车图标
@property (nonatomic, assign) NSInteger numCount;       // 计数器
@end

.m 实现篇

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}
- (void)createSubviews
{
    [self.contentView addSubview:self.foodImage];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.btnMinus];
    [self.contentView addSubview:self.btnPlus];
    [self.contentView addSubview:self.orderCount];
}
- (UIImageView *)foodImage
{
    if (!_foodImage) {
        _foodImage = [[UIImageView alloc] init];
    }
    return _foodImage;
}
- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
    }
    return _nameLabel;
}
- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
    }
    return _priceLabel;
}
- (UIButton *)btnMinus
{
    if (!_btnMinus) {
        _btnMinus = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return _btnMinus;
}
- (UIButton *)btnPlus
{
    if (!_btnPlus) {
        _btnPlus = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return _btnPlus;
}
- (UILabel *)orderCount
{
    if (!_orderCount) {
        _orderCount = [[UILabel alloc] init];
    }
    return _orderCount;
}

UI布局篇--Masonry

- (void)layoutSubviews
{
    [super layoutSubviews];
    [_foodImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(5.0);
        make.left.equalTo(self.contentView.mas_left).with.offset(5.0);
        make.width.equalTo(@88.0);
        make.height.equalTo(@88.0);
    }];
    self.foodImage.backgroundColor = [UIColor cyanColor];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.foodImage.mas_right).with.offset(5.0);
        make.top.equalTo(self.contentView.mas_top).with.offset(5.0);
        make.right.equalTo(self.contentView.mas_right).with.offset(-5.0);
        make.height.equalTo(@30);
    }];
    self.nameLabel.backgroundColor = [UIColor orangeColor];
    
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel);
        make.width.equalTo(@50.0);
        make.height.equalTo(@30);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-5.0);
    }];
    self.priceLabel.backgroundColor = [UIColor lightGrayColor];
    
    [_btnMinus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_nameLabel);
        make.centerY.equalTo(self.contentView);
        make.width.height.mas_equalTo(CGSizeMake(25, 25));
    }];
    self.btnMinus.backgroundColor = [UIColor blackColor];
    
    [_orderCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_btnMinus.mas_right).with.offset(10);
        make.centerY.equalTo(self.contentView);
        make.width.height.mas_equalTo(CGSizeMake(35, 25));
    }];
    self.orderCount.backgroundColor = [UIColor redColor];
    
    [self.btnPlus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_orderCount.mas_right).with.offset(10);
        make.centerY.equalTo(self.contentView);
        make.width.height.mas_equalTo(CGSizeMake(25, 25));
    }];
    self.btnPlus.backgroundColor = [UIColor blackColor];
    
    [_btnMinus setTitle:@"减" forState:UIControlStateNormal];
    [_btnMinus addTarget:self action:@selector(clickMin:) forControlEvents:UIControlEventTouchUpInside];
    _btnMinus.hidden = YES;

    [_btnPlus setTitle:@"加" forState:UIControlStateNormal];
    [_btnPlus addTarget:self action:@selector(clickPuls:) forControlEvents:UIControlEventTouchUpInside];   
}

btn点击方法--

- (void)clickPuls:(UIButton *)btn
{
    self.numCount += 1;
    self.block(self.numCount, YES);
    [self showOrderNums:self.numCount];
    
}
- (void)clickMin:(UIButton *)btn
{
    self.numCount -= 1;
    self.block(self.numCount, NO);
    [self showOrderNums:self.numCount];
}

VC篇-- 这里给出cellForRowAtIndexPath中代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XTFoodCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    // Block 回调
    __weak __typeof(&*cell) weakCell = cell;
    cell.block = ^(NSInteger nCount, BOOL boo){

        CGRect parentRect = [weakCell convertRect:weakCell.btnPlus.frame toView:self.view];
        
        if (boo) {
            // 这里是动画开始的方法
            [self joinCartAnimationWithRect:parentRect];

        }
        else
        {

        }
    };
    return cell;
}
#pragma mark -加入购物车动画
-(void) joinCartAnimationWithRect:(CGRect)rect
{
    _endPoint_x = 35;
    _endPoint_y = Screen_height - 35;
    
    CGFloat startX = rect.origin.x;
    CGFloat startY = rect.origin.y;
    
    _path= [UIBezierPath bezierPath];
    [_path moveToPoint:CGPointMake(startX, startY)];
    
    //三点曲线
    [_path addCurveToPoint:CGPointMake(_endPoint_x, _endPoint_y)
             controlPoint1:CGPointMake(startX, startY)
             controlPoint2:CGPointMake(startX - 180, startY - 200)];
    _dotLayer = [CALayer layer];
    _dotLayer.backgroundColor = [UIColor purpleColor].CGColor;
    _dotLayer.frame = CGRectMake(0, 0, 20, 20);
    _dotLayer.cornerRadius = 5;
    [self.view.layer addSublayer:_dotLayer];
    [self groupAnimation];   
}
#pragma mark - 组合动画
-(void)groupAnimation
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = _path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"alpha"];
    alphaAnimation.duration = 0.5f;
    alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    alphaAnimation.toValue = [NSNumber numberWithFloat:0.1];
    alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,alphaAnimation];
    groups.duration = 0.8f;
    groups.removedOnCompletion = NO;
    groups.fillMode = kCAFillModeForwards;
    groups.delegate = self;
    [groups setValue:@"groupsAnimation" forKey:@"animationName"];
    [_dotLayer addAnimation:groups forKey:nil];
    [self performSelector:@selector(removeFromLayer:) withObject:_dotLayer afterDelay:0.8f];   
}
- (void)removeFromLayer:(CALayer *)layerAnimation{
    
    [layerAnimation removeFromSuperlayer];
}

CAAnimationDelegate--

#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    
    if ([[anim valueForKey:@"animationName"]isEqualToString:@"groupsAnimation"]) {
        
        CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        shakeAnimation.duration = 0.25f;
        shakeAnimation.fromValue = [NSNumber numberWithFloat:0.9];
        shakeAnimation.toValue = [NSNumber numberWithFloat:1];
        shakeAnimation.autoreverses = YES;
        // 这里是下方的自定义View上面 放的btn. 自己随便定义一个 0.- 
        [_shopCartView.btnBackImg.layer addAnimation:shakeAnimation forKey:nil];
    
}

基本就是全部代码了. Demo下载
喜欢请点赞, 不喜欢请关注, 这样你就可以找到我了. @.@
说明: 以下文章均在简书平台发布. 可点击我的主页查看全部. O.-
带你系统学习GCD[一]
带你系统学习GCD[二]
Swift版本仿网易云音乐播放音乐动画效果
三分钟教你把代码托管到Github
Swift 很强大的图表库-Charts使用
Swift版仿简书App淘宝App很友好弹出view效果

---------------------------------------
走心文章, 值得点赞 ---文/夏天然后
微博@夏天是个大人了 QQQ: 498143780
---------------------------------------
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,615评论 4 59
  • 邂逅木心,是因了《从前慢》—— 记得早先少年时 大家诚诚恳恳 说一句 是一句 清早上火车站 长街黑暗无行人 卖豆浆...
    墨语花开时阅读 1,114评论 1 5
  • 看过一些泛滥的谍战剧,正义与邪恶的区分就是信仰问题,信仰坚定者的头颅是高昂的,即使死亡又如何,信仰不灭,精神长存。...
    独一无二杨柳阅读 433评论 0 0
  • 最近项目开发中遇到要复制Label上的文字的需求,经过查阅资料整理开发。 下面是自己借鉴网上各位大神的方法实现自己...
    短发控丶kk阅读 1,582评论 2 0
  • 1、财富背后,总有犯罪。-巴尔扎克 2、有个道理他早就弄清楚了,那就是你必须承受社会强加的侮辱,因为他明白,连最卑...
    弓亍阅读 435评论 0 1