我有 6 个按钮,我想使用 iCarousel 设置动画,代码如下
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
self.icon = [NSMutableArray arrayWithObjects:@"icon-02.png",@"icon-03.png",@"icon-04.png",@"icon-05.png",@"icon-06.png",@"icon-07.png",nil];
//no button available to recycle, so create new one
UIImage *image = [UIImage imageNamed:[icon objectAtIndex:index]];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 130.0f, 130.0f);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
//button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
//[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
return button;
}
但是按钮不在中心,现在有人热衷于将轮播放在中心吗,我已经调整了 uiview 的大小,但仍然无法工作。 谢谢...
最佳答案
默认情况下,轮播和轮播项目应居中(它们位于 iCarousel 附带的示例项目中,它们没有做任何特殊的事情)。无需调整 Nib 中转盘的位置(除非它没有明显居中)。如果这没有按预期工作,您可能发现了一个错误 - 您可以在项目的 github 页面上提出问题吗?
无关:您那里的按钮回收逻辑完全错误,并且只是巧合。除此之外,您还需要重新创建图标数组 6 次。
创建按钮的正确方法是这样的:
- (void)viewDidLoad
{
[super viewDidLoad];
//set up icons array
self.icon = [NSMutableArray arrayWithObjects:@"icon-02.png",@"icon-03.png",@"icon-04.png",@"icon-05.png",@"icon-06.png",@"icon-07.png",nil];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
//*************************************************
//do setup that is the same for every button here
//*************************************************
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 130.0f, 130.0f);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
//[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
//*************************************************
//do setup that is different depending on index here
//*************************************************
UIImage *image = [UIImage imageNamed:[icon objectAtIndex:index]];
[button setBackgroundImage:image forState:UIControlStateNormal];
return button;
}
关于iphone - 如何使 iCarousel View 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13486473/