Assets.xcassets图片是拖到右边里面去的。
//
// ViewController.m
// 03-综合练习
//
#import "ViewController.h"
@interface ViewController ()
// 购物车
@property (weak, nonatomic) IBOutlet UIView *shopCarView;
// 添加按钮
@property (weak, nonatomic) IBOutlet UIButton *addButton;
// 删除按钮
@property (weak, nonatomic) IBOutlet UIButton *removeButton;
// 全局的下标
//@property (nonatomic, assign) NSInteger index;
@implementation ViewController
// // 裁剪多余部分(不可取)
// self.shopCarView.clipsToBounds = YES;
}
/**
* 添加到购物车
*
* @param button 按钮
*/
/***********************2.创建一个商品*****************************/
// 1.创建商品的view
UIView *shopView = [[UIView alloc] init];
// 2.设置frame
shopView.frame = CGRectMake(x, y, width, height);
// 3.设置背景颜色
shopView.backgroundColor = [UIColor greenColor];
// 4.添加到购物车
[self.shopCarView addSubview:shopView];
/***********************3.设置按钮的状态*****************************/
// if (index == 5) {
// button.enabled = NO;
// }
button.enabled = (index != );
// 5.设置删除按钮的状态
self.removeButton.enabled = YES;
// 让下标+1
// self.index += 1;
}
/**
* 从购物车中删除
*
* @param button 按钮
*/
(IBAction)remove:(UIButton *)button {
// 1. 删除最后一个商品
UIView *lastShopView = [self.shopCarView.subviews lastObject];
[lastShopView removeFromSuperview];
// 2.设置索引值 -1
// self.index -= 1;
// 3. 设置添加按钮的状态
self.addButton.enabled = YES;
// 4. 设置删除按钮的状态
/*
if (self.shopCarView.subviews.count == 0) {
self.removeButton.enabled = NO;
}
*/
self.removeButton.enabled = (self.shopCarView.subviews.count != );
}
@end
手机扫一扫
移动阅读更方便
你可能感兴趣的文章