使用 UIScrollView 自定义 UIView 子类
Posted
技术标签:
【中文标题】使用 UIScrollView 自定义 UIView 子类【英文标题】:Custom UIView subClass with UIScrollView 【发布时间】:2016-01-08 16:41:49 【问题描述】:我正在创建一个类,它创建一个选项栏,其中包含多个可通过按钮访问的滚动视图。
这就是我添加子视图及其内容的方式:
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]];
然后子类为每个数组生成一个滚动视图,按钮基于数组的内容:
-(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;
基本上当其中一个按钮被触摸时,它会调用视图控制器中最初调用子类的函数。
问题是,在 touchevent 上,除了NSLog
,什么都没有发生。会不会是因为子类和使用它的VC的依赖关系?
【问题讨论】:
问题是,在 touchevent 上什么都没有发生,除了 NSLog。NSLog
它在哪里?
在原始viewController VideoEditor中的函数“subAction”。当我从 VideoEditor 调用 subAction 时,函数中的任何内容都有效。但是当我从VideoEditor触发的UIView子类中调用“SubAction”时,只有“SubbAction”中的NSLog有效。
也许,您应该先尝试在UIView
中添加按钮。然后看看它是否有效。这样你就可以知道问题出在哪里了:UIScrollView with button or your subclass
【参考方案1】:
你应该为每个 UIScrollView 设置这个
ScrollView.delaysContentTouches = NO;
并尝试使用覆盖方法继承UIScrollView
-(BOOL)touchesShouldCancelInContentView:(UIView *)view
return YES;
【讨论】:
以上是关于使用 UIScrollView 自定义 UIView 子类的主要内容,如果未能解决你的问题,请参考以下文章
支持 UITableView 的自定义 UIScrollView