UIButton无法在ScrollView上单击

时间:2013-10-03 20:12:04

标签: ios uiscrollview uibutton

我正在尝试以编程方式将UIButton添加到我的Scroll View中。该按钮会弹出屏幕,但不可点击......

这是我的代码(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;

}

有人可以帮我解决这个问题吗?或者只是解释一下我做错了什么?

2 个答案:

答案 0 :(得分:1)

不确定,但看起来您将按钮添加到视图中,然后将滚动视图添加到按钮顶部的视图中;这意味着任何触摸事件都将在滚动视图中结束,而不是按钮。

如果我的假设是正确的,您应该在启动和配置后将按钮添加到滚动视图。

答案 1 :(得分:0)

你在scrollView1:做什么?如果有动画,那么这可能是你的问题。

看一下动画选项UIViewAnimationOptionAllowUserInteraction,看看这对你有帮助。