gpt4 book ai didi

iphone - 在 Objective-C 上缩放 UIScrollview

转载 作者:行者123 更新时间:2023-12-03 21:15:49 24 4
gpt4 key购买 nike

在我正在进行的一个项目中,我有一个 UIScrollView,它有很多类似于电影院座位的按钮和标签,用户可以点击它们来选择要购买的座位。是否可以缩放UIScrollView?我已经声明了 UIScrollViewDelegate,将 delegate 设置为 self,设置了最小和最大缩放比例,但我遇到了 viewForZoomingInScrollView 的问题和 scrollViewDidEndZooming 方法。我尝试在 viewForZoomingInScrollView 方法上返回 ScrollView ,但它崩溃了。

下面是一些代码:

- (void) renderSeatMapOnScreen
{
int x_offset_row = 10;
int y_offset_row = 25;
int x_offset_col = 30;
int y_offset_col = 0;
int scrollview_w = 0;
int scrollview_h = 0;

NSString *row_name = @"";
int tag_index = 0;

NSMutableArray *seat_map_table = [seat_map_xml seats_map];
NSString *seat_available_str = [seat_avail_xml seat_available];

HideNetworkActivityIndicator();

NSLog(@"Seats available = %@", seat_available_str);

NSArray *seats_avail = [seat_available_str componentsSeparatedByString:@";"];

int ROW_COL_OFFSET = 25;

for (int row = 0; row < [seat_map_table count]; row++)
{
UILabel * rowlabel = [[UILabel alloc] init];
rowlabel.frame = CGRectMake(x_offset_row , y_offset_row, 22, 22);

SeatMapDeclaration *rowmap = [seat_map_table objectAtIndex:row];
rowlabel.text = rowmap.rows;
[scrollview addSubview:rowlabel];

row_name = [NSString stringWithFormat:@"%@", rowmap.rows];

NSArray *seat = [rowmap.rowmap componentsSeparatedByString:@";"];
NSLog(@"row[%i] = %@", row, rowmap.rowmap);

if (row == 0)
{
scrollview_w = [seat count] * ROW_COL_OFFSET + y_offset_row;
total_column = [seat count];
total_row = [seat_map_table count];
}

x_offset_col = 30 ;
y_offset_col = y_offset_row ;

for (int column = 0; column < [seat count]; column++)
{
if (([[seat objectAtIndex:column] rangeOfString:@"a("].location != NSNotFound)
|| ([[seat objectAtIndex:column] isEqualToString:@""]))
{
CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
[imageView setImage:[UIImage imageNamed:@"noseat.png"]];
[scrollview addSubview:imageView];
}
else if ([[seat objectAtIndex:column] rangeOfString:@"b("].location != NSNotFound)
{
CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
imageView.image = [UIImage imageNamed:@"noseat.png"];
[scrollview addSubview:imageView];
}
else
{
NSString *status = [NSString stringWithFormat:@"%@%@", row_name, [seat objectAtIndex:column]];

BOOL matchedFound = NO;
for (int i = 0; i < [seats_avail count]; i++)
{
if ([[seats_avail objectAtIndex:i] isEqualToString:status])
{
matchedFound = YES ;
break ;
}
}

if (matchedFound == YES)
{
UIButton * seat_btn = [UIButton buttonWithType:UIButtonTypeCustom];
[seat_btn setBackgroundImage:[UIImage imageNamed:@"seatavailable.png"] forState:UIControlStateNormal];
[seat_btn setBackgroundImage:[UIImage imageNamed:@"seatactive.png"] forState:UIControlStateSelected];
seat_btn.frame = CGRectMake(x_offset_col, y_offset_col, 22, 22);
tag_index = [[seat objectAtIndex:column] intValue];
[seat_btn setTitle:[seat objectAtIndex:column] forState:UIControlStateNormal];
seat_btn.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0];
seat_btn.tag = tag_index + row * 100;
[seat_btn addTarget:self
action:@selector(checkboxButton:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:seat_btn];

}
else
{
CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
imageView.image = [UIImage imageNamed:@"seat_not_available.png"];
[scrollview addSubview:imageView];
}
}

x_offset_col += ROW_COL_OFFSET ;

NSString *data = @"";
[seatControl addObject:data];
}
y_offset_row += ROW_COL_OFFSET;
}

UILabel *screenlabel = [[UILabel alloc] init];
screenlabel.frame = CGRectMake((scrollview_w-300)/2, 3, 300, 15);
screenlabel.text = @"Screen";
screenlabel.backgroundColor = [UIColor blackColor];
screenlabel.textColor = [UIColor whiteColor] ;
screenlabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
screenlabel.textAlignment = UITextAlignmentCenter;
[scrollview addSubview:screenlabel];

scrollview_h = y_offset_row + ROW_COL_OFFSET;
// Adjust scroll view
[scrollview setContentSize:CGSizeMake(scrollview_w, scrollview_h )];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
NSLog(@"1");

return scrollview;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
NSLog(@"2");
}

最佳答案

在:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
NSLog(@"1");

return scrollview;
}

您不应该返回 UIScrollView 本身,而是返回您想要滚动/缩放的 subview 。

如果您有许多 subview ,我建议您为从 viewForZoomingInScrollView 返回的所有 subview 创建一个容器 View ;您无需将座位添加到 ScrollView 本身,而是将它们添加到容器 View 。

关于iphone - 在 Objective-C 上缩放 UIScrollview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14211527/

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