//
//  ActionScrollView.m
//  VclustersGemini
//
//  Created by APPLE on 2020/4/26.
//  Copyright © 2020 APPLE. All rights reserved.
//

#import "HelpDownScrollView.h"
#import "UIView+View.h"


@implementation HelpDownScrollView
@synthesize presedDelegate;


- (id)initWithFrame:(CGRect)frame withImageAry:(NSArray *)imageAry tilteAry:(NSArray *)titleAry numberOfLine:(NSInteger)numberOfLine
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        [self setBackgroundColor:[UIColor clearColor]];
        [self drawAnyViewWithImageAry:imageAry tilteAry:titleAry numberOfLine:numberOfLine];
    }
    
    return self;
}

- (void)drawAnyViewWithImageAry:(NSArray *)imageAry tilteAry:(NSArray *)titleAry numberOfLine:(NSInteger)numberOfLine
{
    NSInteger lines = titleAry.count/numberOfLine + ((titleAry.count%numberOfLine == 0)?0:1);
    CGFloat w_btn = self.width / numberOfLine;
    CGFloat h_btn = (self.frame.size.height - 15.f)/lines;
    
    for (NSInteger nFori = 0; nFori < [imageAry count]; nFori++)
    {
        /*x,y计算*/
        NSInteger xnumber = nFori%numberOfLine;
        NSInteger ynumber = nFori/numberOfLine;
        UIButton *tempBtn = [[UIButton alloc] initWithFrame:CGRectMake(xnumber*w_btn, 15.f + ynumber*h_btn, w_btn, h_btn)];
        [tempBtn setBackgroundColor:[UIColor clearColor]];
        [tempBtn setImage:[UIImage imageNamed:[imageAry objectAtIndex:nFori]] forState:(UIControlStateNormal)];
        [tempBtn setTag:nFori + 1];
        [tempBtn setContentMode:(UIViewContentModeScaleAspectFill)];
        tempBtn.clipsToBounds = YES;
        [tempBtn setTitle:[titleAry objectAtIndex:nFori] forState:(UIControlStateNormal)];
        [tempBtn addTarget:self action:@selector(actionPressed:) forControlEvents:(UIControlEventTouchUpInside)];
        [tempBtn.titleLabel setFont:[UIFont systemFontOfSize:13.f]];
        [tempBtn setTitleColor:HW0A132BColor forState:(UIControlStateNormal)];
        [tempBtn setTitleEdgeInsets:UIEdgeInsetsMake(tempBtn.imageView.frame.size.height+10,-tempBtn.imageView.frame.size.width+2, 0.0,0.0)];
        [tempBtn setImageEdgeInsets:UIEdgeInsetsMake(-tempBtn.bounds.size.width/4+15, tempBtn.bounds.size.width/4-2, tempBtn.titleLabel.bounds.size.height, -tempBtn.bounds.size.width/4)];
        
        [self addSubview:tempBtn];
    }
    
    [self setContentSize:CGSizeMake(self.width, self.height)];
}

- (void)actionPressed:(id)sender
{
    
    NSInteger tag = ((UIButton *)sender).tag;
    NSInteger index = tag - 1;
    
    if ([presedDelegate respondsToSelector:@selector(helpDownScrollViewBePressed:withIndex:)])
    {
        [presedDelegate helpDownScrollViewBePressed:sender withIndex:index];
    }
}
@end
