【问题标题】:When Do You Need To Reset the view.frame Property After a Transformation iOS什么时候需要在转换 iOS 后重置 view.frame 属性
【发布时间】:2011-10-14 18:34:44
【问题描述】:

我正在制作一个类似于 Pulse 新闻阅读器的水平表格视图。我在网上找到了几个示例并使其正常工作,但我想知道何时需要在转换后设置 view.frame 属性。

我发现的示例在 90 度旋转后重置垂直表视图单元格内的水平表视图框架

self.tableViewCell.horizontalTableView.transform = rotateTable;
self.tableViewCell.horizontalTableView.frame = CGRectMake(0, 0, self.tableViewCell.horizontalTableView.frame.size.width, self.tableViewCell.horizontalTableView.frame.size.height);

更多上下文:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TableViewCell *cell = (TableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];

    CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
    self.tableViewCell.horizontalTableView.transform = rotateTable;
    self.tableViewCell.horizontalTableView.frame = CGRectMake(0, 0, self.tableViewCell.horizontalTableView.frame.size.width, self.tableViewCell.horizontalTableView.frame.size.height);

    self.tableViewCell.contentArray = [self.arrays objectAtIndex:indexPath.section];

    self.tableViewCell.horizontalTableView.allowsSelection = YES;
    cell = self.tableViewCell;
    self.tableViewCell = nil;

}

cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

}

但不要在单元格变换(旋转)后重置水平表格单元格的框架:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    [[NSBundle mainBundle] loadNibNamed:@"GameTableViewCell" owner:self options:nil];
    cell = self.gameTableCell;
    self.gameTableCell = nil;
}

CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
cell.transform = rotateImage;
return cell;

}

我尝试重置单元格的框架,但它对输出没有影响,即使我提供了

cell.frame = CGMakeRect(200, 200, cell.frame.size.height, cell.frame.size.width)

哪个应该在表格视图中移动单元格,不是吗?

如果我不重置self.tableViewCell.horizontalTableView.frame 的框架,水平表会旋转,但位置错误。

为什么我需要在旋转后重置水平表格视图的框架,而不是单个单元格(它们也被旋转)?

谢谢!

// iPortable 插图

【问题讨论】:

标签: iphone ios uitableview pulse


【解决方案1】:

好的,再读一遍我明白你在问什么^^

  1. 设置框架会导致重绘视图。这需要完成,否则旧图形可能会干扰并且看起来很难看。重绘也应该锐化绘制线。
  2. 您将单元格的框架设置在其他位置(此处为 200,200),但没有任何效果是完全正常的。单元格不能位于CGPointZero 之外的其他位置。如果你想取代它,你必须创建一个更大的视图并移动内容。 (视图可以是半透明的,但会显着减慢旧设备上的滚动速度)
  3. 你问为什么你不必重绘每个单元格而只需要重绘tableView。实际上我并不确切知道这一点,但实际上应该是表格视图的框架更改。通常情况下,通过旋转设备会发生帧变化,从而改变宽度。现在,开发人员将每个单元格更新为新大小是非常愚蠢的。感谢 Apple,tableView 为屏幕上可见的每个单元格调用必要的更新请求。最终,所有视图都调用了相同的方法:drawRect:

【讨论】:

【解决方案2】:

我无法理解您的所有问题,但会尽力而为。
当您旋转设备时,表格视图大小会发生变化(框架)。所以你必须为表格视图设置新的框架来完成新的“设计”(将表格视图拉伸到整个宽度)。对于单元格而言,情况并非如此,因为即使在旋转之后它们仍保持相同的大小。
一个例子:

________
|______| -> a cell width: 50 height: 200

旋转后:

___
| |
| | -> still the same size, width: 50 height: 200
|_|

CGAffineTransform 只是视觉效果。它对其子视图的大小和位置都没有影响。所以你必须为你的单元格做与你的表格视图完全相反的旋转。向哪个方向旋转是错误的?上下颠倒、左转、右转,还是使用边旋转导致选择错误的旋转点?

【讨论】:

  • 感谢您的意见。我没有旋转设备。我有一个表格视图,其中嵌入了另一个表格视图作为单元格的子视图。一个表格视图上下移动,另一个表格视图左右移动。 iosstuff.wordpress.com/2011/06/29/…
猜你喜欢
  • 2013-08-29
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 2019-09-14
  • 2013-12-26
  • 2012-09-16
  • 2011-10-21
  • 2022-11-09
相关资源
最近更新 更多