RightSubView.m 3.61 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
    
zhu's avatar
zhu committed
27 28 29 30 31
//    待接收
}
-(void)initSubView
{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
zhu's avatar
zhu committed
32
    button.frame=CGRectMake(self.frame.size.width-100, self.frame.size.height-60, 60, 60);
zhu's avatar
zhu committed
33
    button.tag=300;
zhu's avatar
zhu committed
34
    [button setBackgroundImage:[UIImage imageNamed:@"产品1"] forState:UIControlStateNormal];
zhu's avatar
zhu committed
35 36
    [button addTarget:self action:@selector(rightBtnDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
zhu's avatar
zhu committed
37
    self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-100)];
zhu's avatar
zhu committed
38 39 40 41 42 43 44 45
       // 禁用滚动条,只设置水平方向的滚动条即可,竖直方向的滚动范围是0,所以没必要设置
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:self.scrollView];
    [self showProductPicture];
}
-(void)showProductPicture
{
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
zhu's avatar
zhu committed
46 47 48
    // 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
49 50
        
        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(80, 20+i*(100+10), 100, 100)];
zhu's avatar
zhu committed
51 52
        UIImage *image=[self.dataArray objectAtIndex:i];
        [button setImage:image forState:UIControlStateNormal];
zhu's avatar
zhu committed
53 54 55
        button.tag=100+i;
        [button addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
        UILongPressGestureRecognizer *longPressGR =
zhu's avatar
zhu committed
56 57
        [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
      
zhu's avatar
zhu committed
58 59 60 61
        longPressGR.minimumPressDuration = 0.5;
        [button addGestureRecognizer:longPressGR];
        
        [self.scrollView addSubview:button];
zhu's avatar
zhu committed
62
        
zhu's avatar
zhu committed
63
    }
zhu's avatar
zhu committed
64 65

}
zhu's avatar
zhu committed
66 67 68 69
//单次点击
-(void)clicked:(UIButton*)sender
{
    NSLog(@"%d",(sender.tag));
zhu's avatar
zhu committed
70
    if ([self.delegate respondsToSelector:@selector(resetSubProductImage:)]) {
zhu's avatar
zhu committed
71
        
zhu's avatar
zhu committed
72
        [self.delegate resetSubProductImage:sender.imageView.image];
zhu's avatar
zhu committed
73 74 75 76 77
    }
    
    //    resetSuperBackGroundImage
}
//长按
zhu's avatar
zhu committed
78
-(void)longPress:(id)sender
zhu's avatar
zhu committed
79
{
zhu's avatar
zhu committed
80 81 82 83 84 85 86 87 88 89 90 91 92
    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
93 94
}

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

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

@end