我正在创建一个类,它创建一个可以通过按钮访问多个滚动视图的选项栏。
这是我添加子视图及其内容的方式:
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]];
Subclass然后为每个Array生成一个scrollview,其中的按钮基于数组的内容:
-(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];
}
这给了我我想要的东西。
然后我检测哪个按钮,按下哪个滚动视图来触发动作。
-(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;
}
基本上,当触摸其中一个按钮时,它会调用视图控制器中最初调用子类的函数。
问题在于,除了NSLog
之外,没有任何事情发生。那是因为subClass和使用它的VC的依赖关系吗?
答案 0 :(得分:0)
您应该为每个UIScrollView
设置一个 ScrollView.delaysContentTouches = NO;
尝试使用覆盖方法
对UIScrollView
进行子类化
-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}