gpt4 book ai didi

iOS - 在 UIScrollView 中添加多个 UIButton 滚动不流畅

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:47:58 24 4
gpt4 key购买 nike

我在 UIScrollview 中添加 (400+) 个 UIButton,当我在 simulator 中滚动时,它运行良好,但在 iPad 中它缓慢滚动。

Here's我在做什么。

- (void)viewDidLoad {
[scrollView setContentSize:CGSizeMake(180 * 24 , 22 * 40 + 200)];
for (int e=0; e<12; e++) {
for(int i=0;i<24;i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
[btn setBackgroundColor:[UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1]];


CALayer *layer = btn.layer;
layer.cornerRadius = 5.f;
layer.masksToBounds = YES;
layer.borderWidth = 1.f;
layer.borderColor = [[UIColor colorWithRed:190.0/255.0 green:190.0/255.0 blue:190.0/255.0 alpha:1] CGColor];

CGRect rect = btn.frame;
rect.size.height = 40;
rect.size.width = 50;
rect.origin.x = 100 * i;
rect.origin.y = 40 * e;
btn.frame = rect;

CAGradientLayer *shineLayer = [CAGradientLayer layer];
shineLayer.frame = layer.bounds;
shineLayer.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:0.9f alpha:0.5f].CGColor,
(id)[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor,
(id)[UIColor colorWithWhite:0.9f alpha:1.f].CGColor,
(id)[UIColor colorWithWhite:0.9f alpha:1.f].CGColor,
(id)[UIColor colorWithWhite:0.9f alpha:1.f].CGColor,nil];
[layer addSublayer:shineLayer];

UILabel *lblDes = [[UILabel alloc] initWithFrame:CGRectMake(0,0,50,40)];
lblDes.backgroundColor = [UIColor clearColor];
lblDes.numberOfLines = 3;
lblDes.textColor = [UIColor colorWithRed:4.0/255.0 green:106.0/255.0 blue:200.0/255.0 alpha:1];
lblDes.textAlignment = UITextAlignmentCenter;
lblDes.text = @"adf";
lblDes.tag = -1;
lblDes.font = [UIFont systemFontOfSize:11.f];

[btn addSubview:lblDes];
[scrollView addSubview:btn];
}
}
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. }

最佳答案

iOS 模拟器在 Mac 上运行,比实际设备快很多。

这完全取决于您的要求(为什么您需要这么多按钮),

我建议添加一个 UIView 作为 subview ,然后在 drawRect 中绘制必要的东西。然后您可以使用手势识别器找出用户点击的位置。 (根据 tdubik 的建议)

其他解决方案可以是 tableView。 (有可能重复使用单元格。您可以为每个单元格添加一个按钮)。

如果 tableView 不是您要查找的内容,并且 drawRect 太简单(因为它没有选择效果)- 您可以有一个隐藏的 UIButton,然后可以将其(处于突出显示状态)相应地放置在用户点击的位置,并在用户从屏幕上移除触摸时移除。

编辑

当使用 drawrect 时 - 然后您可以为使用 drawrect 绘制元素的 subview 添加手势识别器:

CustomViewClass *mView = [CustomViewClass alloc] init];

...

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleTap:)];

singleTap.delegate = self;

singleTap.numberOfTapsRequired = 1;

singleTap.numberOfTouchesRequired = 1;

[mView addGestureRecognizer:singleTap];

然后:

- (void)handleTap:(UITapGestureRecognizer *)tap
{
CGPoint mPoint = [tap locationInView:tap.view];

int mOffset = 0; //beginning offset where buttons will start.

for(int i = 0; i < 20; i++) //how many rows of buttons? 20?
{
mOffset += 20; //your custom drawn button height + offset from previous button

if(mPoint.y < mOffset) //now check - if y tapped position is lower than current button.y + its height, then we know what button we tapped.
{
//do something - we found button row!!
break;
}
}

mOffset = 0; //beginning x offset

for(int i = 0; i < 20; i++) //how many columns buttons? 20?
{
mOffset += 20; //your custom drawn button width + offset from previous button

if(mPoint.x < mOffset) //now check - if x tapped position is lower than current button.x + its width, then we know what button we tapped.
{
//do something - we found button column!! at this point we know which button was tapped.
break;
}
}
}

如果您有二维按钮数组,请添加对 x 坐标的相同检查。

关于iOS - 在 UIScrollView 中添加多个 UIButton 滚动不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12910909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com