1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//
// ProductDetailsViewController.m
// Lighting
//
// Created by 曹云霄 on 16/5/4.
// Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//
#import "ProductDetailsViewController.h"
#import "ProductDetailsTableViewCell.h"
#import "ProductDetailsHeaderView.h"
#import "goodsDetailsSectionview.h"
#import "goodsDetailsTableViewCell.h"
#import "FullScreenViewController.h"
@interface ProductDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic,strong) ProductDetailsHeaderView *headerView;
/**
* 商品详情数据
*/
@property (nonatomic,strong )TOGoodsEntity *entity;
/**
* 图文详情数据源
*/
@property (nonatomic,strong) NSMutableArray *goodsDetailsArray;
/**
* 商品小图数据源
*/
@property (nonatomic,strong) NSMutableArray *imagesArray;
/**
* 点击小图的下标 默认0
*/
@property (nonatomic,assign) NSInteger imageIndex;
@end
@implementation ProductDetailsViewController
/**
* 初始化图文详情
*/
- (NSMutableArray *)goodsDetailsArray
{
if (_goodsDetailsArray == nil) {
_goodsDetailsArray = [NSMutableArray array];
}
return _goodsDetailsArray;
}
/**
* 商品小图数据源
*/
- (NSMutableArray *)imagesArray
{
if (_imagesArray == nil) {
_imagesArray = [NSMutableArray array];
}
return _imagesArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self uiConfigAction];
[self getGoodsListDetails];
}
#pragma mark -UI
- (void)uiConfigAction
{
self.imageIndex = 0;
self.productDetilsTableview.dataSource = self;
self.productDetilsTableview.delegate = self;
[self.productDetilsTableview registerNib:[UINib nibWithNibName:@"goodsDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:@"goodsdetailscell"];
[self CreateHeaderView];
}
#pragma mark -获取商品详情
- (void)getGoodsListDetails
{
[self CreateMBProgressHUDLoding];
NSString *url = [NSString stringWithFormat:@"%@%@",SERVERREQUESTURL(GOODSDETAILS),_goodsID];
[[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:url WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
[self RemoveMBProgressHUDLoding];
if ([returnValue[@"code"] isEqualToNumber:@0]) {
self.entity = [[TOGoodsEntity alloc]initWithDictionary:returnValue[@"data"] error:nil];
[self HeaderViewAssignment];
}
else
{
[self ErrorMBProgressView:returnValue[@"message"]];
}
} WithErrorCodeBlock:^(id errorCodeValue) {
} WithFailureBlock:^(id error) {
[self RemoveMBProgressHUDLoding];
}];
}
#pragma mark -头部视图
- (void)CreateHeaderView
{
self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"ProductDetailsHeaderView" owner:self options:nil] lastObject];
self.headerView.goodsNumber.delegate = self;
//增加,减少商品
[self.headerView.reduceButton addTarget:self action:@selector(reduceGoodsButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.headerView.addButton addTarget:self action:@selector(reduceGoodsButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//放大
[self.headerView.amplificationButton addTarget:self action:@selector(amplificationButtonClick) forControlEvents:UIControlEventTouchUpInside];
self.productDetilsTableview.tableHeaderView = self.headerView;
//添加至购物车
[self.headerView.addGoodsShoppingbagsButton addTarget:self action:@selector(addGoodsShoppingbags:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark -header赋值
- (void)HeaderViewAssignment
{
self.headerView.serialNumber.text = self.entity.code;
self.headerView.nameLabe.text = self.entity.name;
self.headerView.brandName.text = self.entity.company;
self.headerView.inventoryNumber.text = [NSString stringWithFormat:@"%ld",self.inventory];
self.headerView.dorpPriceLabe.text = [NSString stringWithFormat:@"¥%.2f",[self.entity.tagPrice floatValue]];
self.imagesArray = [NSMutableArray arrayWithArray:[self.entity.pictures componentsSeparatedByString:@","]];
[self.headerView.goodsImageview sd_setImageWithURL:[self.imagesArray firstObject] placeholderImage:REPLACEIMAGE];
//商品小图
self.imagesArray = [NSMutableArray arrayWithArray:[self.entity.pictures componentsSeparatedByString:@","]];
for (int i=0; i<self.imagesArray.count; i++) {
UIImageView *subImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, i*100, 80, 60)];
subImageview.tag = 100+i;
subImageview.userInteractionEnabled = YES;
[subImageview sd_setImageWithURL:[NSURL URLWithString:[self.imagesArray objectAtIndex_opple:i]] placeholderImage:REPLACEIMAGE];
[subImageview addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(SubimageViewClickAction:)]];
[self.headerView.goodsBrotherScrollview addSubview:subImageview];
}
self.headerView.goodsBrotherScrollview.contentSize = CGSizeMake(0, self.imagesArray.count*100);
self.headerView.goodsBrotherScrollview.showsVerticalScrollIndicator = NO;
self.headerView.goodsBrotherScrollview.pagingEnabled = YES;
self.headerView.goodsBrotherScrollview.userInteractionEnabled = YES;
//图文大图
self.goodsDetailsArray = [NSMutableArray arrayWithArray:[self.entity.detailedIntro componentsSeparatedByString:@","]];
[self.productDetilsTableview reloadData];
}
#pragma mark -小图点击手势、切换图片显示
- (void)SubimageViewClickAction:(UITapGestureRecognizer *)tap
{
self.imageIndex = tap.view.tag-100;
[self.headerView.goodsImageview sd_setImageWithURL:[self.imagesArray objectAtIndex_opple:tap.view.tag-100] placeholderImage:REPLACEIMAGE];
}
#pragma mark -图片放大点击
- (void)amplificationButtonClick
{
FullScreenViewController *fullScreenVC = [[FullScreenViewController alloc]init];
fullScreenVC.datasArray = self.imagesArray;
fullScreenVC.isProductLibrary = YES;
fullScreenVC.currentindex = self.imageIndex;
[self presentViewController:fullScreenVC animated:YES completion:nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
ProductDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productDetailscell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.datas = self.entity;
return cell;
}else if (indexPath.section == 1)
{
goodsDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"goodsdetailscell" forIndexPath:indexPath];
[cell.detailsImageView sd_setImageWithURL:[NSURL URLWithString:[self.goodsDetailsArray objectAtIndex_opple:indexPath.row]] placeholderImage:TCImage(@"bg-big")];
return cell;
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
}
return self.goodsDetailsArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return 170;
}
NSURL *url = [NSURL URLWithString:[self.goodsDetailsArray objectAtIndex_opple:indexPath.row]];
return [ProductDetailsViewController getImageSizeWithURL:url].height;
}
#pragma mark -计算图片高度
+(CGSize)getImageSizeWithURL:(id)imageURL
{
NSURL* URL = nil;
if([imageURL isKindOfClass:[NSURL class]]){
URL = imageURL;
}
if([imageURL isKindOfClass:[NSString class]]){
URL = [NSURL URLWithString:imageURL];
}
if(URL == nil)
return CGSizeZero; // url不正确返回CGSizeZero
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
NSString* pathExtendsion = [URL.pathExtension lowercaseString];
CGSize size = CGSizeZero;
if([pathExtendsion isEqualToString:@"jpg"]){
size = [self getJPGImageSizeWithRequest:request];
}
if(CGSizeEqualToSize(CGSizeZero, size)) // 如果获取文件头信息失败,发送异步请求请求原图
{
NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:nil error:nil];
UIImage* image = [UIImage imageWithData:data];
if(image)
{
size = image.size;
}
}
return size;
}
+(CGSize)getJPGImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=0-209" forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if ([data length] <= 0x58) {
return CGSizeZero;
}
if ([data length] < 210) {// 肯定只有一个DQT字段
short w1 = 0, w2 = 0;
[data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
[data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
short w = (w1 << 8) + w2;
short h1 = 0, h2 = 0;
[data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
[data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
short h = (h1 << 8) + h2;
return CGSizeMake(w, h);
} else {
short word = 0x0;
[data getBytes:&word range:NSMakeRange(0x15, 0x1)];
if (word == 0xdb) {
[data getBytes:&word range:NSMakeRange(0x5a, 0x1)];
if (word == 0xdb) {// 两个DQT字段
short w1 = 0, w2 = 0;
[data getBytes:&w1 range:NSMakeRange(0xa5, 0x1)];
[data getBytes:&w2 range:NSMakeRange(0xa6, 0x1)];
short w = (w1 << 8) + w2;
short h1 = 0, h2 = 0;
[data getBytes:&h1 range:NSMakeRange(0xa3, 0x1)];
[data getBytes:&h2 range:NSMakeRange(0xa4, 0x1)];
short h = (h1 << 8) + h2;
return CGSizeMake(w, h);
} else {// 一个DQT字段
short w1 = 0, w2 = 0;
[data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
[data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
short w = (w1 << 8) + w2;
short h1 = 0, h2 = 0;
[data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
[data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
short h = (h1 << 8) + h2;
return CGSizeMake(w, h);
}
} else {
return CGSizeZero;
}
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
goodsDetailsSectionview *sectionView = [[[NSBundle mainBundle] loadNibNamed:@"goodsDetailsSectionview" owner:self options:nil] firstObject];
sectionView.goodSectionLabe.text = section?@"图文详情":@"商品参数";
return sectionView;
}
#pragma mark -减少商品----增加商品
- (void)reduceGoodsButtonClick:(UIButton *)sender {
NSInteger goodsNumber = [self.headerView.goodsNumber.text integerValue];
switch (sender.tag) {
case 100://减少
{
if (goodsNumber <= 1) {
//不能小于1
[self ErrorMBProgressView:@"不能小于1"];
return;
}
goodsNumber --;
self.headerView.goodsNumber.text = [NSString stringWithFormat:@"%ld",(long)goodsNumber];
}
break;
case 101://增加
{
if (goodsNumber >= [self.entity.number integerValue]) {
//不能大于库存
[self ErrorMBProgressView:@"超过库存"];
return;
}
goodsNumber ++;
self.headerView.goodsNumber.text = [NSString stringWithFormat:@"%ld",(long)goodsNumber];
}
break;
default:
break;
}
}
#pragma mark -UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSString *inputString = textField.text;
if (![self isPureInt:inputString]) {
[self ErrorMBProgressView:@"输入格式错误"];
textField.text = @"1";
return;
}
if ([inputString integerValue] > [self.entity.number integerValue]) {
[self ErrorMBProgressView:@"超过库存数量"];
textField.text = [NSString stringWithFormat:@"%ld",(long)[self.entity.number integerValue]];
return;
}
}
#pragma mark -开发加入购物车动画
- (void)StartAddShoppingCarAnimationWithimage:(UIImage *)image withStartpoint:(CGPoint)point withSize:(CGSize)size
{
// //起点
CGPoint startPoint = point;
// //终点
CGPoint endPoint = SHARED_APPDELEGATE.shoppingCarPoint;
// //控点
CGPoint controlPoint = CGPointMake(endPoint.x, startPoint.x);
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
imageView.backgroundColor = kTCColor(252, 248, 239);
imageView.image = image;
imageView.center = point;
[self.view.window.layer addSublayer:imageView.layer];
//路径曲线
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:imageView.center];
[movePath addQuadCurveToPoint:endPoint
controlPoint:controlPoint];
//关键帧
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = YES;
//旋转变化
CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
//x,y轴缩小到0.1,Z 轴不变
scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
scaleAnim.removedOnCompletion = YES;
//透明度变化
CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
opacityAnim.removedOnCompletion = YES;
//关键帧,旋转,透明度组合起来执行
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
//当动画完成,停留到结束位置
animGroup.removedOnCompletion = NO;
animGroup.fillMode = kCAFillModeForwards;
animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim,opacityAnim, nil];
animGroup.duration = 1;
animGroup.delegate = self;
[imageView.layer addAnimation:animGroup forKey:nil];
[self performSelector:@selector(removeFromLayer:) withObject:imageView.layer afterDelay:1];
}
#pragma mark -动画完成后移除
- (void)removeFromLayer:(CALayer *)layerAnimation{
[layerAnimation removeFromSuperlayer];
}
#pragma mark -完成加入购物车动画完成后回调
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSLog(@"动画完成了");
[[NSNotificationCenter defaultCenter]postNotificationName:@"ADDGOODSNUMBER" object:@([self.headerView.goodsNumber.text integerValue])];
}
#pragma mark -添加至购物车
- (void)addGoodsShoppingbags:(UIButton *)button
{
//判断是否有当前客户
if (![Shoppersmanager manager].currentCustomer) {
[self ErrorMBProgressView:@"必须设置当前客户"];
return;
}
[self StartAddShoppingCarAnimationWithimage:self.headerView.goodsImageview.image withStartpoint:[self.headerView convertPoint:[self.headerView.goodsImageview center] toView:self.view.window] withSize:self.headerView.goodsImageview.frame.size];
SaveShoppingCartRequest *shopCar = [[SaveShoppingCartRequest alloc]init];
shopCar.consumerId = [Customermanager manager].model.fid;
shopCar.goodsId = _goodsID;
shopCar.count = self.headerView.goodsNumber.text;
[[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(ADDSHOPPINGBAG) WithRequestType:0 WithParameter:shopCar WithReturnValueBlock:^(id returnValue) {
if ([returnValue[@"code"] isEqualToNumber:@0]) {
NSLog(@"添加购物车成功");
}else
{
[self ErrorMBProgressView:returnValue[@"message"]];
}
} WithErrorCodeBlock:^(id errorCodeValue) {
} WithFailureBlock:^(id error) {
[self ErrorMBProgressView:@"添加购物车失败"];
}];
}
#pragma mark -判断是否为纯数字
- (BOOL)isPureInt:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return[scan scanInt:&val] && [scan isAtEnd];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end