RightSubView.m 3.8 KB
Newer Older
zhu's avatar
zhu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
//
//  RightSubView.m
//  Lighting
//
//  Created by mac on 16/5/10.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "RightSubView.h"

@implementation RightSubView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
         
        [self initdata];
        [self initSubView];
    }
    return self;
}
-(void)initdata
{
zhu's avatar
zhu committed
23 24 25
    NSString *imageName = [NSString stringWithFormat:@"05产品库-详情_03" ];
   UIImage *image= [UIImage imageNamed:imageName];
    self.dataArray=[[NSMutableArray alloc]initWithObjects:image,image,image,nil];
zhu's avatar
zhu committed
26
    self.productModelArray=[[NSMutableArray alloc]init];
zhu's avatar
zhu committed
27
    
zhu's avatar
zhu committed
28 29 30 31 32
//    待接收
}
-(void)initSubView
{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
zhu's avatar
zhu committed
33
    button.frame=CGRectMake(self.frame.size.width-100, self.frame.size.height-60, 60, 60);
zhu's avatar
zhu committed
34
    button.tag=300;
zhu's avatar
zhu committed
35
    [button setBackgroundImage:[UIImage imageNamed:@"产品1"] forState:UIControlStateNormal];
zhu's avatar
zhu committed
36 37
    [button addTarget:self action:@selector(rightBtnDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
zhu's avatar
zhu committed
38
    self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-100)];
zhu's avatar
zhu committed
39 40 41 42 43 44 45 46
       // 禁用滚动条,只设置水平方向的滚动条即可,竖直方向的滚动范围是0,所以没必要设置
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:self.scrollView];
    [self showProductPicture];
}
-(void)showProductPicture
{
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
zhu's avatar
zhu committed
47 48 49
    // contentSize的y值为0表示在垂直方向上不做滚动
    self.scrollView.contentSize = CGSizeMake(0, (self.dataArray.count+1) * (100+10));
    for (int i=0; i<self.dataArray.count; i++) {
zhu's avatar
zhu committed
50 51
        
        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(80, 20+i*(100+10), 100, 100)];
zhu's avatar
zhu committed
52 53
        UIImage *image=[self.dataArray objectAtIndex:i];
        [button setImage:image forState:UIControlStateNormal];
zhu's avatar
zhu committed
54 55 56
        button.tag=100+i;
        [button addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
        UILongPressGestureRecognizer *longPressGR =
zhu's avatar
zhu committed
57 58
        [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
      
zhu's avatar
zhu committed
59 60 61 62
        longPressGR.minimumPressDuration = 0.5;
        [button addGestureRecognizer:longPressGR];
        
        [self.scrollView addSubview:button];
zhu's avatar
zhu committed
63
        
zhu's avatar
zhu committed
64
    }
zhu's avatar
zhu committed
65 66

}
zhu's avatar
zhu committed
67 68 69 70
//单次点击
-(void)clicked:(UIButton*)sender
{
    NSLog(@"%d",(sender.tag));
zhu's avatar
zhu committed
71
    if ([self.delegate respondsToSelector:@selector(resetSubProductImage:productModel:)]) {
zhu's avatar
zhu committed
72
        
zhu's avatar
zhu committed
73
        [self.delegate resetSubProductImage:sender.imageView.image productModel:self.productModelArray];
zhu's avatar
zhu committed
74 75 76 77 78
    }
    
    //    resetSuperBackGroundImage
}
//长按
zhu's avatar
zhu committed
79
-(void)longPress:(id)sender
zhu's avatar
zhu committed
80
{
zhu's avatar
zhu committed
81 82 83 84 85 86 87 88 89 90 91 92 93
    UILongPressGestureRecognizer *longPress=sender;
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSInteger index=longPress.view.tag-100;
        if (self.dataArray.count>index) {
            [self.dataArray removeObjectAtIndex:index];
            [self showProductPicture];
        }
        
    }
    else {
        
    }
     NSLog(@"长按");
zhu's avatar
zhu committed
94 95
}

zhu's avatar
zhu committed
96 97
-(void)rightBtnDidSelected:(UIButton *)sender
{
zhu's avatar
zhu committed
98 99 100 101 102
    if ([self.delegate respondsToSelector:@selector(buttonClick:withButton:)]) {
        
        [self.delegate buttonClick:sender.tag withButton:sender];
    }

zhu's avatar
zhu committed
103
}
zhu's avatar
zhu committed
104
-(void)addRightSubViewImage:(UIImage*)image productModel:(TOGoodsEntity *)productModel
zhu's avatar
zhu committed
105
{
zhu's avatar
zhu committed
106
    [self.productModelArray addObject:productModel];
zhu's avatar
zhu committed
107 108 109
    [self.dataArray addObject:image];
    [self showProductPicture];
    
zhu's avatar
zhu committed
110 111 112 113 114 115 116 117 118 119
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end