ScrollView 中的按钮未启用
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要更改触摸时的图像,
还要确保您有
menubutton.showTouchOnHighlight = YES;
并且也许滚动视图会接受触摸事件。试试这些
To change image on touch use
Also make sure you have
menubutton.showTouchOnHighlight = YES;
andMaybe the scrollView takes the touch events. Try these
我通过更改添加到滚动视图的视图框架解决了这个问题。
因此,而不是这一行:
我现在让视图在每次更改页面时更改其框架:
所以原因与我添加到滚动视图的视图框架的位置有关。当滚动视图与分页一起使用时,似乎会出现此问题。当页面更改时,请确保滚动视图内的视图也移动到同一帧。
I solved this problem by changing the frame of the view I am adding to my scroll view.
So instead of this line:
I now make the view change its frame every time I change the page:
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.
请在你的scrollView中实现此代码
并添加此方法
Please Implement this code inside your scrollView
And Add this method
menuImage
不应被释放。它来自`[UIImage imageNamed:],它是一个自动释放的对象。这会导致图像过度释放。menuImage
should not be release. It is come from `[UIImage imageNamed:], and it is an autoreleased object. It will cause the image over released.试试这个,我不确定它是否适合你的情况。
Just try this and i am not sure whether it works out in your case.
尝试此代码
并请验证名为
menuimage.png
和menuimageselected.png
的两个图像是否存在。try this code
and please verify that the two images named
menuimage.png
andmenuimageselected.png
are there.我会在“for”括号内声明“J”。
您正在保留 UIButton,然后释放它。您不需要执行任何操作,因为您没有分配它。
您不应该释放“menuImage”。
您正在将“menuImage2”分配给 UIButton 的选定状态。它应该是 UIControlStateHighlighted。
编辑:
不要这样做:
也试试这个:
I would declare "J" inside the "for" parentheses.
You are retaining the UIButton and then releasing it. You don't need to do either, because you aren't allocating it.
You shouldn't release "menuImage".
You are assigning "menuImage2" to selected state of the UIButton. It should be UIControlStateHighlighted.
Edit:
Dont do this:
Also try this: