0

I am creating a class, that creates an option bar with multiple scroll views accessible by buttons.

This is how I add the subview with its content:

NSArray *scroll1 = [NSArray arrayWithObjects:@"normal",@"tiltshift",@"zoom",@"normal",@"blur",@"outline",@"coming soon", nil];
NSArray *scroll2 = [NSArray arrayWithObjects:@"Emoji Natur-01",@"Emoji Natur-02",@"Emoji Natur-03",@"Emoji Natur-04",@"Emoji Natur-05",@"Emoji Natur-06",@"Emoji Natur-07",@"Emoji Natur-08",@"Emoji Natur-09",@"Emoji Natur-10",@"Emoji Natur-11",@"Emoji Natur-12", nil];
NSArray *scroll3 = [NSArray arrayWithObjects:@"Linear",@"Parallel",@"Parallel 2",@"Crescendo",@"Dubstep",@"Blackhole",@"coming soon", nil];
NSArray *content = [NSArray arrayWithObjects:scroll1,scroll2,scroll3, nil];

[self.view addSubview:[self.bottomOptionView initWithFrame:self.view.frame withContent:content]];

The Subclass then generate a scrollview for each Array, with the buttons based on the content of the array:

-(void)addScrollViewOfContent:(NSArray*)array{

int viewNumber = array.count;

for (int i = 0 ; i < viewNumber; i++) {

    //create the main buttons
    NSArray *content = array[i];

    UIButton *buttonAccess = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    CGFloat buttonEdge = 40;
    CGFloat buttonSpace = self.frame.size.width /viewNumber;
    int placeOfButton = i+1;
    CGFloat buttonAnchor = ((placeOfButton*(buttonSpace)) - (buttonSpace /2+ buttonEdge/2));
    buttonAccess.frame = CGRectMake(buttonAnchor,  0 , buttonEdge, buttonEdge);

    [buttonAccess setBackgroundColor:[UIColor redColor]];

    buttonAccess.tag = i;
    [buttonAccess addTarget:self action:@selector(buttonAccess:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:buttonAccess];

    UIScrollView *ScrollView = [[UIScrollView alloc]init];
    ScrollView.frame = CGRectMake(0, 50, self.frame.size.width, self.frame.size.height);
    ScrollView.showsHorizontalScrollIndicator = YES;
    ScrollView.tag = i;

    ScrollView.backgroundColor =[UIColor colorWithRed:0 green:0.0 blue:0.0 alpha:0.0];
    ScrollView.indicatorStyle = UIScrollViewDecelerationRateNormal ;
    //UIImageView *selector = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"FILTER_SELECT.png"]];

    CGFloat btnX = 0;
    for (int i = 0 ; i < content.count; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(btnX, 2 , 65, 65);

        UIImage *img = [UIImage imageNamed:[content objectAtIndex:i]];
        [button setBackgroundImage:img forState:UIControlStateNormal];
        [button addTarget:self action:@selector(subButtonTarget:) forControlEvents:UIControlEventTouchDown];
        button.tag = i;
        [button setTitle:[content objectAtIndex:i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont fontWithName:@"MyriadPro-Cond" size:15];
        button.titleLabel.textColor = [UIColor whiteColor];
        button.titleLabel.textAlignment = UIBaselineAdjustmentAlignBaselines ;         
        button.showsTouchWhenHighlighted = YES;

        [ScrollView addSubview:button];

        btnX = btnX + 100;

    }

    btnX = btnX - 80;

    ScrollView.contentSize = CGSizeMake(btnX + 50, 80);
    ScrollView.hidden = YES;


    [self.scrollViewArray addObject:ScrollView];
    [self addSubview:ScrollView];      
}

This gives me exactly what I want.

Then I detect which button, of which scroll view is pressed to trigger the action.

-(void)subButtonTarget:(UIButton *)sender{

    int button = sender.tag;
    int scrollview = self.selectedScrollView;

[self.videoEditor subAction:button ofScrollView:scrollview];

}

-(void)buttonAccess:(UIButton *)sender{

    //  self.selectedScrollView = sender.tag;

    for (int i=0; i< self.scrollViewArray.count; i++) {

        UIScrollView *scrollview= [self.scrollViewArray objectAtIndex:i];
    scrollview.hidden= YES;
    }

    int scrollIndex = sender.tag;
    UIScrollView *scrollview= [self.scrollViewArray objectAtIndex:scrollIndex];
    scrollview.hidden = NO;

}

Basically when one of the button is touched, it calls a function in the view controller in which the sublclass was initially called.

The problem is that, on touchevent nothing happens, except for NSLog. Would that be because of the dependency relation of the subClass and the VC that uses it?

Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
Zeretyh
  • 17
  • 1
  • 6
  • The problem is that, on touchevent nothing happens, except for NSLog. `NSLog` where it is? – tuledev Jan 08 '16 at 17:28
  • In the original viewController VideoEditor in the function "subAction". When I call subAction from VideoEditor whatever is in the function works. But when I call "SubAction" from the UIView subclass triggered by VideoEditor, then only the NSLog in "SubbAction" is effective. – Zeretyh Jan 08 '16 at 17:32
  • Maybe, you should try to add button in the `UIView` first. And then see it works or not. So you can know, where the problem comes from: UIScrollView with buttons or your subclass – tuledev Jan 08 '16 at 17:37

1 Answers1

0

You should set this one for each UIScrollView

ScrollView.delaysContentTouches = NO;

And try to subclass UIScrollView with override method

-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{   
    return YES;
}
tuledev
  • 10,177
  • 4
  • 29
  • 49