ScrollView 中的按钮未启用

发布于 2024-11-09 13:35:43 字数 1772 浏览 5 评论 0原文

使用 Erica Sadun 的滚动视图示例,我创建了一个项目。 现在我的项目中有一个滚动视图和一个页面控件。 我为每个页面创建一个新视图并将其添加到我的滚动视图中,如下所示。 每个视图都有 5 个按钮,因此我可以 5 个滚动 5 个。 但是这些按钮似乎没有启用。 有什么想法吗?

    // Create the scroll view and set its content size and delegate
    sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 200.0f, 300.0f, 60.0f)] autorelease];
    sv.contentSize = CGSizeMake(NPAGES * 300.0f, sv.frame.size.height);
    sv.pagingEnabled = YES;
    sv.delegate = self;


    // Load in all the pages
    for (int i = 0; i < NPAGES; i++)
    {
        UIView *menuView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 60.0f)];


    for (int j = 0; j < 5 * (i+1); j++)
        {
            UIImage *menuImage = [UIImage imageNamed:@"menuimage.png"];
            UIImage *menuImage2 = [UIImage imageNamed:@"menuimageselected.png"];
UIButton *menuButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

            menuButton.frame = CGRectMake(j * 60.0f, 0.0f, 60.0f, 60.0f);
            [menuButton addTarget:self action:@selector(menuButtonClicked) forControlEvents:UIControlEventTouchUpInside];


        menuButton.tag = j + 5 * i;

            menuButton.backgroundColor = [UIColor clearColor];
            [menuButton setImage:menuImage forState:UIControlStateNormal];
            [menuButton setImage:menuImage2 forState:UIControlStateSelected];
            [menuButton setImage:menuImage2 forState:UIControlStateHighlighted];
            [menuButton setEnabled:YES];


            [menuImage release];
            [menuButton release];
            [menuView addSubview:menuButton];

        }

        [sv addSubview:menuView];
        [sv bringSubviewToFront:menuView];

        [menuView release];

    }

    [self.view addSubview:sv];

Using Erica Sadun's scroll view example I created a project.
I now have a scroll view and a page control in my project.
I create a new view for each page and add it to my scroll view as seen below.
Each view has 5 buttons so I can scroll 5 by 5.
However the buttons do not seem to be enabled.
Any ideas why?

    // Create the scroll view and set its content size and delegate
    sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 200.0f, 300.0f, 60.0f)] autorelease];
    sv.contentSize = CGSizeMake(NPAGES * 300.0f, sv.frame.size.height);
    sv.pagingEnabled = YES;
    sv.delegate = self;


    // Load in all the pages
    for (int i = 0; i < NPAGES; i++)
    {
        UIView *menuView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 60.0f)];


    for (int j = 0; j < 5 * (i+1); j++)
        {
            UIImage *menuImage = [UIImage imageNamed:@"menuimage.png"];
            UIImage *menuImage2 = [UIImage imageNamed:@"menuimageselected.png"];
UIButton *menuButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

            menuButton.frame = CGRectMake(j * 60.0f, 0.0f, 60.0f, 60.0f);
            [menuButton addTarget:self action:@selector(menuButtonClicked) forControlEvents:UIControlEventTouchUpInside];


        menuButton.tag = j + 5 * i;

            menuButton.backgroundColor = [UIColor clearColor];
            [menuButton setImage:menuImage forState:UIControlStateNormal];
            [menuButton setImage:menuImage2 forState:UIControlStateSelected];
            [menuButton setImage:menuImage2 forState:UIControlStateHighlighted];
            [menuButton setEnabled:YES];


            [menuImage release];
            [menuButton release];
            [menuView addSubview:menuButton];

        }

        [sv addSubview:menuView];
        [sv bringSubviewToFront:menuView];

        [menuView release];

    }

    [self.view addSubview:sv];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

十二 2024-11-16 13:35:43

要更改触摸时的图像,

[menuButton setImage:menuImage2 forState:UIControlStateHighlighted];

还要确保您有 menubutton.showTouchOnHighlight = YES; 并且

menuButton.adjustImageWhenHighlighted = YES;

也许滚动视图会接受触摸事件。试试这些

scrollView.delaysContentTouches = NO;
scrollView.canCancelContentTouches = NO;

To change image on touch use

[menuButton setImage:menuImage2 forState:UIControlStateHighlighted];

Also make sure you have menubutton.showTouchOnHighlight = YES; and

menuButton.adjustImageWhenHighlighted = YES;

Maybe the scrollView takes the touch events. Try these

scrollView.delaysContentTouches = NO;
scrollView.canCancelContentTouches = NO;
时光礼记 2024-11-16 13:35:43

我通过更改添加到滚动视图的视图框架解决了这个问题。
因此,而不是这一行:

UIView *menuView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 60.0f)];

我现在让视图在每次更改页面时更改其框架:

UIView *menuView = [[UIView alloc] initWithFrame:CGRectMake(i * 300.0f, 0.0f, 300.0f, 60.0f)];

所以原因与我添加到滚动视图的视图框架的位置有关。当滚动视图与分页一起使用时,似乎会出现此问题。当页面更改时,请确保滚动视图内的视图也移动到同一帧。

