FootSubView.m 3.59 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 23
//
//  FootSubView.m
//  Lighting
//
//  Created by mac on 16/5/10.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "FootSubView.h"

@implementation FootSubView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
    
        [self initdata];
        [self initSubView];
    }
    return self;
}

-(void)initdata
{
24 25 26
//    NSString *imageName = [NSString stringWithFormat:@"backView" ];
//    UIImage *image=[UIImage imageNamed:imageName];
    self.dataArray=[[NSMutableArray alloc]init];
zhu's avatar
zhu committed
27 28 29 30
    //    待接收
}
-(void)initSubView
{
31 32 33 34 35 36
    self.selectBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.selectBtn.frame=CGRectMake(30, 50, 60, 60);
    [self.selectBtn setBackgroundImage:[UIImage imageNamed:@"场景2"] forState:UIControlStateNormal];
    self.selectBtn.tag=200;
    [self.selectBtn addTarget:self action:@selector(footBtnDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.selectBtn];
zhu's avatar
zhu committed
37
    self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(100, 0, self.frame.size.width-100, self.frame.size.height)];
zhu's avatar
zhu committed
38 39 40
    // 禁用滚动条,只设置水平方向的滚动条即可,竖直方向的滚动范围是0,所以没必要设置
    self.scrollView.showsHorizontalScrollIndicator = NO;
     [self addSubview:self.scrollView];
zhu's avatar
zhu committed
41
    // contentSize的y值为0表示在垂直方向上不做滚动
zhu's avatar
zhu committed
42 43 44 45 46 47
    [self showSecenePicture];
    
}
-(void)showSecenePicture
{
     [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
zhu's avatar
zhu committed
48 49
    self.scrollView.contentSize = CGSizeMake((self.dataArray.count+1) * (100+10), 0);
    for (int i=0; i<self.dataArray.count; i++) {
50
        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(i*(125+10), 30, 125, 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
        [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
zhu's avatar
zhu committed
57 58
        longPressGR.minimumPressDuration = 0.5;
        [button addGestureRecognizer:longPressGR];
zhu's avatar
zhu committed
59 60 61
        
        [self.scrollView addSubview:button];
    }
zhu's avatar
zhu committed
62

zhu's avatar
zhu committed
63
}
zhu's avatar
zhu committed
64
//单次点击 设置为背景色
zhu's avatar
zhu committed
65 66 67 68 69 70 71 72 73 74
-(void)clicked:(UIButton*)sender
{
    NSLog(@"%d",(sender.tag));
    if ([self.delegate respondsToSelector:@selector(resetSuperBackGroundImage:)]) {
        
        [self.delegate resetSuperBackGroundImage:sender.imageView.image];
    }

//    resetSuperBackGroundImage
}
zhu's avatar
zhu committed
75 76
//长按   删除已添加
-(void)longPress:(id)sender
zhu's avatar
zhu committed
77
{
zhu's avatar
zhu committed
78 79 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 showSecenePicture];
        }

    }
    else {
        
    }
    
    

zhu's avatar
zhu committed
93 94
    NSLog(@"长按");
}
zhu's avatar
zhu committed
95
//回调
zhu's avatar
zhu committed
96 97
-(void)footBtnDidSelected:(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 105 106 107 108
-(void)addFootSubViewImage:(UIImage*)image
{
    [self.dataArray addObject:image];
    [self showSecenePicture];  //重新绘制图片数量
}
zhu's avatar
zhu committed
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