UIButton 在 ScrollView 上不可点击
Posted
技术标签:
【中文标题】UIButton 在 ScrollView 上不可点击【英文标题】:UIButton isn't clickable on ScrollView 【发布时间】:2013-10-03 20:12:03 【问题描述】:我正在尝试以编程方式将 UIButton 添加到我的滚动视图中。按钮在屏幕上弹出,但不可点击...
这是我的代码 (viewDidLoad):
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(showAlert:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Enter app" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
self.view.backgroundColor = COLOR_BG;
self.view.clipsToBounds = YES;
CGRect scrollFrame;
scrollFrame.origin.x = 0;
scrollFrame.origin.y = 0;
scrollFrame.size.width = self.view.frame.size.width + 500;
scrollFrame.size.height = self.view.frame.size.height + 500;
self.scrollView = [[[UIScrollView alloc] initWithFrame:scrollFrame] autorelease];
self.scrollView.backgroundColor = COLOR_BG;
self.scrollView.bounces = YES;
self.scrollView.delegate = self;
self.scrollView.userInteractionEnabled = YES;
self.scrollView.exclusiveTouch = YES;
_scrollView.delaysContentTouches = NO;
_scrollView.canCancelContentTouches = NO;
self.slideImages = [[[NSMutableArray alloc] init] autorelease];
int rows = 8;
int total_count = 8;
for (int i = 0; i < total_count;i++)
ImageRenderView *imageView = [[ImageRenderView alloc] initWithFrame:CGRectMake(i * (SIZE+LEFT_EDGE_OFSET), 0, SIZE + LEFT_EDGE_OFSET, SIZE * rows)];
[self.scrollView addSubview:imageView];
[self.slideImages addObject:imageView];
[imageView release];
[self.scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE, self.scrollView.frame.size.height)];
[self.scrollView setContentOffset:CGPointMake(0, 0)];
self.gradient = [CAGradientLayer layer];
self.gradient.frame = self.scrollView.bounds;
self.gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor], (id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:0] CGColor], nil];
self.gradient.startPoint = CGPointMake(0.1, 0.5);
self.gradient.endPoint = CGPointMake(0.9, 0.5);
[self.view addSubview:self.scrollView];
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE*2,0,WIDTH_OF_IMAGE,self.scrollView.frame.size.height) animated:NO];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(scrollView1:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
[self.scrollView.layer addSublayer:self.gradient];
[self makePerspective];
self.scrollView.alpha = 0.0;
////
CC_DIRECTOR_END();
// Create an CCGLView with a RGB8 color buffer, and a depth buffer f 0-bits
CCGLView *glView = [CCGLView viewWithFrame:[[UIScreen mainScreen] bounds]
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
director_ = (CCDirectorios*)[CCDirector sharedDirector];
director_.wantsFullScreenLayout = YES;
// Display Milliseconds Per Frame
[director_ setDisplayStats:YES];
// set FPS at 60
[director_ setAnimationInterval:1.0/60];
// attach the openglView to the director
[director_ setView:glView];
// 3D projection
[director_ setProjection:kCCDirectorProjection3D];
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director_ enableRetinaDisplay:YES] )
CCLOG(@"Retina Display Not supported");
UINavigationController *navController_ = [[[UINavigationController alloc] initWithRootViewController:director_] autorelease];
navController_.navigationBarHidden = YES;
[director_ setDelegate:nil];
[self.view addSubview:navController_.view];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];
[sharedFileUtils setEnableFallbackSuffixes:YES];
[sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"];
[sharedFileUtils setiPadSuffix:@"-ipad"];
[sharedFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"];
movingBlockVC = [[MovingBlockContainer alloc] initWithNibName:nil bundle:nil];
[movingBlockVC view];
CGRect old = movingBlockVC.view.frame;
[self.view addSubview:movingBlockVC.view];
movingBlockVC.view.frame = CGRectMake(-old.size.width, -old.size.height, old.size.width, old.size.height);
movingBlockVC.view.hidden = YES;
[self.view sendSubviewToBack:movingBlockVC.view];
director_.view.hidden = YES;
有人可以帮我解决这个问题吗?或者只是解释我做错了什么?
【问题讨论】:
你登录过 showalert() 方法吗?你是怎么说它不可点击的。为滚动视图设置背景颜色,检查按钮在滚动视图上方出现? 【参考方案1】:不确定,但看起来您将按钮添加到视图中,然后将滚动视图添加到按钮顶部的视图中;这意味着任何触摸事件都将出现在滚动视图中,而不是按钮中。
如果我的假设是正确的,您应该在启动和配置按钮后将其添加到滚动视图。
【讨论】:
【参考方案2】:你在scrollView1:
做什么?如果它有动画,那很可能是你的问题。
查看动画选项UIViewAnimationOptionAllowUserInteraction
,看看是否对您有帮助。
【讨论】:
是的,滚动视图有动画。其实这个:github.com/indideveloper/MKMovingBlockAnimation 我没有通读所有的 MKMovingBlockAnimiation 代码,但大多数动画会移动按钮的“图像”,而不是按钮本身。显然动画图像不会像实际的UIButton
那样响应触摸。
好的,那么你建议我应该怎么做?以上是关于UIButton 在 ScrollView 上不可点击的主要内容,如果未能解决你的问题,请参考以下文章
scrollViewDidScroll(_ scrollView: UIScrollView) 到达底部后如何保持 UIButton 浮动?
UITapGestureRecognizer 正在覆盖 Scrollview 上的 UIButton 操作
使用swift在scrollview内的stackview中更改uibutton中的标题