I solved this problem by changing the frame of the view I am adding to my scroll view.
So instead of this line:

UIView *menuView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 60.0f)];

I now make the view change its frame every time I change the page:

UIView *menuView = [[UIView alloc] initWithFrame:CGRectMake(i * 300.0f, 0.0f, 300.0f, 60.0f)];

So the reason was related with the location of the frame of the view I was adding to the scroll view. This problem seems to occur when scroll view is used together with paging. When the page changes make sure the view inside scrollview also moves to the same frame.

三生一梦 2024-11-16 13:35:43

请在你的scrollView中实现此代码

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(X, Y, Width, Height);
    btn.tag = //your tag Number;
    [btn addTarget:self action:@selector(showGameDetail:) forControlEvents:UIControlEventTouchUpInside];

并添加此方法

 -(IBAction)showGameDetail:(id)sender
 {
    //Your Code
 }

Please Implement this code inside your scrollView

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(X, Y, Width, Height);
    btn.tag = //your tag Number;
    [btn addTarget:self action:@selector(showGameDetail:) forControlEvents:UIControlEventTouchUpInside];

And Add this method

 -(IBAction)showGameDetail:(id)sender
 {
    //Your Code
 }
潦草背影 2024-11-16 13:35:43
 [menuImage release];

menuImage 不应被释放。它来自`[UIImage imageNamed:],它是一个自动释放的对象。这会导致图像过度释放。

 [menuImage release];

menuImage should not be release. It is come from `[UIImage imageNamed:], and it is an autoreleased object. It will cause the image over released.

落墨 2024-11-16 13:35:43

试试这个,我不确定它是否适合你的情况。

 [scrollView bringSubviewToFront:button];

Just try this and i am not sure whether it works out in your case.

 [scrollView bringSubviewToFront:button];
旧伤还要旧人安 2024-11-16 13:35:43

尝试此代码

for (int i = 0; i < NPAGES; i++)
{
    UIView *menuView = [[[UIView alloc] initWithFrame:CGRectMake(i * 300.0f, 0.0f, 300.0f, 60.0f)] autorelease];

    for (j = 0; j < 5 ; j++)
    {
        UIImage *menuImage = [UIImage imageNamed:@"menuimage.png"];
        UIImage *menuImage2 = [UIImage imageNamed:@"menuimageselected.png"];
        UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
        menuButton.frame = CGRectMake(j * 60.0f, 0.0f, 60.0f, 60.0f);
        menuButton.backgroundColor = [UIColor clearColor];
        [menuButton setImage:menuImage forState:UIControlStateNormal];
        [menuButton setImage:menuImage2 forState:UIControlStateHighlighted];
        [menuButton setEnabled:YES];
        [menuButton addTarget:self action:@selector(someSelector)  forControlEvents:UIControlEventTouchUpInside];

        [menuView addSubview:menuButton];
    }

    [sv addSubview:menuView];
}

并请验证名为 menuimage.pngmenuimageselected.png 的两个图像是否存在。

try this code

for (int i = 0; i < NPAGES; i++)
{
    UIView *menuView = [[[UIView alloc] initWithFrame:CGRectMake(i * 300.0f, 0.0f, 300.0f, 60.0f)] autorelease];

    for (j = 0; j < 5 ; j++)
    {
        UIImage *menuImage = [UIImage imageNamed:@"menuimage.png"];
        UIImage *menuImage2 = [UIImage imageNamed:@"menuimageselected.png"];
        UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
        menuButton.frame = CGRectMake(j * 60.0f, 0.0f, 60.0f, 60.0f);
        menuButton.backgroundColor = [UIColor clearColor];
        [menuButton setImage:menuImage forState:UIControlStateNormal];
        [menuButton setImage:menuImage2 forState:UIControlStateHighlighted];
        [menuButton setEnabled:YES];
        [menuButton addTarget:self action:@selector(someSelector)  forControlEvents:UIControlEventTouchUpInside];

        [menuView addSubview:menuButton];
    }

    [sv addSubview:menuView];
}

and please verify that the two images named menuimage.png and menuimageselected.png are there.

究竟谁懂我的在乎 2024-11-16 13:35:43
  1. 我会在“for”括号内声明“J”。

  2. 您正在保留 UIButton,然后释放它。您不需要执行任何操作,因为您没有分配它。

  3. 您不应该释放“menuImage”。

  4. 您正在将“menuImage2”分配给 UIButton 的选定状态。它应该是 UIControlStateHighlighted。

编辑:

不要这样做:

 [menuImage release];
 [menuButton release];

也试试这个:

- (void) menuButtonClicked: (id) sender {

    UIButton *btn = (UIButton *)sender;
    NSLog(@"clicked %d",[btn tag]);
}
  1. I would declare "J" inside the "for" parentheses.

  2. You are retaining the UIButton and then releasing it. You don't need to do either, because you aren't allocating it.

  3. You shouldn't release "menuImage".

  4. You are assigning "menuImage2" to selected state of the UIButton. It should be UIControlStateHighlighted.

Edit:

Dont do this:

 [menuImage release];
 [menuButton release];

Also try this:

- (void) menuButtonClicked: (id) sender {

    UIButton *btn = (UIButton *)sender;
    NSLog(@"clicked %d",[btn tag]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文